use alloc::string::String;
use alloc::vec::Vec;
use core::convert::Infallible;
pub trait Update {
type Error;
fn update(&mut self, chunk: impl AsRef<[u8]>) -> Result<(), Self::Error>;
fn chain(mut self, chunk: impl AsRef<[u8]>) -> Result<Self, Self::Error>
where
Self: Sized,
{
self.update(chunk)?;
Ok(self)
}
}
impl Update for Vec<u8> {
type Error = Infallible;
fn update(&mut self, chunk: impl AsRef<[u8]>) -> Result<(), Self::Error> {
self.extend(chunk.as_ref());
Ok(())
}
}
impl Update for String {
type Error = core::str::Utf8Error;
fn update(&mut self, chunk: impl AsRef<[u8]>) -> Result<(), Self::Error> {
self.push_str(core::str::from_utf8(chunk.as_ref())?);
Ok(())
}
}
impl<T: Update> Update for Vec<T> {
type Error = T::Error;
fn update(&mut self, chunk: impl AsRef<[u8]>) -> Result<(), Self::Error> {
for x in self.iter_mut() {
x.update(chunk.as_ref())?;
}
Ok(())
}
}
impl<T: crate::Zeroize + Update> Update for crate::Zeroizing<T> {
type Error = T::Error;
fn update(&mut self, chunk: impl AsRef<[u8]>) -> Result<(), Self::Error> {
(**self).update(chunk)
}
}