subspace_core_primitives/
objects.rs#[cfg(not(feature = "std"))]
extern crate alloc;
use crate::hashes::Blake3Hash;
use crate::pieces::PieceIndex;
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
use core::default::Default;
use parity_scale_codec::{Decode, Encode};
use scale_info::TypeInfo;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
#[derive(Debug, Copy, Clone, PartialEq, Eq, Ord, PartialOrd, Hash, Encode, Decode, TypeInfo)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
pub struct BlockObject {
pub hash: Blake3Hash,
pub offset: u32,
}
#[derive(Debug, Clone, PartialEq, Eq, Ord, PartialOrd, Hash, Encode, Decode, TypeInfo)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
#[cfg_attr(feature = "serde", serde(rename_all_fields = "camelCase"))]
pub enum BlockObjectMapping {
#[codec(index = 0)]
V0 {
objects: Vec<BlockObject>,
},
}
impl Default for BlockObjectMapping {
fn default() -> Self {
Self::V0 {
objects: Vec::new(),
}
}
}
impl BlockObjectMapping {
#[inline]
pub fn from_objects(objects: impl IntoIterator<Item = BlockObject>) -> Self {
Self::V0 {
objects: objects.into_iter().collect(),
}
}
pub fn objects(&self) -> &[BlockObject] {
match self {
Self::V0 { objects, .. } => objects,
}
}
pub fn objects_mut(&mut self) -> &mut Vec<BlockObject> {
match self {
Self::V0 { objects, .. } => objects,
}
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Ord, PartialOrd, Hash, Encode, Decode, TypeInfo)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(
feature = "serde",
serde(from = "CompactGlobalObject", into = "CompactGlobalObject")
)]
pub struct GlobalObject {
pub hash: Blake3Hash,
pub piece_index: PieceIndex,
pub offset: u32,
}
impl From<CompactGlobalObject> for GlobalObject {
fn from(object: CompactGlobalObject) -> Self {
Self {
hash: object.0,
piece_index: object.1,
offset: object.2,
}
}
}
impl From<GlobalObject> for CompactGlobalObject {
fn from(object: GlobalObject) -> Self {
Self(object.hash, object.piece_index, object.offset)
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Ord, PartialOrd, Hash, Encode, Decode, TypeInfo)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct CompactGlobalObject(Blake3Hash, PieceIndex, u32);
#[derive(Debug, Clone, PartialEq, Eq, Ord, PartialOrd, Hash, Encode, Decode, TypeInfo)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
#[cfg_attr(feature = "serde", serde(rename_all_fields = "camelCase"))]
pub enum GlobalObjectMapping {
#[codec(index = 0)]
V0 {
objects: Vec<GlobalObject>,
},
}
impl Default for GlobalObjectMapping {
fn default() -> Self {
Self::V0 {
objects: Vec::new(),
}
}
}
impl GlobalObjectMapping {
#[inline]
pub fn from_objects(objects: impl IntoIterator<Item = GlobalObject>) -> Self {
Self::V0 {
objects: objects.into_iter().collect(),
}
}
pub fn objects(&self) -> &[GlobalObject] {
match self {
Self::V0 { objects, .. } => objects,
}
}
pub fn objects_mut(&mut self) -> &mut Vec<GlobalObject> {
match self {
Self::V0 { objects, .. } => objects,
}
}
}