Skip to content

Commit

Permalink
fix: remove implicit serialization of MastNodeId
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbinth committed Jul 21, 2024
1 parent ba78af3 commit 9e59aec
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 19 deletions.
24 changes: 10 additions & 14 deletions core/src/mast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub use node::{
BasicBlockNode, CallNode, DynNode, ExternalNode, JoinNode, LoopNode, MastNode, OpBatch,
OperationOrDecorator, SplitNode, OP_BATCH_SIZE, OP_GROUP_SIZE,
};
use winter_utils::{ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable};
use winter_utils::DeserializationError;

mod serialization;

Expand Down Expand Up @@ -152,25 +152,21 @@ impl MastNodeId {
}
}

impl fmt::Display for MastNodeId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "MastNodeId({})", self.0)
impl From<MastNodeId> for u32 {
fn from(value: MastNodeId) -> Self {
value.0
}
}

impl Serializable for MastNodeId {
fn write_into<W: ByteWriter>(&self, target: &mut W) {
self.0.write_into(target)
impl From<&MastNodeId> for u32 {
fn from(value: &MastNodeId) -> Self {
value.0
}
}

impl Deserializable for MastNodeId {
fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
let inner = source.read_u32()?;

// TODO: fix

Ok(Self(inner))
impl fmt::Display for MastNodeId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "MastNodeId({})", self.0)
}
}

Expand Down
10 changes: 5 additions & 5 deletions core/src/mast/serialization/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ impl Serializable for MastForest {
target.write_usize(self.nodes.len());

// roots
self.roots.write_into(target);
let roots: Vec<u32> = self.roots.iter().map(u32::from).collect();
roots.write_into(target);

// Prepare MAST node infos, but don't store them yet. We store them at the end to make
// deserialization more efficient.
Expand Down Expand Up @@ -102,11 +103,8 @@ impl Deserializable for MastForest {
}

let node_count = source.read_usize()?;

let roots: Vec<MastNodeId> = Deserializable::read_from(source)?;

let roots: Vec<u32> = Deserializable::read_from(source)?;
let strings: Vec<DataOffset> = Deserializable::read_from(source)?;

let data: Vec<u8> = Deserializable::read_from(source)?;

let basic_block_data_decoder = BasicBlockDataDecoder::new(&data, &strings);
Expand All @@ -128,6 +126,8 @@ impl Deserializable for MastForest {
}

for root in roots {
// make sure the root is valid in the context of the MAST forest
let root = MastNodeId::from_u32_safe(root, &mast_forest)?;
mast_forest.make_root(root);
}

Expand Down

0 comments on commit 9e59aec

Please sign in to comment.