From cea3f8d516a9b792671767e78d5369c3a8b7e9d3 Mon Sep 17 00:00:00 2001 From: Victor Koenders Date: Tue, 19 Sep 2023 18:35:59 +0200 Subject: [PATCH] Fixed a possible fallible allocation issue --- src/features/impl_alloc.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/features/impl_alloc.rs b/src/features/impl_alloc.rs index 4a1889e1..4ed00f8e 100644 --- a/src/features/impl_alloc.rs +++ b/src/features/impl_alloc.rs @@ -25,10 +25,10 @@ pub(crate) struct VecWriter { impl VecWriter { /// Create a new vec writer with the given capacity - pub fn with_capacity(cap: usize) -> Self { - Self { - inner: Vec::with_capacity(cap), - } + pub fn with_capacity(cap: usize) -> Result { + let mut inner = Vec::new(); + inner.try_reserve(cap)?; + Ok(Self { inner }) } // May not be used in all feature combinations #[allow(dead_code)] @@ -57,7 +57,7 @@ pub fn encode_to_vec(val: E, config: C) -> Result::new(writer, config); val.encode(&mut encoder)?; Ok(encoder.into_writer().inner) @@ -453,6 +453,8 @@ where } } +// TODO +// Vec does not implement Into for Box<[T]> because it allocates again impl Decode for Box<[T]> where T: Decode + 'static, @@ -465,7 +467,6 @@ where // TODO // Vec does not implement Into for Box<[T]> because it allocates again -// we could do this manually with `Box::try_new_uninit` #[cfg(not(feature = "unstable-strict-oom-checks"))] impl<'de, T> BorrowDecode<'de> for Box<[T]> where