1#[allow(unused_extern_crates)]
3extern crate self as tycho_util;
4
5use std::collections::{HashMap, HashSet};
6use std::path::PathBuf;
7use std::process::Command;
8
9pub mod compression;
10pub mod config;
11pub mod io;
12pub mod progress_bar;
13pub mod serde_helpers;
14pub mod time;
15pub mod transactional;
16
17pub mod tl;
18
19#[cfg(feature = "fs")]
20pub mod fs {
21 pub use self::mapped_file::{MappedFile, MappedFileMut};
22 pub use self::target_writer::TargetWriter;
23
24 mod mapped_file;
25 mod target_writer;
26}
27
28pub mod futures {
29 pub use self::await_blocking::AwaitBlocking;
30 pub use self::box_future_or_noop::BoxFutureOrNoop;
31 pub use self::join_task::JoinTask;
32 pub use self::shared::{Shared, WeakShared, WeakSharedHandle};
33
34 mod await_blocking;
35 mod box_future_or_noop;
36 mod join_task;
37 mod shared;
38}
39
40pub mod mem {
41 pub use self::reclaimer::{Reclaimer, ReclaimerError};
42 pub use self::slicer::{
43 AllocatedMemoryConstraints, MemoryConstraint, MemoryConstraints, MemorySlicer,
44 MemorySlicerGuard, MemorySlicerRange,
45 };
46
47 mod reclaimer;
48 mod slicer;
49}
50
51pub mod num {
52 pub use self::median::{StreamingUnsignedMedian, VecOfStreamingUnsignedMedian};
53 pub use self::safe_avg::{SafeSignedAvg, SafeUnsignedAvg, SafeUnsignedVecAvg};
54
55 mod median;
56 mod safe_avg;
57}
58
59pub mod sync {
60 pub use self::once_take::*;
61 pub use self::priority_semaphore::{AcquireError, PrioritySemaphore, TryAcquireError};
62 pub use self::rayon::{rayon_run, rayon_run_fifo};
63 pub use self::task::{CancellationFlag, DebounceCancellationFlag, yield_on_complex};
64
65 mod once_take;
66 mod priority_semaphore;
67 mod rayon;
68 mod task;
69}
70
71#[cfg(any(test, feature = "test"))]
72pub mod test {
73 pub use self::logger::init_logger;
74
75 mod logger;
76}
77
78pub mod metrics {
79 pub use self::fs_usage::{FsUsageBuilder, FsUsageMonitor, Stats, StatsEntry};
80 pub use self::gauge_guard::GaugeGuard;
81 pub use self::histogram_guard::{HistogramGuard, HistogramGuardWithLabels};
82 pub use self::metrics_loop::spawn_metrics_loop;
83
84 mod fs_usage;
85 mod gauge_guard;
86 mod histogram_guard;
87 mod metrics_loop;
88}
89
90mod util {
91 pub(crate) mod linked_list;
92 pub(crate) mod wake_list;
93}
94
95#[cfg(feature = "cli")]
96pub mod cli;
97
98pub use dashmap::mapref::entry::Entry as DashMapEntry;
99
100pub type FastDashMap<K, V> = dashmap::DashMap<K, V, ahash::RandomState>;
101pub type FastDashSet<K> = dashmap::DashSet<K, ahash::RandomState>;
102pub type FastHashMap<K, V> = HashMap<K, V, ahash::RandomState>;
103pub type FastHashSet<K> = HashSet<K, ahash::RandomState>;
104pub type FastHasherState = ahash::RandomState;
105
106#[macro_export]
127macro_rules! realloc_box_enum {
128 ($value:expr, {
129 $target_variant:pat => Box::new($extracted:ident) => $target:expr,
130 $other_variant:pat => $other:expr,
131 }) => {{
132 let value: ::std::boxed::Box<_> = $value;
133 match ::core::convert::AsRef::as_ref(&value) {
134 #[allow(unused_variables)]
135 $target_variant => {
136 let $extracted = unsafe {
137 $crate::__internal::realloc_box(value, |value| match value {
138 $target_variant => $extracted,
139 _ => unreachable!(),
140 })
141 };
142 $target
143 }
144 $other_variant => $other,
145 }
146 }};
147}
148
149#[doc(hidden)]
150pub mod __internal {
151 pub use serde;
152
153 pub unsafe fn realloc_box<T, F, R>(value: Box<T>, f: F) -> Box<R>
158 where
159 F: FnOnce(T) -> R,
160 {
161 unsafe {
162 assert!(std::mem::align_of::<T>() == std::mem::align_of::<R>());
163
164 let ptr = Box::into_raw(value);
165 let value = std::ptr::read(ptr);
166
167 let ptr = std::alloc::realloc(
168 ptr.cast::<u8>(),
169 std::alloc::Layout::new::<T>(),
170 std::mem::size_of::<R>(),
171 )
172 .cast::<R>();
173
174 if ptr.is_null() {
175 std::alloc::handle_alloc_error(std::alloc::Layout::new::<R>());
176 }
177
178 std::ptr::write(ptr, f(value));
180
181 Box::from_raw(ptr)
182 }
183 }
184}
185
186pub fn project_root() -> Result<PathBuf, std::io::Error> {
187 let project_root = Command::new("git")
188 .arg("rev-parse")
189 .arg("--show-toplevel")
190 .output()?
191 .stdout;
192 let project_root = PathBuf::from(
194 String::from_utf8(project_root)
195 .map_err(|e| std::io::Error::other(format!("invalid project root: {e}")))?
196 .trim(),
197 );
198 Ok(project_root)
199}
200
201#[cfg(test)]
202mod tests {
203 #[test]
204 #[allow(dead_code)]
205 fn realloc_enum() {
206 enum Value {
207 One(BigValue1),
208 Two(BigValue2),
209 }
210
211 #[derive(Clone)]
212 struct BigValue1([u32; 10]);
213
214 #[derive(Clone)]
215 struct BigValue2([u32; 7]);
216
217 fn convert_to_one(value: Box<Value>) -> Option<Box<BigValue1>> {
218 realloc_box_enum!(value, {
219 Value::One(value) => Box::new(value) => Some(value),
220 _ => None,
221 })
222 }
223
224 let value = BigValue1([123; 10]);
225 let one = convert_to_one(Box::new(Value::One(value.clone())));
226 assert_eq!(one.unwrap().0, value.0);
227 }
228}