From 88f1134cd8e200bf9e85c0f9c757a4ae4c694fbe Mon Sep 17 00:00:00 2001 From: Yunare Date: Sun, 30 Aug 2026 19:05:38 +0000 Subject: [PATCH 1/2] 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 00000000..2d3015b0 --- /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 d2d69049..8d3e2ccd 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 { From ddbc39e7895c37d7fa3284d8a4b72b79345536d0 Mon Sep 17 00:00:00 2001 From: Yunare Date: Wed, 2 Sep 2026 12:28:34 +0000 Subject: [PATCH 2/2] fix: address review feedback on errors.rs (remove unnecessary Layout import) --- src/errors.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/errors.rs b/src/errors.rs index 2d3015b0..d34ea5ae 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -4,8 +4,6 @@ // 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 { @@ -14,7 +12,7 @@ pub enum CollectionAllocErr { /// The allocator return an error AllocErr { /// The layout that was passed to the allocator - layout: Layout, + layout: alloc::alloc::Layout, }, }