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_match)]
23#![feature(core_intrinsics)]
24#![feature(dropck_eyepatch)]
25#![feature(extend_one)]
26#![feature(file_buffered)]
27#![feature(macro_metavar_expr)]
28#![feature(map_try_insert)]
29#![feature(min_specialization)]
30#![feature(negative_impls)]
31#![feature(never_type)]
32#![feature(ptr_alignment_type)]
33#![feature(rustc_attrs)]
34#![feature(rustdoc_internals)]
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 unord;
81pub mod vec_cache;
82pub mod work_queue;
83
84mod atomic_ref;
85
86/// This calls the passed function while ensuring it won't be inlined into the caller.
87#[inline(never)]
88#[cold]
89pub fn outline<F: FnOnce() -> R, R>(f: F) -> R {
90    f()
91}
92
93/// Returns a structure that calls `f` when dropped.
94pub fn defer<F: FnOnce()>(f: F) -> OnDrop<F> {
95    OnDrop(Some(f))
96}
97
98pub struct OnDrop<F: FnOnce()>(Option<F>);
99
100impl<F: FnOnce()> OnDrop<F> {
101    /// Disables on-drop call.
102    #[inline]
103    pub fn disable(mut self) {
104        self.0.take();
105    }
106}
107
108impl<F: FnOnce()> Drop for OnDrop<F> {
109    #[inline]
110    fn drop(&mut self) {
111        if let Some(f) = self.0.take() {
112            f();
113        }
114    }
115}
116
117/// This is a marker for a fatal compiler error used with `resume_unwind`.
118pub struct FatalErrorMarker;
119
120/// Turns a closure that takes an `&mut Formatter` into something that can be display-formatted.
121pub fn make_display(f: impl Fn(&mut fmt::Formatter<'_>) -> fmt::Result) -> impl fmt::Display {
122    struct Printer<F> {
123        f: F,
124    }
125    impl<F> fmt::Display for Printer<F>
126    where
127        F: Fn(&mut fmt::Formatter<'_>) -> fmt::Result,
128    {
129        fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
130            (self.f)(fmt)
131        }
132    }
133
134    Printer { f }
135}
136
137// See comment in compiler/rustc_middle/src/tests.rs and issue #27438.
138#[doc(hidden)]
139pub fn __noop_fix_for_windows_dllimport_issue() {}
140
141#[macro_export]
142macro_rules! external_bitflags_debug {
143    ($Name:ident) => {
144        impl ::std::fmt::Debug for $Name {
145            fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
146                ::bitflags::parser::to_writer(self, f)
147            }
148        }
149    };
150}