Skip to main content

burn_dataset/dataset/
error.rs

1use std::fmt;
2
3/// Default error type used by [`Dataset`](crate::Dataset) implementations that don't define
4/// their own.
5///
6/// Wraps specific dataset error types (e.g. `SqliteDatasetError`) at call sites that don't need
7/// the concrete error type. Use `result.map_err(DatasetError::new)?` to convert and propagate an
8/// error; `?` alone does not perform this wrapping.
9#[derive(Debug)]
10pub struct DatasetError(Box<dyn std::error::Error + Send + Sync + 'static>);
11
12impl DatasetError {
13    /// Wraps an arbitrary error as a [`DatasetError`].
14    pub fn new<E>(err: E) -> Self
15    where
16        E: std::error::Error + Send + Sync + 'static,
17    {
18        Self(Box::new(err))
19    }
20}
21
22impl fmt::Display for DatasetError {
23    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
24        write!(f, "{}", self.0)
25    }
26}
27
28impl std::error::Error for DatasetError {
29    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
30        Some(self.0.as_ref())
31    }
32}