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