diff --git a/src/errors.rs b/src/errors.rs new file mode 100644 index 00000000..d34ea5ae --- /dev/null +++ b/src/errors.rs @@ -0,0 +1,25 @@ +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +/// Error type for APIs with fallible heap allocation +#[derive(Debug)] +pub enum CollectionAllocErr { + /// Overflow `usize::MAX` or other error during size computation + CapacityOverflow, + /// The allocator return an error + AllocErr { + /// The layout that was passed to the allocator + layout: alloc::alloc::Layout, + }, +} + +impl core::fmt::Display for CollectionAllocErr { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + write!(f, "Allocation error: {:?}", self) + } +} + +impl core::error::Error for CollectionAllocErr {} diff --git a/src/lib.rs b/src/lib.rs index 73a081f4..3e60bf9e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -69,6 +69,7 @@ extern crate std; mod borsh; mod comparisons; mod conversions; +mod errors; mod macros; #[cfg(feature = "malloc_size_of")] mod mallocsizeof; @@ -80,6 +81,8 @@ mod references; mod serde; mod taggedlen; +pub use errors::CollectionAllocErr; + #[cfg(feature = "bytes")] use bytes::{ BufMut, @@ -132,25 +135,6 @@ use { taggedlen::TaggedLen }; -/// Error type for APIs with fallible heap allocation -#[derive(Debug)] -pub enum CollectionAllocErr { - /// Overflow `usize::MAX` or other error during size computation - CapacityOverflow, - /// The allocator return an error - AllocErr { - /// The layout that was passed to the allocator - layout: Layout - } -} -impl core::fmt::Display for CollectionAllocErr { - fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { - write!(f, "Allocation error: {:?}", self) - } -} - -impl core::error::Error for CollectionAllocErr {} - #[inline] fn infallible(result: Result) -> T { match result {