Unverified Commit f2dda94f authored by Hero Bird's avatar Hero Bird Committed by GitHub

Add scale::Output to Wrap (no_std) + minor improvements (#381)

* [core] add scale::Output to Wrap (no_std) + minor improvements

* [core] turn asserts into debug_asserts

* [core] remove unused is_empty method
parent 662369fe
Pipeline #89398 passed with stages
in 7 minutes and 57 seconds
......@@ -79,6 +79,18 @@ pub struct Wrap<'a> {
len: usize,
}
impl Wrap<'_> {
/// Returns the capacity of the underlying buffer.
fn capacity(&self) -> usize {
self.buffer.len()
}
/// Returns the length of the underlying buffer.
fn len(&self) -> usize {
self.len
}
}
#[cfg(feature = "std")]
impl<'a> Write for Wrap<'a> {
fn write(&mut self, buf: &[u8]) -> IoResult<usize> {
......@@ -103,6 +115,7 @@ impl<'a> Accumulator for Wrap<'a> {
}
fn write(&mut self, bytes: &[u8]) {
debug_assert!(self.len() + bytes.len() <= self.capacity());
let len = self.len;
let bytes_len = bytes.len();
self.buffer[len..(len + bytes_len)].copy_from_slice(bytes);
......@@ -113,3 +126,16 @@ impl<'a> Accumulator for Wrap<'a> {
&self.buffer[..self.len]
}
}
#[cfg(not(feature = "std"))]
impl<'a> scale::Output for Wrap<'a> {
fn write(&mut self, bytes: &[u8]) {
<Self as Accumulator>::write(self, bytes)
}
fn push_byte(&mut self, byte: u8) {
debug_assert!(self.len() < self.capacity());
self.buffer[self.len] = byte;
self.len += 1;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment