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