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(hash_raw_entry)]
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 captures;
52pub mod fingerprint;
53pub mod flat_map_in_place;
54pub mod flock;
55pub mod frozen;
56pub mod fx;
57pub mod graph;
58pub mod intern;
59pub mod jobserver;
60pub mod marker;
61pub mod memmap;
62pub mod obligation_forest;
63pub mod owned_slice;
64pub mod packed;
65pub mod profiling;
66pub mod sharded;
67pub mod small_c_str;
68pub mod snapshot_map;
69pub mod sorted_map;
70pub mod sso;
71pub mod stable_hasher;
72pub mod stack;
73pub mod steal;
74pub mod svh;
75pub mod sync;
76pub mod tagged_ptr;
77pub mod temp_dir;
78pub mod thinvec;
79pub mod thousands;
80pub mod transitive_relation;
81pub mod unhash;
82pub mod unord;
83pub mod vec_cache;
84pub mod work_queue;
85
86mod atomic_ref;
87mod hashes;
88
89/// This calls the passed function while ensuring it won't be inlined into the caller.
90#[inline(never)]
91#[cold]
92pub fn outline<F: FnOnce() -> R, R>(f: F) -> R {
93    f()
94}
95
96/// Returns a structure that calls `f` when dropped.
97pub fn defer<F: FnOnce()>(f: F) -> OnDrop<F> {
98    OnDrop(Some(f))
99}
100
101pub struct OnDrop<F: FnOnce()>(Option<F>);
102
103impl<F: FnOnce()> OnDrop<F> {
104    /// Disables on-drop call.
105    #[inline]
106    pub fn disable(mut self) {
107        self.0.take();
108    }
109}
110
111impl<F: FnOnce()> Drop for OnDrop<F> {
112    #[inline]
113    fn drop(&mut self) {
114        if let Some(f) = self.0.take() {
115            f();
116        }
117    }
118}
119
120/// This is a marker for a fatal compiler error used with `resume_unwind`.
121pub struct FatalErrorMarker;
122
123/// Turns a closure that takes an `&mut Formatter` into something that can be display-formatted.
124pub fn make_display(f: impl Fn(&mut fmt::Formatter<'_>) -> fmt::Result) -> impl fmt::Display {
125    struct Printer<F> {
126        f: F,
127    }
128    impl<F> fmt::Display for Printer<F>
129    where
130        F: Fn(&mut fmt::Formatter<'_>) -> fmt::Result,
131    {
132        fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
133            (self.f)(fmt)
134        }
135    }
136
137    Printer { f }
138}
139
140// See comment in compiler/rustc_middle/src/tests.rs and issue #27438.
141#[doc(hidden)]
142pub fn __noop_fix_for_windows_dllimport_issue() {}
143
144#[macro_export]
145macro_rules! external_bitflags_debug {
146    ($Name:ident) => {
147        impl ::std::fmt::Debug for $Name {
148            fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
149                ::bitflags::parser::to_writer(self, f)
150            }
151        }
152    };
153}