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