Skip to content

Commit

Permalink
remove io
Browse files Browse the repository at this point in the history
  • Loading branch information
tjjfvi committed May 27, 2024
1 parent cc493ee commit e3f7661
Show file tree
Hide file tree
Showing 24 changed files with 92 additions and 480 deletions.
4 changes: 0 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ debug = "full"
clap = { version = "4.5.4", features = ["derive"] }
libloading = { version = "0.8.3", default-features = false }
ordered-float = { version = "4.2.0" }
parking_lot = "0.12.2"
thiserror = "1.0.58"

hvm64-ast = { path = "./ast" }
Expand Down
2 changes: 0 additions & 2 deletions host/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ edition = "2021"
path = "src/host.rs"

[dependencies]
parking_lot = "0.12.2"

hvm64-ast = { path = "../ast", default-features = false }
hvm64-util = { path = "../util", default-features = false }
hvm64-runtime = { path = "../runtime", default-features = false }
Expand Down
37 changes: 6 additions & 31 deletions host/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ include!("../../prelude.rs");

use crate::prelude::*;
use hvm64_ast::{Book, Tree};
use hvm64_runtime::{Addr, Def, InterpretedDef, LabSet, Port, Tag, Wire};

use core::ops::{Deref, DerefMut};

pub mod stdlib;
use hvm64_runtime::{Addr, Def, DynDef, InterpretedDef, LabSet, Port, Tag, Wire};

mod calc_labels;
mod encode;
Expand All @@ -23,29 +19,11 @@ use calc_labels::calculate_label_sets;
#[derive(Default)]
pub struct Host {
/// the forward mapping, from a name to the runtime def
pub defs: Map<String, DefRef>,
pub defs: Map<String, Box<DynDef>>,
/// the backward mapping, from the address of a runtime def to the name
pub back: Map<Addr, String>,
}

/// A potentially-owned reference to a [`Def`]. Vitally, the address of the
/// `Def` is stable, even if the `DefRef` moves -- this is why
/// [`std::Borrow::Cow`] cannot be used here.
pub enum DefRef {
Owned(Box<dyn DerefMut<Target = Def> + Send + Sync>),
Static(&'static Def),
}

impl Deref for DefRef {
type Target = Def;
fn deref(&self) -> &Def {
match self {
DefRef::Owned(x) => x,
DefRef::Static(x) => x,
}
}
}

impl Host {
pub fn new(book: &Book) -> Host {
let mut host = Host::default();
Expand All @@ -63,7 +41,7 @@ impl Host {
/// Like `insert_book`, but allows specifying a function (`default_def`) that
/// will be run when the name of a definition is not found in the book.
/// The return value of the function will be inserted into the host.
pub fn insert_book_with_default(&mut self, book: &Book, default_def: &mut dyn FnMut(&str) -> DefRef) {
pub fn insert_book_with_default(&mut self, book: &Book, default_def: &mut dyn FnMut(&str) -> Box<DynDef>) {
#[cfg(feature = "std")]
{
self.defs.reserve(book.len());
Expand All @@ -85,7 +63,7 @@ impl Host {
})
.into_iter()
{
let def = DefRef::Owned(Box::new(Def::new(labs, InterpretedDef::default())));
let def = Box::new(Def::new(labs, InterpretedDef::default()));
self.insert_def(name, def);
}

Expand All @@ -98,16 +76,13 @@ impl Host {
}

/// Inserts a singular def into the mapping.
pub fn insert_def(&mut self, name: &str, def: DefRef) {
pub fn insert_def(&mut self, name: &str, def: Box<DynDef>) {
self.back.insert(Port::new_ref(&def).addr(), name.to_owned());
self.defs.insert(name.to_owned(), def);
}

/// Returns a mutable [`Def`] named `name`.
pub fn get_mut<T: Send + Sync + 'static>(&mut self, name: &str) -> &mut Def<T> {
match self.defs.get_mut(name).unwrap() {
DefRef::Owned(def) => def.downcast_mut().unwrap(),
DefRef::Static(_) => unreachable!(),
}
self.defs.get_mut(name).unwrap().downcast_mut().unwrap()
}
}
Loading

0 comments on commit e3f7661

Please sign in to comment.