From 85c7397cb7e96af0c4c07e6a790290276537c741 Mon Sep 17 00:00:00 2001 From: timvisee Date: Mon, 4 Dec 2023 14:40:19 +0100 Subject: [PATCH] Conversions from borrowed types into sparse vectors --- src/client.rs | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/client.rs b/src/client.rs index d700994..66c7147 100644 --- a/src/client.rs +++ b/src/client.rs @@ -228,11 +228,18 @@ impl From> for Vector { impl From> for Vector { fn from(tuples: Vec<(u32, f32)>) -> Self { + Self::from(tuples.as_slice()) + } +} + +// Since we construct two new Vec's anyway it's fine to source from a reference +impl From<&[(u32, f32)]> for Vector { + fn from(tuples: &[(u32, f32)]) -> Self { let mut indices = Vec::with_capacity(tuples.len()); let mut values = Vec::with_capacity(tuples.len()); for (i, w) in tuples { - indices.push(i); - values.push(w); + indices.push(*i); + values.push(*w); } Vector { data: values, @@ -277,6 +284,20 @@ impl From>> for Vectors { } } +// Since we construct two new Vec's anyway it's fine to source from a reference +impl From> for Vectors { + fn from(named_vectors: HashMap) -> Self { + Vectors { + vectors_options: Some(VectorsOptions::Vectors(NamedVectors { + vectors: named_vectors + .into_iter() + .map(|(k, v)| (k, v.into())) + .collect(), + })), + } + } +} + impl From> for Vectors { fn from(vector: Vec) -> Self { Vectors {