From 88f1134cd8e200bf9e85c0f9c757a4ae4c694fbe Mon Sep 17 00:00:00 2001 From: Yunare Date: Sun, 30 Aug 2026 19:05:38 +0000 Subject: [PATCH] refactor: modularize CollectionAllocErr into errors.rs Move CollectionAllocErr enum and its Display/Error impls from lib.rs to a dedicated src/errors.rs module, re-exporting it from lib.rs to preserve the public API. Fixes #513 --- src/errors.rs | 27 +++++++++++++++++++++++++++ src/lib.rs | 22 +++------------------- 2 files changed, 30 insertions(+), 19 deletions(-) create mode 100644 src/errors.rs diff --git a/src/errors.rs b/src/errors.rs new file mode 100644 index 0000000..2d3015b --- /dev/null +++ b/src/errors.rs @@ -0,0 +1,27 @@ +// 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. + +use alloc::alloc::Layout; + +/// 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 {} diff --git a/src/lib.rs b/src/lib.rs index d2d6904..8d3e2cc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -67,8 +67,11 @@ extern crate std; #[cfg(feature = "borsh")] mod borsh; +mod errors; mod rawsmallvec; +pub use errors::CollectionAllocErr; + #[cfg(feature = "bytes")] use bytes::{ BufMut, @@ -138,25 +141,6 @@ use { } }; -/// 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 {