From 43c6db0b068f0a98c9fb22fbfbd2d4c39c9ccb87 Mon Sep 17 00:00:00 2001 From: Izumi Hoshino Date: Thu, 11 Apr 2024 00:18:09 +0900 Subject: [PATCH] Updated `LazyNode::to_bytes` --- src/serde/mod.rs | 4 ++-- wasm/src/lazy_node.rs | 17 ++++++++--------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/src/serde/mod.rs b/src/serde/mod.rs index cbf597b6..19b5e9d6 100644 --- a/src/serde/mod.rs +++ b/src/serde/mod.rs @@ -18,8 +18,8 @@ mod test; pub use de::node_from_bytes; pub use de_br::{node_from_bytes_backrefs, node_from_bytes_backrefs_record}; pub use de_tree::{parse_triples, ParsedTriple}; -pub use ser::node_to_bytes; -pub use ser_br::node_to_bytes_backrefs; +pub use ser::{node_to_bytes, node_to_bytes_limit}; +pub use ser_br::{node_to_bytes_backrefs, node_to_bytes_backrefs_limit}; pub use tools::{ serialized_length_from_bytes, serialized_length_from_bytes_trusted, tree_hash_from_stream, }; diff --git a/wasm/src/lazy_node.rs b/wasm/src/lazy_node.rs index 5c50968e..f1af930c 100644 --- a/wasm/src/lazy_node.rs +++ b/wasm/src/lazy_node.rs @@ -2,9 +2,8 @@ use js_sys::Array; use std::rc::Rc; use wasm_bindgen::prelude::*; -use crate::flags::ALLOW_BACKREFS; use clvmr::allocator::{Allocator, NodePtr, SExp}; -use clvmr::serde::{node_to_bytes, node_to_bytes_backrefs}; +use clvmr::serde::{node_to_bytes_limit, node_to_bytes_backrefs_limit}; #[wasm_bindgen] #[derive(Clone)] @@ -39,13 +38,13 @@ impl LazyNode { } #[wasm_bindgen] - pub fn to_bytes(&self, flag: u32) -> Option> { - let serializer = if (flag & ALLOW_BACKREFS) != 0 { - node_to_bytes_backrefs - } else { - node_to_bytes - }; - serializer(&self.allocator, self.node).ok() + pub fn to_bytes_with_backref(&self, limit: usize) -> Result, String> { + node_to_bytes_backrefs_limit(&self.allocator, self.node, limit).map_err(|e| e.to_string()) + } + + #[wasm_bindgen] + pub fn to_bytes(&self, limit: usize) -> Result, String> { + node_to_bytes_limit(&self.allocator, self.node, limit).map_err(|e| e.to_string()) } }