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