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    /// Assert that this result should never be an error in development or tests.
 83    fn debug_assert_ok(self, reason: &str) -> Self;
 84    fn warn_on_err(self) -> Option<Self::Ok>;
 85    fn is_err_or<F>(self, f: F) -> bool
 86    where
 87        F: FnOnce(Self::Ok) -> bool;
 88
 89    fn log_with_level(self, level: log::Level) -> Option<Self::Ok>;
 90    fn anyhow(self) -> anyhow::Result<Self::Ok>
 91    where
 92        E: Into<anyhow::Error>;
 93}
 94
 95impl<T, E> ResultExt<E> for Result<T, E>
 96where
 97    E: std::fmt::Debug,
 98{
 99    type Ok = T;
100
101    #[track_caller]
102    fn log_err(self) -> Option<T> {
103        self.log_with_level(log::Level::Error)
104    }
105
106    #[track_caller]
107    fn debug_assert_ok(self, reason: &str) -> Self {
108        if let Err(error) = &self {
109            debug_panic!("{reason} - {error:?}");
110        }
111        self
112    }
113
114    #[track_caller]
115    fn warn_on_err(self) -> Option<T> {
116        self.log_with_level(log::Level::Warn)
117    }
118
119    fn is_err_or<F>(self, f: F) -> bool
120    where
121        F: FnOnce(T) -> bool,
122    {
123        match self {
124            Ok(value) => f(value),
125            Err(_) => true,
126        }
127    }
128
129    #[track_caller]
130    fn log_with_level(self, level: log::Level) -> Option<T> {
131        match self {
132            Ok(value) => Some(value),
133            Err(error) => {
134                log_error_with_caller(*Location::caller(), error, level);
135                None
136            }
137        }
138    }
139
140    fn anyhow(self) -> anyhow::Result<T>
141    where
142        E: Into<anyhow::Error>,
143    {
144        self.map_err(Into::into)
145    }
146}
147
148fn log_error_with_caller<E>(caller: core::panic::Location<'_>, error: E, level: log::Level)
149where
150    E: std::fmt::Debug,
151{
152    #[cfg(not(windows))]
153    let file = caller.file();
154    #[cfg(windows)]
155    let file = caller.file().replace('\\', "/");
156    // In this codebase all crates reside in a `crates` directory,
157    // so discard the prefix up to that segment to find the crate name
158    let file = file.split_once("crates/");
159    let target = file.as_ref().and_then(|(_, s)| s.split_once("/src/"));
160
161    let module_path = target.map(|(krate, module)| {
162        if module.starts_with(krate) {
163            module.trim_end_matches(".rs").replace('/', "::")
164        } else {
165            krate.to_owned() + "::" + &module.trim_end_matches(".rs").replace('/', "::")
166        }
167    });
168    let file = file.map(|(_, file)| format!("crates/{file}"));
169    log::logger().log(
170        &log::Record::builder()
171            .target(module_path.as_deref().unwrap_or(""))
172            .module_path(file.as_deref())
173            .args(format_args!("{:?}", error))
174            .file(Some(caller.file()))
175            .line(Some(caller.line()))
176            .level(level)
177            .build(),
178    );
179}
180
181pub fn log_err<E: std::fmt::Debug>(error: &E) {
182    log_error_with_caller(*Location::caller(), error, log::Level::Error);
183}
184
185pub trait TryFutureExt {
186    fn log_err(self) -> LogErrorFuture<Self>
187    where
188        Self: Sized;
189
190    fn log_tracked_err(self, location: core::panic::Location<'static>) -> LogErrorFuture<Self>
191    where
192        Self: Sized;
193
194    fn warn_on_err(self) -> LogErrorFuture<Self>
195    where
196        Self: Sized;
197    fn unwrap(self) -> UnwrapFuture<Self>
198    where
199        Self: Sized;
200}
201
202impl<F, T, E> TryFutureExt for F
203where
204    F: Future<Output = Result<T, E>>,
205    E: std::fmt::Debug,
206{
207    #[track_caller]
208    fn log_err(self) -> LogErrorFuture<Self>
209    where
210        Self: Sized,
211    {
212        let location = Location::caller();
213        LogErrorFuture(self, log::Level::Error, *location)
214    }
215
216    fn log_tracked_err(self, location: core::panic::Location<'static>) -> LogErrorFuture<Self>
217    where
218        Self: Sized,
219    {
220        LogErrorFuture(self, log::Level::Error, location)
221    }
222
223    #[track_caller]
224    fn warn_on_err(self) -> LogErrorFuture<Self>
225    where
226        Self: Sized,
227    {
228        let location = Location::caller();
229        LogErrorFuture(self, log::Level::Warn, *location)
230    }
231
232    fn unwrap(self) -> UnwrapFuture<Self>
233    where
234        Self: Sized,
235    {
236        UnwrapFuture(self)
237    }
238}
239
240#[must_use]
241pub struct LogErrorFuture<F>(F, log::Level, core::panic::Location<'static>);
242
243impl<F, T, E> Future for LogErrorFuture<F>
244where
245    F: Future<Output = Result<T, E>>,
246    E: std::fmt::Debug,
247{
248    type Output = Option<T>;
249
250    fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
251        let level = self.1;
252        let location = self.2;
253        let inner = unsafe { Pin::new_unchecked(&mut self.get_unchecked_mut().0) };
254        match inner.poll(cx) {
255            Poll::Ready(output) => Poll::Ready(match output {
256                Ok(output) => Some(output),
257                Err(error) => {
258                    log_error_with_caller(location, error, level);
259                    None
260                }
261            }),
262            Poll::Pending => Poll::Pending,
263        }
264    }
265}
266
267pub struct UnwrapFuture<F>(F);
268
269impl<F, T, E> Future for UnwrapFuture<F>
270where
271    F: Future<Output = Result<T, E>>,
272    E: std::fmt::Debug,
273{
274    type Output = T;
275
276    fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
277        let inner = unsafe { Pin::new_unchecked(&mut self.get_unchecked_mut().0) };
278        match inner.poll(cx) {
279            Poll::Ready(result) => Poll::Ready(result.unwrap()),
280            Poll::Pending => Poll::Pending,
281        }
282    }
283}
284
285pub struct Deferred<F: FnOnce()>(Option<F>);
286
287impl<F: FnOnce()> Deferred<F> {
288    /// Drop without running the deferred function.
289    pub fn abort(mut self) {
290        self.0.take();
291    }
292}
293
294impl<F: FnOnce()> Drop for Deferred<F> {
295    fn drop(&mut self) {
296        if let Some(f) = self.0.take() {
297            f()
298        }
299    }
300}
301
302/// Run the given function when the returned value is dropped (unless it's cancelled).
303#[must_use]
304pub fn defer<F: FnOnce()>(f: F) -> Deferred<F> {
305    Deferred(Some(f))
306}