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