What changed, and why it matters
This commit adds a new `try_push` method to an internal `Buffer` trait used during Base58 encoding. It is a straightforward, additive change that lets encoding code gracefully handle a full fixed-size buffer in no-allocation builds. There is no bug fix, no behavior change to existing code paths, and no security issue visible in the diff.
No security action required. Treat as a normal API/internal refactor. If reviewing the broader no-alloc encoding work, verify that callers of `try_push` handle the returned error rather than unwrapping.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch extends the Buffer trait (gated behind the alloc feature) with an associated Err type and a try_push method. For Vec<u8>, try_push delegates to the infallible Vec::push and returns Result<(), Infallible>. For ArrayVec<u8, N>, it forwards to ArrayVec::try_push, propagating the existing overflow error. Existing push and slice methods are unchanged. The change is purely preparatory for future no-alloc encoding support.
Changed components
base58/src/lib.rsinternal Buffer traitBase58 encoding buffer abstractionInspect captured patch +15 / −0
diff --git a/base58/src/lib.rs b/base58/src/lib.rs
index c9697723..be0babea 100644
--- a/base58/src/lib.rs
+++ b/base58/src/lib.rs
@@ -36,6 +36,7 @@ pub mod error;
#[cfg(not(feature = "std"))]
pub use alloc::{string::String, vec::Vec};
#[cfg(feature = "alloc")]
+use core::convert::Infallible;
use core::fmt;
#[cfg(feature = "std")]
pub use std::{string::String, vec::Vec};
@@ -221,15 +222,25 @@ const fn encoded_check_reserve_len(unencoded_len: usize) -> usize {
#[cfg(feature = "alloc")]
trait Buffer: Sized {
+ type Err: fmt::Debug;
+
fn push(&mut self, val: u8);
+ fn try_push(&mut self, val: u8) -> Result<(), Self::Err>;
fn slice(&self) -> &[u8];
fn slice_mut(&mut self) -> &mut [u8];
}
#[cfg(feature = "alloc")]
impl Buffer for Vec<u8> {
+ type Err = Infallible;
+
fn push(&mut self, val: u8) { Self::push(self, val) }
+ fn try_push(&mut self, val: u8) -> Result<(), Self::Err> {
+ self.push(val);
+ Ok(())
+ }
+
fn slice(&self) -> &[u8] { self }
fn slice_mut(&mut self) -> &mut [u8] { self }
@@ -237,8 +248,12 @@ impl Buffer for Vec<u8> {
#[cfg(feature = "alloc")]
impl<const N: usize> Buffer for ArrayVec<u8, N> {
+ type Err = internals::array_vec::error::Error;
+
fn push(&mut self, val: u8) { Self::push(self, val) }
+ fn try_push(&mut self, val: u8) -> Result<(), Self::Err> { self.try_push(val) }
+
fn slice(&self) -> &[u8] { self.as_slice() }
fn slice_mut(&mut self) -> &mut [u8] { self.as_mut_slice() }
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.