use crate::qmdb::any::{ operation::{update::sealed::Sealed, Update as UpdateTrait}, value::{FixedEncoding, ValueEncoding, VariableEncoding}, FixedValue, VariableValue, }; use bytes::{Buf, BufMut}; use commonware_codec::{ Encode as _, EncodeSize, Error as CodecError, FixedSize, Read, ReadExt as _, Write, }; use commonware_utils::{hex, Array}; use std::fmt; #[derive(Clone, PartialEq, Debug, Eq)] pub struct Update(pub K, pub V::Value); #[cfg(feature = "arbitrary")] impl arbitrary::Arbitrary<'_> for Update where K: for<'a> arbitrary::Arbitrary<'a>, V::Value: for<'a> arbitrary::Arbitrary<'a>, { fn arbitrary(u: &mut arbitrary::Unstructured<'_>) -> arbitrary::Result { Ok(Self(u.arbitrary()?, u.arbitrary()?)) } } impl Sealed for Update {} impl UpdateTrait for Update { fn key(&self) -> &K { &self.0 } fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "[key:{} value:{}]", self.0, hex(&self.1.encode())) } } impl FixedSize for Update> { const SIZE: usize = K::SIZE + V::SIZE; } impl Write for Update> { fn write(&self, buf: &mut impl BufMut) { self.0.write(buf); self.1.write(buf); } } impl Read for Update> { type Cfg = (); fn read_cfg(buf: &mut impl Buf, cfg: &Self::Cfg) -> Result { let key = K::read(buf)?; let value = V::read_cfg(buf, cfg)?; Ok(Self(key, value)) } } impl EncodeSize for Update> { fn encode_size(&self) -> usize { K::SIZE + self.1.encode_size() } } impl Write for Update> { fn write(&self, buf: &mut impl BufMut) { self.0.write(buf); self.1.write(buf); } } impl Read for Update> { type Cfg = ::Cfg; fn read_cfg(buf: &mut impl Buf, cfg: &Self::Cfg) -> Result { let key = K::read(buf)?; let value = V::read_cfg(buf, cfg)?; Ok(Self(key, value)) } }