Skip to content

Commit

Permalink
small steps
Browse files Browse the repository at this point in the history
  • Loading branch information
doug-q committed Jan 17, 2025
1 parent 86e7327 commit 0ac2e5f
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 29 deletions.
4 changes: 2 additions & 2 deletions hugr-model/src/v0/binary/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
mod read;
mod write;

pub use read::{read_from_reader, read_from_slice, ReadError};
pub use write::{write_to_vec, write_to_writer, WriteError};
pub use read::{read_from_slice, ReadError};
pub use write::write_to_vec;
9 changes: 1 addition & 8 deletions hugr-model/src/v0/binary/read.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::io::BufRead;

use crate::hugr_v0_capnp as hugr_capnp;
use crate::v0 as model;
use bumpalo::collections::Vec as BumpVec;
Expand All @@ -18,13 +16,8 @@ type ReadResult<T> = Result<T, ReadError>;

/// Read a hugr module from a byte slice.
pub fn read_from_slice<'a>(slice: &[u8], bump: &'a Bump) -> ReadResult<model::Module<'a>> {
read_from_reader(slice, bump)
}

/// Read a hugr module from an impl of [BufRead].
pub fn read_from_reader(reader: impl BufRead, bump: &Bump) -> ReadResult<model::Module<'_>> {
let reader =
capnp::serialize_packed::read_message(reader, capnp::message::ReaderOptions::new())?;
capnp::serialize_packed::read_message(slice, capnp::message::ReaderOptions::new())?;
let root = reader.get_root::<hugr_capnp::module::Reader>()?;
read_module(bump, root)
}
Expand Down
19 changes: 0 additions & 19 deletions hugr-model/src/v0/binary/write.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
use std::io::Write;

use crate::hugr_v0_capnp as hugr_capnp;
use crate::v0 as model;

/// An error encounter while serializing a model.
#[derive(Debug, derive_more::From, derive_more::Display, derive_more::Error)]
#[non_exhaustive]
pub enum WriteError {
/// An error encountered while encoding a `capnproto` buffer.
EncodingError(capnp::Error),
}

/// Write a list of items into a list builder.
macro_rules! write_list {
($builder:expr, $init:ident, $write:expr, $list:expr) => {
Expand All @@ -21,15 +11,6 @@ macro_rules! write_list {
};
}

/// Writes a module to an impl of [Write].
pub fn write_to_writer(module: &model::Module, writer: impl Write) -> Result<(), WriteError> {
let mut message = capnp::message::Builder::new_default();
let builder = message.init_root();
write_module(builder, module);

Ok(capnp::serialize_packed::write_message(writer, &message)?)
}

/// Writes a module to a byte vector.
pub fn write_to_vec(module: &model::Module) -> Vec<u8> {
let mut message = capnp::message::Builder::new_default();
Expand Down

0 comments on commit 0ac2e5f

Please sign in to comment.