rustc_data_structures/lib.rs
1//! Various data structures used by the Rust compiler. The intention
2//! is that code in here should not be *specific* to rustc, so that
3//! it can be easily unit tested and so forth.
4//!
5//! # Note
6//!
7//! This API is completely unstable and subject to change.
8
9// tidy-alphabetical-start
10#![allow(internal_features)]
11#![allow(rustc::default_hash_types)]
12#![allow(rustc::potential_query_instability)]
13#![deny(unsafe_op_in_unsafe_fn)]
14#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
15#![doc(rust_logo)]
16#![feature(allocator_api)]
17#![feature(array_windows)]
18#![feature(ascii_char)]
19#![feature(ascii_char_variants)]
20#![feature(assert_matches)]
21#![feature(auto_traits)]
22#![feature(cfg_select)]
23#![feature(core_intrinsics)]
24#![feature(dropck_eyepatch)]
25#![feature(extend_one)]
26#![feature(file_buffered)]
27#![feature(map_try_insert)]
28#![feature(min_specialization)]
29#![feature(negative_impls)]
30#![feature(never_type)]
31#![feature(ptr_alignment_type)]
32#![feature(rustc_attrs)]
33#![feature(rustdoc_internals)]
34#![feature(sized_hierarchy)]
35#![feature(test)]
36#![feature(thread_id_value)]
37#![feature(type_alias_impl_trait)]
38#![feature(unwrap_infallible)]
39// tidy-alphabetical-end
40
41use std::fmt;
42
43pub use atomic_ref::AtomicRef;
44pub use ena::{snapshot_vec, undo_log, unify};
45pub use rustc_index::static_assert_size;
46
47pub mod aligned;
48pub mod base_n;
49pub mod binary_search_util;
50pub mod fingerprint;
51pub mod flat_map_in_place;
52pub mod flock;
53pub mod frozen;
54pub mod fx;
55pub mod graph;
56pub mod intern;
57pub mod jobserver;
58pub mod marker;
59pub mod memmap;
60pub mod obligation_forest;
61pub mod owned_slice;
62pub mod packed;
63pub mod profiling;
64pub mod sharded;
65pub mod small_c_str;
66pub mod snapshot_map;
67pub mod sorted_map;
68pub mod sso;
69pub mod stable_hasher;
70pub mod stack;
71pub mod steal;
72pub mod svh;
73pub mod sync;
74pub mod tagged_ptr;
75pub mod temp_dir;
76pub mod thinvec;
77pub mod thousands;
78pub mod transitive_relation;
79pub mod unhash;
80pub mod union_find;
81pub mod unord;
82pub mod vec_cache;
83pub mod work_queue;
84
85mod atomic_ref;
86
87/// This calls the passed function while ensuring it won't be inlined into the caller.
88#[inline(never)]
89#[cold]
90pub fn outline<F: FnOnce() -> R, R>(f: F) -> R {
91 f()
92}
93
94/// Returns a structure that calls `f` when dropped.
95pub fn defer<F: FnOnce()>(f: F) -> OnDrop<F> {
96 OnDrop(Some(f))
97}
98
99pub struct OnDrop<F: FnOnce()>(Option<F>);
100
101impl<F: FnOnce()> OnDrop<F> {
102 /// Disables on-drop call.
103 #[inline]
104 pub fn disable(mut self) {
105 self.0.take();
106 }
107}
108
109impl<F: FnOnce()> Drop for OnDrop<F> {
110 #[inline]
111 fn drop(&mut self) {
112 if let Some(f) = self.0.take() {
113 f();
114 }
115 }
116}
117
118/// This is a marker for a fatal compiler error used with `resume_unwind`.
119pub struct FatalErrorMarker;
120
121/// Turns a closure that takes an `&mut Formatter` into something that can be display-formatted.
122pub fn make_display(f: impl Fn(&mut fmt::Formatter<'_>) -> fmt::Result) -> impl fmt::Display {
123 struct Printer<F> {
124 f: F,
125 }
126 impl<F> fmt::Display for Printer<F>
127 where
128 F: Fn(&mut fmt::Formatter<'_>) -> fmt::Result,
129 {
130 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
131 (self.f)(fmt)
132 }
133 }
134
135 Printer { f }
136}
137
138// See comment in compiler/rustc_middle/src/tests.rs and issue #27438.
139#[doc(hidden)]
140pub fn __noop_fix_for_windows_dllimport_issue() {}
141
142#[macro_export]
143macro_rules! external_bitflags_debug {
144 ($Name:ident) => {
145 impl ::std::fmt::Debug for $Name {
146 fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
147 ::bitflags::parser::to_writer(self, f)
148 }
149 }
150 };
151}