Skip to main content

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(test, feature(test))]
14#![deny(unsafe_op_in_unsafe_fn)]
15#![feature(allocator_api)]
16#![feature(ascii_char)]
17#![feature(ascii_char_variants)]
18#![feature(auto_traits)]
19#![feature(const_default)]
20#![feature(const_trait_impl)]
21#![feature(dropck_eyepatch)]
22#![feature(extend_one)]
23#![feature(file_buffered)]
24#![feature(map_try_insert)]
25#![feature(min_specialization)]
26#![feature(negative_impls)]
27#![feature(never_type)]
28#![feature(pattern_type_macro)]
29#![feature(pattern_types)]
30#![feature(ptr_alignment_type)]
31#![feature(rustc_attrs)]
32#![feature(sized_hierarchy)]
33#![feature(thread_id_value)]
34#![feature(trusted_len)]
35#![feature(type_alias_impl_trait)]
36#![feature(unwrap_infallible)]
37// tidy-alphabetical-end
38
39// This allows derive macros to reference this crate
40extern crate self as rustc_data_structures;
41
42use std::fmt;
43
44pub use atomic_ref::AtomicRef;
45pub use ena::{snapshot_vec, undo_log, unify};
46// Re-export `hashbrown::hash_table`, because it's part of our API
47// (via `ShardedHashMap`), and because it lets other compiler crates use the
48// lower-level `HashTable` API without a tricky `hashbrown` dependency.
49pub use hashbrown::hash_table;
50pub use rustc_index::static_assert_size;
51// Re-export some data-structure crates which are part of our public API.
52pub use {either, indexmap, smallvec, thin_vec};
53
54pub mod aligned;
55pub mod base_n;
56pub mod binary_search_util;
57pub mod fingerprint;
58pub mod flat_map_in_place;
59pub mod flock;
60pub mod frozen;
61pub mod fx;
62pub mod graph;
63pub mod intern;
64pub mod jobserver;
65pub mod marker;
66pub mod memmap;
67pub mod obligation_forest;
68pub mod owned_slice;
69pub mod packed;
70pub mod profiling;
71pub mod sharded;
72pub mod small_c_str;
73pub mod snapshot_map;
74pub mod sorted_map;
75pub mod sso;
76pub mod stable_hasher;
77pub mod stack;
78pub mod steal;
79pub mod svh;
80pub mod sync;
81pub mod tagged_ptr;
82pub mod temp_dir;
83pub mod thinvec;
84pub mod thousands;
85pub mod transitive_relation;
86pub mod unhash;
87pub mod union_find;
88pub mod unord;
89pub mod vec_cache;
90pub mod work_queue;
91
92mod atomic_ref;
93
94/// This calls the passed function while ensuring it won't be inlined into the caller.
95#[inline(never)]
96#[cold]
97pub fn outline<F: FnOnce() -> R, R>(f: F) -> R {
98    f()
99}
100
101/// Returns a structure that calls `f` when dropped.
102pub fn defer<F: FnOnce()>(f: F) -> OnDrop<F> {
103    OnDrop(Some(f))
104}
105
106pub struct OnDrop<F: FnOnce()>(Option<F>);
107
108impl<F: FnOnce()> OnDrop<F> {
109    /// Disables on-drop call.
110    #[inline]
111    pub fn disable(mut self) {
112        self.0.take();
113    }
114}
115
116impl<F: FnOnce()> Drop for OnDrop<F> {
117    #[inline]
118    fn drop(&mut self) {
119        if let Some(f) = self.0.take() {
120            f();
121        }
122    }
123}
124
125/// This is a marker for a fatal compiler error used with `resume_unwind`.
126pub struct FatalErrorMarker;
127
128/// Turns a closure that takes an `&mut Formatter` into something that can be display-formatted.
129pub fn make_display(f: impl Fn(&mut fmt::Formatter<'_>) -> fmt::Result) -> impl fmt::Display {
130    struct Printer<F> {
131        f: F,
132    }
133    impl<F> fmt::Display for Printer<F>
134    where
135        F: Fn(&mut fmt::Formatter<'_>) -> fmt::Result,
136    {
137        fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
138            (self.f)(fmt)
139        }
140    }
141
142    Printer { f }
143}
144
145// See comment in compiler/rustc_middle/src/tests.rs and issue #27438.
146#[doc(hidden)]
147pub fn __noop_fix_for_windows_dllimport_issue() {}
148
149#[macro_export]
150macro_rules! external_bitflags_debug {
151    ($Name:ident) => {
152        impl ::std::fmt::Debug for $Name {
153            fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
154                ::bitflags::parser::to_writer(self, f)
155            }
156        }
157    };
158}