Skip to main content

gpui_util_gpui_unofficial/
lib.rs

1// FluentBuilder
2// pub use gpui_util::{FutureExt, Timeout, arc_cow::ArcCow};
3
4use std::{
5    env,
6    ops::AddAssign,
7    panic::Location,
8    pin::Pin,
9    sync::OnceLock,
10    task::{Context, Poll},
11    time::Instant,
12};
13
14pub mod arc_cow;
15
16pub fn post_inc<T: From<u8> + AddAssign<T> + Copy>(value: &mut T) -> T {
17    let prev = *value;
18    *value += T::from(1);
19    prev
20}
21
22pub fn measure<R>(label: &str, f: impl FnOnce() -> R) -> R {
23    static ZED_MEASUREMENTS: OnceLock<bool> = OnceLock::new();
24    let zed_measurements = ZED_MEASUREMENTS.get_or_init(|| {
25        env::var("ZED_MEASUREMENTS")
26            .map(|measurements| measurements == "1" || measurements == "true")
27            .unwrap_or(false)
28    });
29
30    if *zed_measurements {
31        let start = Instant::now();
32        let result = f();
33        let elapsed = start.elapsed();
34        eprintln!("{}: {:?}", label, elapsed);
35        result
36    } else {
37        f()
38    }
39}
40
41#[macro_export]
42macro_rules! debug_panic {
43    ( $($fmt_arg:tt)* ) => {
44        if cfg!(debug_assertions) {
45            panic!( $($fmt_arg)* );
46        } else {
47            let backtrace = std::backtrace::Backtrace::capture();
48            log::error!("{}\n{:?}", format_args!($($fmt_arg)*), backtrace);
49        }
50    };
51}
52
53#[track_caller]
54pub fn some_or_debug_panic<T>(option: Option<T>) -> Option<T> {
55    #[cfg(debug_assertions)]
56    if option.is_none() {
57        panic!("Unexpected None");
58    }
59    option
60}
61
62/// Expands to an immediately-invoked function expression. Good for using the ? operator
63/// in functions which do not return an Option or Result.
64///
65/// Accepts a normal block, an async block, or an async move block.
66#[macro_export]
67macro_rules! maybe {
68    ($block:block) => {
69        (|| $block)()
70    };
71    (async $block:block) => {
72        (async || $block)()
73    };
74    (async move $block:block) => {
75        (async move || $block)()
76    };
77}
78pub trait ResultExt<E> {
79    type Ok;
80
81    fn log_err(self) -> Option<Self::Ok>;
82    /// Like [`ResultExt::log_err`], but uses `{:?}` formatting so `anyhow::Error` values emit their
83    /// full backtrace. Reach for this only when a backtrace is genuinely wanted — most call sites
84    /// should stick with `log_err` / `warn_on_err`, whose output is a single chained error message.
85    fn log_err_with_backtrace(self) -> Option<Self::Ok>
86    where
87        E: std::fmt::Debug;
88    /// Assert that this result should never be an error in development or tests.
89    fn debug_assert_ok(self, reason: &str) -> Self;
90    fn warn_on_err(self) -> Option<Self::Ok>;
91    fn log_with_level(self, level: log::Level) -> Option<Self::Ok>;
92    fn anyhow(self) -> anyhow::Result<Self::Ok>
93    where
94        E: Into<anyhow::Error>;
95}
96
97impl<T, E> ResultExt<E> for Result<T, E>
98where
99    E: std::fmt::Display,
100{
101    type Ok = T;
102
103    #[track_caller]
104    fn log_err(self) -> Option<T> {
105        self.log_with_level(log::Level::Error)
106    }
107
108    #[track_caller]
109    fn log_err_with_backtrace(self) -> Option<T>
110    where
111        E: std::fmt::Debug,
112    {
113        match self {
114            Ok(value) => Some(value),
115            Err(error) => {
116                log_error_with_caller(
117                    *Location::caller(),
118                    DebugAsDisplay(&error),
119                    log::Level::Error,
120                );
121                None
122            }
123        }
124    }
125
126    #[track_caller]
127    fn debug_assert_ok(self, reason: &str) -> Self {
128        if let Err(error) = &self {
129            debug_panic!("{reason} - {error:#}");
130        }
131        self
132    }
133
134    #[track_caller]
135    fn warn_on_err(self) -> Option<T> {
136        self.log_with_level(log::Level::Warn)
137    }
138
139    #[track_caller]
140    fn log_with_level(self, level: log::Level) -> Option<T> {
141        match self {
142            Ok(value) => Some(value),
143            Err(error) => {
144                log_error_with_caller(*Location::caller(), error, level);
145                None
146            }
147        }
148    }
149
150    fn anyhow(self) -> anyhow::Result<T>
151    where
152        E: Into<anyhow::Error>,
153    {
154        self.map_err(Into::into)
155    }
156}
157
158fn log_error_with_caller<E>(caller: core::panic::Location<'_>, error: E, level: log::Level)
159where
160    E: std::fmt::Display,
161{
162    #[cfg(not(windows))]
163    let file = caller.file();
164    #[cfg(windows)]
165    let file = caller.file().replace('\\', "/");
166    // In this codebase all crates reside in a `crates` directory,
167    // so discard the prefix up to that segment to find the crate name
168    let file = file.split_once("crates/");
169    let target = file.as_ref().and_then(|(_, s)| s.split_once("/src/"));
170
171    let module_path = target.map(|(krate, module)| {
172        if module.starts_with(krate) {
173            module.trim_end_matches(".rs").replace('/', "::")
174        } else {
175            krate.to_owned() + "::" + &module.trim_end_matches(".rs").replace('/', "::")
176        }
177    });
178    let file = file.map(|(_, file)| format!("crates/{file}"));
179    log::logger().log(
180        &log::Record::builder()
181            .target(module_path.as_deref().unwrap_or(""))
182            .module_path(file.as_deref())
183            .args(format_args!("{:#}", error))
184            .file(Some(caller.file()))
185            .line(Some(caller.line()))
186            .level(level)
187            .build(),
188    );
189}
190
191pub fn log_err<E: std::fmt::Display>(error: &E) {
192    log_error_with_caller(*Location::caller(), error, log::Level::Error);
193}
194
195// Forces `{:?}` formatting through a `Display`-bounded logging helper so `anyhow::Error` emits a
196// backtrace instead of the single-line chained message produced by its `Display`/`{:#}` forms.
197struct DebugAsDisplay<'a, E>(&'a E);
198
199impl<E: std::fmt::Debug> std::fmt::Display for DebugAsDisplay<'_, E> {
200    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
201        write!(f, "{:?}", self.0)
202    }
203}
204
205pub trait TryFutureExt {
206    fn log_err(self) -> LogErrorFuture<Self>
207    where
208        Self: Sized;
209
210    fn log_tracked_err(self, location: core::panic::Location<'static>) -> LogErrorFuture<Self>
211    where
212        Self: Sized;
213
214    fn warn_on_err(self) -> LogErrorFuture<Self>
215    where
216        Self: Sized;
217    fn unwrap(self) -> UnwrapFuture<Self>
218    where
219        Self: Sized;
220}
221
222/// `{:?}`-formatting companion to [`TryFutureExt`]; emits a backtrace for `anyhow::Error`. Prefer
223/// [`TryFutureExt`] unless a backtrace is genuinely wanted.
224pub trait TryFutureExtBacktrace {
225    fn log_err_with_backtrace(self) -> LogErrorWithBacktraceFuture<Self>
226    where
227        Self: Sized;
228
229    fn log_tracked_err_with_backtrace(
230        self,
231        location: core::panic::Location<'static>,
232    ) -> LogErrorWithBacktraceFuture<Self>
233    where
234        Self: Sized;
235}
236
237impl<F, T, E> TryFutureExt for F
238where
239    F: Future<Output = Result<T, E>>,
240    E: std::fmt::Display,
241{
242    #[track_caller]
243    fn log_err(self) -> LogErrorFuture<Self>
244    where
245        Self: Sized,
246    {
247        let location = Location::caller();
248        LogErrorFuture(self, log::Level::Error, *location)
249    }
250
251    fn log_tracked_err(self, location: core::panic::Location<'static>) -> LogErrorFuture<Self>
252    where
253        Self: Sized,
254    {
255        LogErrorFuture(self, log::Level::Error, location)
256    }
257
258    #[track_caller]
259    fn warn_on_err(self) -> LogErrorFuture<Self>
260    where
261        Self: Sized,
262    {
263        let location = Location::caller();
264        LogErrorFuture(self, log::Level::Warn, *location)
265    }
266
267    fn unwrap(self) -> UnwrapFuture<Self>
268    where
269        Self: Sized,
270    {
271        UnwrapFuture(self)
272    }
273}
274
275impl<F, T, E> TryFutureExtBacktrace for F
276where
277    F: Future<Output = Result<T, E>>,
278    E: std::fmt::Debug,
279{
280    #[track_caller]
281    fn log_err_with_backtrace(self) -> LogErrorWithBacktraceFuture<Self>
282    where
283        Self: Sized,
284    {
285        let location = Location::caller();
286        LogErrorWithBacktraceFuture(self, log::Level::Error, *location)
287    }
288
289    fn log_tracked_err_with_backtrace(
290        self,
291        location: core::panic::Location<'static>,
292    ) -> LogErrorWithBacktraceFuture<Self>
293    where
294        Self: Sized,
295    {
296        LogErrorWithBacktraceFuture(self, log::Level::Error, location)
297    }
298}
299
300#[must_use]
301pub struct LogErrorFuture<F>(F, log::Level, core::panic::Location<'static>);
302
303impl<F, T, E> Future for LogErrorFuture<F>
304where
305    F: Future<Output = Result<T, E>>,
306    E: std::fmt::Display,
307{
308    type Output = Option<T>;
309
310    fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
311        let level = self.1;
312        let location = self.2;
313        let inner = unsafe { Pin::new_unchecked(&mut self.get_unchecked_mut().0) };
314        match inner.poll(cx) {
315            Poll::Ready(output) => Poll::Ready(match output {
316                Ok(output) => Some(output),
317                Err(error) => {
318                    log_error_with_caller(location, error, level);
319                    None
320                }
321            }),
322            Poll::Pending => Poll::Pending,
323        }
324    }
325}
326
327#[must_use]
328pub struct LogErrorWithBacktraceFuture<F>(F, log::Level, core::panic::Location<'static>);
329
330impl<F, T, E> Future for LogErrorWithBacktraceFuture<F>
331where
332    F: Future<Output = Result<T, E>>,
333    E: std::fmt::Debug,
334{
335    type Output = Option<T>;
336
337    fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
338        let level = self.1;
339        let location = self.2;
340        let inner = unsafe { Pin::new_unchecked(&mut self.get_unchecked_mut().0) };
341        match inner.poll(cx) {
342            Poll::Ready(output) => Poll::Ready(match output {
343                Ok(output) => Some(output),
344                Err(error) => {
345                    log_error_with_caller(location, DebugAsDisplay(&error), level);
346                    None
347                }
348            }),
349            Poll::Pending => Poll::Pending,
350        }
351    }
352}
353
354pub struct UnwrapFuture<F>(F);
355
356impl<F, T, E> Future for UnwrapFuture<F>
357where
358    F: Future<Output = Result<T, E>>,
359    E: std::fmt::Debug,
360{
361    type Output = T;
362
363    fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
364        let inner = unsafe { Pin::new_unchecked(&mut self.get_unchecked_mut().0) };
365        match inner.poll(cx) {
366            Poll::Ready(result) => Poll::Ready(result.unwrap()),
367            Poll::Pending => Poll::Pending,
368        }
369    }
370}
371
372pub struct Deferred<F: FnOnce()>(Option<F>);
373
374impl<F: FnOnce()> Deferred<F> {
375    /// Drop without running the deferred function.
376    pub fn abort(mut self) {
377        self.0.take();
378    }
379}
380
381impl<F: FnOnce()> Drop for Deferred<F> {
382    fn drop(&mut self) {
383        if let Some(f) = self.0.take() {
384            f()
385        }
386    }
387}
388
389/// Run the given function when the returned value is dropped (unless it's cancelled).
390#[must_use]
391pub fn defer<F: FnOnce()>(f: F) -> Deferred<F> {
392    Deferred(Some(f))
393}