gpui: Relax AssetLogger trait bounds (#29450)

Loosen trait bounds from `std::error::Error` to `std::fmt::Display`,
since it's not required for the `log::error!` macro or any other bounds.

Specify that `AssetLogger` specifically logs errors in its
documentation.

Use the `futures::TryFutureExt` trait extension to use `inspect_err`
directly on a future.

Release Notes:

- N/A
This commit is contained in:
tidely 2025-04-29 22:43:54 +03:00 committed by GitHub
parent f2813f60ed
commit fde1cc78a1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,5 +1,5 @@
use crate::{App, SharedString, SharedUri}; use crate::{App, SharedString, SharedUri};
use futures::Future; use futures::{Future, TryFutureExt};
use std::fmt::Debug; use std::fmt::Debug;
use std::hash::{Hash, Hasher}; use std::hash::{Hash, Hasher};
@ -51,14 +51,17 @@ pub trait Asset: 'static {
) -> impl Future<Output = Self::Output> + Send + 'static; ) -> impl Future<Output = Self::Output> + Send + 'static;
} }
/// An asset Loader that logs whatever passes through it /// An asset Loader which logs the [`Err`] variant of a [`Result`] during loading
pub enum AssetLogger<T> { pub enum AssetLogger<T> {
#[doc(hidden)] #[doc(hidden)]
_Phantom(PhantomData<T>, &'static dyn crate::seal::Sealed), _Phantom(PhantomData<T>, &'static dyn crate::seal::Sealed),
} }
impl<R: Clone + Send, E: Clone + Send + std::error::Error, T: Asset<Output = Result<R, E>>> Asset impl<T, R, E> Asset for AssetLogger<T>
for AssetLogger<T> where
T: Asset<Output = Result<R, E>>,
R: Clone + Send,
E: Clone + Send + std::fmt::Display,
{ {
type Source = T::Source; type Source = T::Source;
@ -69,10 +72,7 @@ impl<R: Clone + Send, E: Clone + Send + std::error::Error, T: Asset<Output = Res
cx: &mut App, cx: &mut App,
) -> impl Future<Output = Self::Output> + Send + 'static { ) -> impl Future<Output = Self::Output> + Send + 'static {
let load = T::load(source, cx); let load = T::load(source, cx);
async { load.inspect_err(|e| log::error!("Failed to load asset: {}", e))
load.await
.inspect_err(|e| log::error!("Failed to load asset: {}", e))
}
} }
} }