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#![cfg_attr(bootstrap, feature(cfg_match))]
14#![cfg_attr(not(bootstrap), feature(cfg_select))]
15#![deny(unsafe_op_in_unsafe_fn)]
16#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
17#![doc(rust_logo)]
18#![feature(allocator_api)]
19#![feature(array_windows)]
20#![feature(ascii_char)]
21#![feature(ascii_char_variants)]
22#![feature(assert_matches)]
23#![feature(auto_traits)]
24#![feature(core_intrinsics)]
25#![feature(dropck_eyepatch)]
26#![feature(extend_one)]
27#![feature(file_buffered)]
28#![feature(macro_metavar_expr)]
29#![feature(map_try_insert)]
30#![feature(min_specialization)]
31#![feature(negative_impls)]
32#![feature(never_type)]
33#![feature(ptr_alignment_type)]
34#![feature(rustc_attrs)]
35#![feature(rustdoc_internals)]
36#![feature(test)]
37#![feature(thread_id_value)]
38#![feature(type_alias_impl_trait)]
39#![feature(unwrap_infallible)]
40// tidy-alphabetical-end
41
42use std::fmt;
43
44pub use atomic_ref::AtomicRef;
45pub use ena::{snapshot_vec, undo_log, unify};
46pub use rustc_index::static_assert_size;
47
48pub mod aligned;
49pub mod base_n;
50pub mod binary_search_util;
51pub mod fingerprint;
52pub mod flat_map_in_place;
53pub mod flock;
54pub mod frozen;
55pub mod fx;
56pub mod graph;
57pub mod intern;
58pub mod jobserver;
59pub mod marker;
60pub mod memmap;
61pub mod obligation_forest;
62pub mod owned_slice;
63pub mod packed;
64pub mod profiling;
65pub mod sharded;
66pub mod small_c_str;
67pub mod snapshot_map;
68pub mod sorted_map;
69pub mod sso;
70pub mod stable_hasher;
71pub mod stack;
72pub mod steal;
73pub mod svh;
74pub mod sync;
75pub mod tagged_ptr;
76pub mod temp_dir;
77pub mod thinvec;
78pub mod thousands;
79pub mod transitive_relation;
80pub mod unhash;
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}