use crate::internal_prelude::*;
use sbor::*;
#[derive(Debug, Clone, Eq, PartialEq)]
#[deprecated = "For new models, prefer SummarizedRawValueBody, which allows the hash to be computed both when it's a full value, OR when it's only a child (e.g. in a vec)"]
pub struct SummarizedRawFullValue<T: ManifestDecode> {
pub inner: T,
pub summary: Summary,
}
impl_has_summary!(<T: ManifestDecode> SummarizedRawFullValue<T>);
#[allow(deprecated)]
impl<T: ManifestDecode> TransactionPreparableFromValue for SummarizedRawFullValue<T> {
fn prepare_from_value(decoder: &mut TransactionDecoder) -> Result<Self, PrepareError> {
let start_offset = decoder.get_offset();
let inner = decoder.decode::<T>()?;
let end_offset = decoder.get_offset();
let summary = Summary {
effective_length: end_offset - start_offset,
total_bytes_hashed: end_offset - start_offset,
hash: hash(decoder.get_slice_with_valid_bounds(start_offset, end_offset)),
};
Ok(Self { inner, summary })
}
}
#[derive(Debug, Clone, Eq, PartialEq)]
#[deprecated = "For new models, prefer SummarizedRawValueBodyWithReferences, which allows the hash to be computed both when it's a full value, OR when it's only a child (e.g. in a vec)"]
pub struct SummarizedRawFullValueWithReferences<T: ManifestDecode> {
pub inner: T,
pub summary: Summary,
pub references: IndexSet<Reference>,
}
#[allow(deprecated)]
impl<T: ManifestDecode> HasSummary for SummarizedRawFullValueWithReferences<T> {
fn get_summary(&self) -> &Summary {
&self.summary
}
fn summary_mut(&mut self) -> &mut Summary {
&mut self.summary
}
}
#[allow(deprecated)]
impl<T: ManifestDecode> TransactionPreparableFromValue for SummarizedRawFullValueWithReferences<T> {
fn prepare_from_value(decoder: &mut TransactionDecoder) -> Result<Self, PrepareError> {
let start_offset = decoder.get_offset();
let inner = decoder.decode::<T>()?;
let end_offset = decoder.get_offset();
let slice = decoder.get_slice_with_valid_bounds(start_offset, end_offset);
let references = extract_references(slice, traversal::ExpectedStart::Value);
let summary = Summary {
effective_length: end_offset - start_offset,
total_bytes_hashed: end_offset - start_offset,
hash: hash(slice),
};
Ok(Self {
inner,
summary,
references,
})
}
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct SummarizedRawValueBodyWithReferences<T: ManifestDecode + ManifestCategorize> {
pub inner: T,
pub summary: Summary,
pub references: IndexSet<Reference>,
}
impl_has_summary!(<T: ManifestDecode + ManifestCategorize> SummarizedRawValueBodyWithReferences<T>);
impl<T: ManifestDecode + ManifestCategorize> TransactionPreparableFromValueBody
for SummarizedRawValueBodyWithReferences<T>
{
fn prepare_from_value_body(decoder: &mut TransactionDecoder) -> Result<Self, PrepareError> {
let start_offset = decoder.get_offset();
let inner = decoder.decode_deeper_body_with_value_kind::<T>(T::value_kind())?;
let end_offset = decoder.get_offset();
let slice = decoder.get_slice_with_valid_bounds(start_offset, end_offset);
let references =
extract_references(slice, traversal::ExpectedStart::ValueBody(T::value_kind()));
let summary = Summary {
effective_length: end_offset - start_offset,
total_bytes_hashed: end_offset - start_offset,
hash: hash(slice),
};
Ok(Self {
inner,
references,
summary,
})
}
fn value_kind() -> ManifestValueKind {
T::value_kind()
}
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct SummarizedRawValueBody<T: ManifestDecode + ManifestCategorize> {
pub inner: T,
pub summary: Summary,
}
impl_has_summary!(<T: ManifestDecode + ManifestCategorize> SummarizedRawValueBody<T>);
impl<T: ManifestDecode + ManifestCategorize> TransactionPreparableFromValueBody
for SummarizedRawValueBody<T>
{
fn prepare_from_value_body(decoder: &mut TransactionDecoder) -> Result<Self, PrepareError> {
let start_offset = decoder.get_offset();
let inner = decoder.decode_deeper_body_with_value_kind::<T>(T::value_kind())?;
let end_offset = decoder.get_offset();
let summary = Summary {
effective_length: end_offset - start_offset,
total_bytes_hashed: end_offset - start_offset,
hash: hash(decoder.get_slice_with_valid_bounds(start_offset, end_offset)),
};
Ok(Self { inner, summary })
}
fn value_kind() -> ManifestValueKind {
T::value_kind()
}
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct SummarizedRawValueBodyRawBytes {
pub inner: Vec<u8>,
pub summary: Summary,
}
impl_has_summary!(SummarizedRawValueBodyRawBytes);
impl TransactionPreparableFromValueBody for SummarizedRawValueBodyRawBytes {
fn prepare_from_value_body(decoder: &mut TransactionDecoder) -> Result<Self, PrepareError> {
let inner = decoder.decode_deeper_body_with_value_kind::<Vec<u8>>(Self::value_kind())?;
let effective_length = 2usize;
let summary = Summary {
effective_length: effective_length
.checked_add(inner.len())
.ok_or(PrepareError::LengthOverflow)?,
total_bytes_hashed: inner.len(),
hash: hash(&inner),
};
Ok(Self { inner, summary })
}
fn value_kind() -> ManifestValueKind {
Vec::<u8>::value_kind()
}
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct RawHash {
pub hash: Hash,
pub summary: Summary,
}
impl_has_summary!(RawHash);
impl TransactionPreparableFromValueBody for RawHash {
fn prepare_from_value_body(decoder: &mut TransactionDecoder) -> Result<Self, PrepareError> {
let start_offset = decoder.get_offset();
let hash = decoder.decode_deeper_body_with_value_kind::<Hash>(Self::value_kind())?;
let end_offset = decoder.get_offset();
let summary = Summary {
effective_length: end_offset - start_offset,
total_bytes_hashed: 0,
hash,
};
Ok(Self { hash, summary })
}
fn value_kind() -> ManifestValueKind {
ManifestValueKind::Array
}
}