Skip to content

Commit 0531706

Browse files
committed
Import/export functions instead of methods in hugr-core.
1 parent 5fd144e commit 0531706

File tree

7 files changed

+42
-66
lines changed

7 files changed

+42
-66
lines changed

hugr-core/src/envelope.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ fn decode_model(
248248
extension_registry: &ExtensionRegistry,
249249
format: EnvelopeFormat,
250250
) -> Result<Package, EnvelopeError> {
251-
use crate::Extension;
251+
use crate::{import::import_package, Extension};
252252
use hugr_model::v0::bumpalo::Bump;
253253

254254
if format.model_version() != Some(0) {
@@ -270,7 +270,7 @@ fn decode_model(
270270
}
271271
}
272272

273-
Ok(Package::from_model(&model_package, &extension_registry)?)
273+
Ok(import_package(&model_package, &extension_registry)?)
274274
}
275275

276276
/// Internal implementation of [`write_envelope`] to call with/without the zstd compression wrapper.
@@ -305,6 +305,8 @@ fn encode_model(
305305
) -> Result<(), EnvelopeError> {
306306
use hugr_model::v0::{binary::write_to_writer, bumpalo::Bump};
307307

308+
use crate::export::export_package;
309+
308310
if format.model_version() != Some(0) {
309311
return Err(EnvelopeError::FormatUnsupported {
310312
format,
@@ -313,7 +315,7 @@ fn encode_model(
313315
}
314316

315317
let bump = Bump::default();
316-
let model_package = package.to_model(&bump);
318+
let model_package = export_package(package, &bump);
317319
write_to_writer(&model_package, &mut writer)?;
318320

319321
if format.append_extensions() {

hugr-core/src/export.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::{
55
ops::{
66
constant::CustomSerialized, DataflowBlock, DataflowOpTrait, OpName, OpTrait, OpType, Value,
77
},
8+
package::Package,
89
std_extensions::{
910
arithmetic::{float_types::ConstF64, int_types::ConstInt},
1011
collections::array::ArrayValue,
@@ -27,8 +28,18 @@ use hugr_model::v0::{
2728
use petgraph::unionfind::UnionFind;
2829
use std::fmt::Write;
2930

31+
/// Export a [`Package`] to its representation in the model.
32+
pub fn export_package<'a>(package: &'a Package, bump: &'a Bump) -> table::Package<'a> {
33+
let modules = package
34+
.modules
35+
.iter()
36+
.map(|module| export_hugr(module, bump))
37+
.collect();
38+
table::Package { modules }
39+
}
40+
3041
/// Export a [`Hugr`] graph to its representation in the model.
31-
pub(crate) fn export_hugr<'a>(hugr: &'a Hugr, bump: &'a Bump) -> table::Module<'a> {
42+
pub fn export_hugr<'a>(hugr: &'a Hugr, bump: &'a Bump) -> table::Module<'a> {
3243
let mut ctx = Context::new(hugr, bump);
3344
ctx.export_root();
3445
ctx.module

hugr-core/src/hugr.rs

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,6 @@ use crate::ops::{OpTag, OpTrait};
3434
pub use crate::ops::{OpType, DEFAULT_OPTYPE};
3535
use crate::{Direction, Node};
3636

37-
#[cfg(feature = "model_unstable")]
38-
use crate::export::export_hugr;
39-
#[cfg(feature = "model_unstable")]
40-
use crate::import::{import_hugr, ImportError};
41-
#[cfg(feature = "model_unstable")]
42-
use hugr_model::v0 as model;
43-
4437
/// The Hugr data structure.
4538
#[derive(Clone, Debug, PartialEq)]
4639
pub struct Hugr {
@@ -263,21 +256,6 @@ impl Hugr {
263256
self.extensions = used_extensions;
264257
Ok(())
265258
}
266-
267-
/// Export this module to the model representation.
268-
#[cfg(feature = "model_unstable")]
269-
pub fn to_model<'a>(&'a self, bump: &'a model::bumpalo::Bump) -> model::table::Module<'a> {
270-
export_hugr(self, bump)
271-
}
272-
273-
/// Import a module from the model representation.
274-
#[cfg(feature = "model_unstable")]
275-
pub fn from_model<'a>(
276-
module: &'a model::table::Module<'a>,
277-
extensions: &ExtensionRegistry,
278-
) -> Result<Self, ImportError> {
279-
import_hugr(module, extensions)
280-
}
281259
}
282260

283261
/// Internal API for HUGRs, not intended for use by users.

hugr-core/src/import.rs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use crate::{
1414
ExitBlock, FuncDecl, FuncDefn, Input, LoadConstant, LoadFunction, Module, OpType, OpaqueOp,
1515
Output, Tag, TailLoop, Value, CFG, DFG,
1616
},
17+
package::Package,
1718
std_extensions::{
1819
arithmetic::{float_types::ConstF64, int_types::ConstInt},
1920
collections::array::ArrayValue,
@@ -79,8 +80,24 @@ macro_rules! error_uninferred {
7980
($($e:expr),*) => { ImportError::Uninferred(format!($($e),*)) }
8081
}
8182

82-
/// Import a `hugr` module from its model representation.
83-
pub(crate) fn import_hugr(
83+
/// Import a [`Package`] from its model representation.
84+
pub fn import_package(
85+
package: &table::Package,
86+
extensions: &ExtensionRegistry,
87+
) -> Result<Package, ImportError> {
88+
let modules = package
89+
.modules
90+
.iter()
91+
.map(|module| import_hugr(module, extensions))
92+
.collect::<Result<Vec<_>, _>>()?;
93+
94+
// This does not panic since the import already requires a module root.
95+
let package = Package::new(modules).expect("non-module root");
96+
Ok(package)
97+
}
98+
99+
/// Import a [`Hugr`] module from its model representation.
100+
pub fn import_hugr(
84101
module: &table::Module,
85102
extensions: &ExtensionRegistry,
86103
) -> Result<Hugr, ImportError> {

hugr-core/src/package.rs

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,6 @@ use crate::hugr::{ExtensionError, HugrView, ValidationError};
1414
use crate::ops::{FuncDefn, Module, NamedOp, OpTag, OpTrait, OpType};
1515
use crate::{Extension, Hugr};
1616

17-
#[cfg(feature = "model_unstable")]
18-
use crate::import::ImportError;
19-
#[cfg(feature = "model_unstable")]
20-
use hugr_model::v0 as model;
21-
2217
#[derive(Debug, Default, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
2318
/// Package of module HUGRs.
2419
pub struct Package {
@@ -313,34 +308,6 @@ impl Package {
313308
#[allow(deprecated)]
314309
self.to_json_writer(writer)
315310
}
316-
317-
/// Export this package to the model representation.
318-
#[cfg(feature = "model_unstable")]
319-
pub fn to_model<'a>(&'a self, bump: &'a model::bumpalo::Bump) -> model::table::Package<'a> {
320-
let modules = self
321-
.modules
322-
.iter()
323-
.map(|module| module.to_model(bump))
324-
.collect();
325-
model::table::Package { modules }
326-
}
327-
328-
/// Import a package from the model representation.
329-
#[cfg(feature = "model_unstable")]
330-
pub fn from_model<'a>(
331-
package: &'a model::table::Package<'a>,
332-
extensions: &ExtensionRegistry,
333-
) -> Result<Self, ImportError> {
334-
let modules = package
335-
.modules
336-
.iter()
337-
.map(|module| Hugr::from_model(module, extensions))
338-
.collect::<Result<Vec<_>, _>>()?;
339-
340-
// This does not panic since the import already requires a module root.
341-
let package = Self::new(modules).expect("non-module root");
342-
Ok(package)
343-
}
344311
}
345312

346313
impl AsRef<[Hugr]> for Package {

hugr-core/tests/model.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22

33
use std::str::FromStr;
44

5-
use hugr::{package::Package, std_extensions::std_reg};
5+
use hugr::std_extensions::std_reg;
6+
use hugr_core::{export::export_package, import::import_package};
67
use hugr_model::v0 as model;
78

89
fn roundtrip(source: &str) -> String {
910
let bump = model::bumpalo::Bump::new();
1011
let package_ast = model::ast::Package::from_str(source).unwrap();
1112
let package_table = package_ast.resolve(&bump).unwrap();
12-
let core = Package::from_model(&package_table, &std_reg()).unwrap();
13-
let exported_table = core.to_model(&bump);
13+
let core = import_package(&package_table, &std_reg()).unwrap();
14+
let exported_table = export_package(&core, &bump);
1415
let exported_ast = exported_table.as_ast().unwrap();
1516
exported_ast.to_string()
1617
}

hugr/benches/benchmarks/hugr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ struct CapnpSer;
3131
impl Serializer for CapnpSer {
3232
fn serialize(&self, hugr: &Hugr) -> Vec<u8> {
3333
let bump = bumpalo::Bump::new();
34-
let module = hugr.to_model(&bump);
34+
let module = hugr_core::export::export_hugr(hugr, &bump);
3535
let package = hugr_model::v0::table::Package {
3636
modules: vec![module],
3737
};
@@ -41,7 +41,7 @@ impl Serializer for CapnpSer {
4141
fn deserialize(&self, bytes: &[u8]) -> Hugr {
4242
let bump = bumpalo::Bump::new();
4343
let package = hugr_model::v0::binary::read_from_slice(bytes, &bump).unwrap();
44-
Hugr::from_model(&package.modules[0], &STD_REG).unwrap()
44+
hugr_core::import::import_hugr(&package.modules[0], &STD_REG).unwrap()
4545
}
4646
}
4747

0 commit comments

Comments
 (0)