diff --git a/src/database/db_core.rs b/src/database/db_core.rs index 395fbd8..aaf973b 100644 --- a/src/database/db_core.rs +++ b/src/database/db_core.rs @@ -120,13 +120,9 @@ impl Core { } // Check if the given space_id is referenced in the current core. - pub fn is_empty(&self, space_id: S) -> bool - where - S: Into, - { - let id = space_id.into(); + pub fn is_empty(&self, space_id: &str) -> bool { for s in &self.space_db { - if s.name() == &id { + if s.name() == space_id { return s.is_empty(); } } diff --git a/src/database/mod.rs b/src/database/mod.rs index fdad9c7..debd40f 100644 --- a/src/database/mod.rs +++ b/src/database/mod.rs @@ -94,15 +94,10 @@ impl DataBase { } // Check if the given space_id is referenced in the DB. - fn is_empty(&self, id: S) -> bool - where - S: Into, - { - let id = id.into(); - + fn is_empty(&self, id: &str) -> bool { for s in self.cores.keys() { let core: &Core = self.cores.find(s)[0]; - if !core.is_empty(id.clone()) { + if !core.is_empty(id) { return false; } } @@ -110,22 +105,19 @@ impl DataBase { true } - fn check_exactly_one<'t, T, S>(list: &[&'t T], name: S, value: S) -> Result<&'t T, String> - where - S: Into, - { + fn check_exactly_one<'t, T>(list: &[&'t T], name: &str, value: &str) -> Result<&'t T, String> { if list.len() > 1 { Err(format!( "Multiple {} registered under `{}`: {}", - name.into(), - value.into(), + name, + value, list.len() )) } else if list.is_empty() { Err(format!( "No {} registered under `{}`: {}", - name.into(), - value.into(), + name, + value, list.len() )) } else { @@ -133,17 +125,6 @@ impl DataBase { } } - pub fn space_id(&self, name: S) -> Result - where - S: Into, - { - let name = name.into(); - let r = self.reference_spaces.find(&name); - let s: &Space = Self::check_exactly_one(&r, "spaces", &name)?; - - Ok(s.name().clone()) - } - // Lookup a space within the reference spaces registered pub fn space_keys(&self) -> &Vec { self.reference_spaces.keys() @@ -151,13 +132,12 @@ impl DataBase { // Lookup a space within the reference spaces registered pub fn space(&self, name: &str) -> Result<&Space, String> { - let name = name.into(); - if &name == space::Space::universe().name() { + if name == space::Space::universe().name() { Ok(space::Space::universe()) } else { - let r = self.reference_spaces.find(&name); + let r = self.reference_spaces.find(&name.to_string()); - Self::check_exactly_one(&r, "spaces", &name) + Self::check_exactly_one(&r, "spaces", name) } } @@ -168,10 +148,9 @@ impl DataBase { // Lookup a dataset within the datasets registered pub fn core(&self, name: &str) -> Result<&Core, String> { - let name = name.into(); - let r = self.cores.find(&name); + let r = self.cores.find(&name.to_string()); - Self::check_exactly_one(&r, "cores", &name) + Self::check_exactly_one(&r, "cores", name) } } diff --git a/src/database/space/axis.rs b/src/database/space/axis.rs index b6a09e4..4ffa91c 100644 --- a/src/database/space/axis.rs +++ b/src/database/space/axis.rs @@ -9,9 +9,9 @@ pub enum NumberSet { R, } -impl From for NumberSet { - fn from(set: String) -> Self { - match set.as_str() { +impl From<&str> for NumberSet { + fn from(set: &str) -> Self { + match set { "N" => NumberSet::N, "Z" => NumberSet::Z, "Q" => NumberSet::Q, @@ -21,8 +21,8 @@ impl From for NumberSet { } } -impl From for String { - fn from(set: NumberSet) -> String { +impl From<&NumberSet> for String { + fn from(set: &NumberSet) -> String { let s = match set { NumberSet::N => "N", NumberSet::Z => "R", diff --git a/src/database/space/coordinate_system.rs b/src/database/space/coordinate_system.rs index 4fb2c5b..0b4e88b 100644 --- a/src/database/space/coordinate_system.rs +++ b/src/database/space/coordinate_system.rs @@ -25,13 +25,13 @@ impl CoordinateSystem { } } - pub fn axes(&self) -> Vec { + pub fn axes(&self) -> &Vec { match self { CoordinateSystem::Universe { .. } => { //FIXME: Generate a CoordinateSystem on the fly or store it as part of the Universe Space? unimplemented!() } - CoordinateSystem::AffineSystem { axes, .. } => axes.clone(), + CoordinateSystem::AffineSystem { axes, .. } => axes, } } diff --git a/src/database/space/mod.rs b/src/database/space/mod.rs index e0aa203..b3e44ac 100644 --- a/src/database/space/mod.rs +++ b/src/database/space/mod.rs @@ -59,7 +59,7 @@ impl Space { self.system.origin() } - pub fn axes(&self) -> Vec { + pub fn axes(&self) -> &Vec { self.system.axes() } diff --git a/src/database/space/position.rs b/src/database/space/position.rs index bad8725..e811915 100644 --- a/src/database/space/position.rs +++ b/src/database/space/position.rs @@ -384,14 +384,15 @@ impl From> for Position { impl From for Vec { fn from(position: Position) -> Self { - let point: Vec<&Coordinate> = (&position).into(); - - point.into_iter().map(|c| c.into()).collect() + (&position).into() } } + impl From<&Position> for Vec { - fn from(coordinates: &Position) -> Self { - coordinates.clone().into() + fn from(position: &Position) -> Self { + let point: Vec<&Coordinate> = position.into(); + + point.into_iter().map(|c| c.into()).collect() } } diff --git a/src/database/space/shape.rs b/src/database/space/shape.rs index 990103a..027a63d 100644 --- a/src/database/space/shape.rs +++ b/src/database/space/shape.rs @@ -167,7 +167,7 @@ impl Shape { // Initialise the current value let mut current = lower.clone(); - // Add the first Position to the results, as nxt will return the following one. + // Add the first Position to the results, as next will return the following one. results.push(current.clone()); while next(lower.dimensions(), &lower, higher, &mut current) { results.push(current.clone()) diff --git a/src/database/space_db.rs b/src/database/space_db.rs index 4f00020..9151654 100644 --- a/src/database/space_db.rs +++ b/src/database/space_db.rs @@ -60,8 +60,9 @@ impl SpaceDB { // Limit temporary values lifetimes { - // Sort by values, smaller to bigger. + // Sort by values, smaller to bigger. We clone in order leave as-is scales. let mut exps = scales.clone(); + // FIXME: This should be done using all the values, somehow exps.sort_unstable_by_key(|v| v[0]); let mut previous = 0u32; diff --git a/src/json/model.rs b/src/json/model.rs index e5dcd7a..35bb143 100644 --- a/src/json/model.rs +++ b/src/json/model.rs @@ -54,7 +54,7 @@ pub struct Properties { impl From<&space::Graduation> for Graduation { fn from(g: &space::Graduation) -> Self { Graduation { - set: g.set.clone().into(), + set: (&g.set).into(), minimum: g.minimum, maximum: g.maximum, steps: g.steps, @@ -69,7 +69,7 @@ impl From for space::Axis { space::Axis::new( &axis.measurement_unit, axis.unit_vector, - g.set.into(), + g.set.as_str().into(), g.minimum, g.maximum, g.steps, @@ -108,7 +108,7 @@ impl From<&space::Space> for Space { Space { name: space.name().clone(), - origin: space.origin().clone().into(), + origin: space.origin().into(), axes, } } @@ -196,7 +196,8 @@ pub fn build_index( space_set_objects.push(SpaceSetObject::new( &point.reference_space, - point.vertices[0].clone().into(), + // Use a reference to prevent an allocation + (&point.vertices[0]).into(), value.into(), )) } @@ -205,7 +206,7 @@ pub fn build_index( properties.append(&mut properties_hm.drain().map(|(_, v)| v).collect::>()); } - properties.sort_unstable_by_key(|p| p.id().clone()); + properties.sort_unstable_by(|a, b| a.id().cmp(b.id())); space_set_objects.iter_mut().for_each(|object| { let id = properties_ref[object.value().u64() as usize];