Skip to content

Commit

Permalink
rust: Trade Box for Sized
Browse files Browse the repository at this point in the history
  • Loading branch information
felixhekhorn committed Aug 19, 2024
1 parent 9b0e1df commit edd0915
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
8 changes: 5 additions & 3 deletions crates/dekoder/src/inventory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ const HEADER_EXT: &'static str = "*.yaml";
/// Header type in an inventory.
pub(crate) trait HeaderT {
/// Load from yaml.
fn load_from_yaml(yml: &Yaml) -> Result<Box<Self>>;
fn load_from_yaml(yml: &Yaml) -> Result<Self>
where
Self: Sized;
/// Comparator.
fn eq(&self, other: &Self, ulps: i64) -> bool;
}
Expand All @@ -32,7 +34,7 @@ pub(crate) struct Inventory<K: HeaderT> {
/// Working directory
pub(crate) path: PathBuf,
/// Available keys
pub(crate) keys: HashMap<OsString, Box<K>>,
pub(crate) keys: HashMap<OsString, K>,
}

impl<K: HeaderT> Inventory<K> {
Expand Down Expand Up @@ -62,7 +64,7 @@ impl<K: HeaderT> Inventory<K> {
}

/// List available keys.
pub fn keys(&self) -> Vec<&Box<K>> {
pub fn keys(&self) -> Vec<&K> {
let mut ks = Vec::new();
for k in self.keys.values() {
ks.push(k);
Expand Down
17 changes: 13 additions & 4 deletions crates/dekoder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ use yaml_rust2::Yaml;

mod inventory;

use crate::inventory::HeaderT;

/// The EKO errors.
#[derive(Error, Debug)]
pub enum EKOError {
Expand Down Expand Up @@ -43,7 +45,7 @@ pub struct EvolutionPoint {

impl inventory::HeaderT for EvolutionPoint {
/// Load from yaml.
fn load_from_yaml(yml: &Yaml) -> Result<Box<Self>> {
fn load_from_yaml(yml: &Yaml) -> Result<Self> {
// work around float representation
let scale = yml["scale"].as_f64();
let scale = if scale.is_some() {
Expand All @@ -58,15 +60,22 @@ impl inventory::HeaderT for EvolutionPoint {
let nf = yml["nf"]
.as_i64()
.ok_or(EKOError::KeyError("because failed to read nf".to_owned()))?;
Ok(Box::new(Self { scale, nf }))
Ok(Self { scale, nf })
}

/// Comparator.
/// (Protected) comparator.
fn eq(&self, other: &Self, ulps: i64) -> bool {
self.nf == other.nf && approx_eq!(f64, self.scale, other.scale, ulps = ulps)
}
}

impl EvolutionPoint {
/// Comparator.
pub fn equals(&self, other: &Self, ulps: i64) -> bool {
self.eq(other, ulps)
}
}

/// 4D evolution operator.
pub struct Operator {
pub op: Array4<f64>,
Expand Down Expand Up @@ -203,7 +212,7 @@ impl EKO {
}

/// List available evolution points.
pub fn available_operators(&self) -> Vec<&Box<EvolutionPoint>> {
pub fn available_operators(&self) -> Vec<&EvolutionPoint> {
self.operators.keys()
}

Expand Down
2 changes: 2 additions & 0 deletions crates/dekoder/tests/test_load.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ fn has_operator() {
scale: 10000.,
nf: 4,
};
// it is the one
assert!(ep.equals(eko.available_operators()[0], 64));
assert!(eko.has_operator(&ep, 64));
eko.close(false).unwrap();
}
Expand Down

0 comments on commit edd0915

Please sign in to comment.