diff --git a/src/lib.rs b/src/lib.rs index 7dc16bf..95ec417 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -22,10 +22,10 @@ use core::ops::{ use serde::{Deserialize, Deserializer, Serialize, Serializer}; #[cfg(all(feature = "borsh", not(feature = "std")))] -use alloc::{collections::BTreeMap, string::ToString, vec}; +use alloc::{collections::BTreeMap, string::ToString}; #[cfg(all(feature = "borsh", feature = "std"))] -use std::{collections::BTreeMap, string::ToString, vec}; +use std::{collections::BTreeMap, string::ToString}; #[cfg(feature = "schemars")] use schemars::JsonSchema; @@ -1105,7 +1105,11 @@ where Self: Number, { fn deserialize_reader(reader: &mut R) -> borsh::io::Result { - let mut buf = vec![0u8; core::mem::size_of::()]; + // Ideally, we'd want a buffer of size `BITS >> 3` or `size_of::`, but that's not possible + // with arrays. + // So instead we'll do a 16 byte buffer which handles the largest arbitrary-ints possible - + // not ideal, but still pretty small and better than going through an allocator. + let mut buf = [0u8; 16]; reader.read(&mut buf)?; let value = T::deserialize(&mut &buf[..])?; if value >= Self::MIN.value() && value <= Self::MAX.value() { diff --git a/tests/tests.rs b/tests/tests.rs index ac84078..9d8ffaf 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -1914,9 +1914,7 @@ fn serde() { #[cfg(feature = "borsh")] mod borsh_tests { - use arbitrary_int::{ - u1, u14, u15, u50, u6, u63, u65, u7, u72, u79, u80, u81, u9, Number, UInt, - }; + use arbitrary_int::{u1, u14, u15, u6, u63, u65, u7, u72, u79, u80, u81, u9, Number, UInt}; use borsh::schema::BorshSchemaContainer; use borsh::{BorshDeserialize, BorshSchema, BorshSerialize}; use std::fmt::Debug;