From 53571fd28325df8d9199c59bd04764dd25f2413a Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Fri, 22 Nov 2024 19:30:11 +0100 Subject: [PATCH] Add both ArrowError and Arrow2Error to SerializationError --- crates/store/re_types_core/src/result.rs | 57 +++++++++++++++++++++--- 1 file changed, 51 insertions(+), 6 deletions(-) diff --git a/crates/store/re_types_core/src/result.rs b/crates/store/re_types_core/src/result.rs index 181c600532bd..f03967072bfd 100644 --- a/crates/store/re_types_core/src/result.rs +++ b/crates/store/re_types_core/src/result.rs @@ -32,6 +32,10 @@ pub enum SerializationError { /// E.g. too many values (overflows i32). #[error(transparent)] ArrowError(#[from] ArcArrowError), + + /// E.g. too many values (overflows i32). + #[error(transparent)] + Arrow2Error(#[from] ArcArrow2Error), } impl std::fmt::Debug for SerializationError { @@ -75,20 +79,59 @@ impl SerializationError { match self { Self::MissingExtensionMetadata { backtrace, .. } | Self::NotImplemented { backtrace, .. } => Some(backtrace.clone()), - Self::ArrowError { .. } | Self::Context { .. } => None, + Self::ArrowError { .. } | Self::Arrow2Error { .. } | Self::Context { .. } => None, } } } +// ---------------------------------------------------------------------------- + +/// A cloneable wrapper around [`arrow::error::ArrowError`], for easier use. +/// +/// The motivation behind this type is that we often use code that can return a `arrow2::error::Error` +/// inside functions that return a `SerializationError`. By wrapping it we can use the ? operator and simplify the code. +/// Second, normally also [`arrow::error::ArrowError`] isn't cloneable, but `SerializationError` is. +#[derive(Clone, Debug)] +pub struct ArcArrowError(std::sync::Arc); + +impl From for ArcArrowError { + fn from(e: arrow::error::ArrowError) -> Self { + Self(std::sync::Arc::new(e)) + } +} + +impl From for SerializationError { + fn from(e: arrow::error::ArrowError) -> Self { + Self::ArrowError(ArcArrowError::from(e)) + } +} + +impl Deref for ArcArrowError { + type Target = arrow::error::ArrowError; + + #[inline] + fn deref(&self) -> &Self::Target { + self.0.as_ref() + } +} + +impl Display for ArcArrowError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + self.0.fmt(f) + } +} + +// ---------------------------------------------------------------------------- + /// A cloneable wrapper around `arrow2::error::Error`, for easier use. /// /// The motivation behind this type is that we often use code that can return a `arrow2::error::Error` /// inside functions that return a `SerializationError`. By wrapping it we can use the ? operator and simplify the code. /// Second, normally also `arrow2::error::Error` isn't cloneable, but `SerializationError` is. #[derive(Clone, Debug)] -pub struct ArcArrowError(std::sync::Arc); +pub struct ArcArrow2Error(std::sync::Arc); -impl From for ArcArrowError { +impl From for ArcArrow2Error { fn from(e: arrow2::error::Error) -> Self { Self(std::sync::Arc::new(e)) } @@ -96,11 +139,11 @@ impl From for ArcArrowError { impl From for SerializationError { fn from(e: arrow2::error::Error) -> Self { - Self::ArrowError(ArcArrowError::from(e)) + Self::Arrow2Error(ArcArrow2Error::from(e)) } } -impl Deref for ArcArrowError { +impl Deref for ArcArrow2Error { type Target = arrow2::error::Error; #[inline] @@ -109,12 +152,14 @@ impl Deref for ArcArrowError { } } -impl Display for ArcArrowError { +impl Display for ArcArrow2Error { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { self.0.fmt(f) } } +// ---------------------------------------------------------------------------- + pub type SerializationResult = ::std::result::Result; // ---