From 90273ab87726330d2138f3758d0a4e8d8c479671 Mon Sep 17 00:00:00 2001 From: Alexander Andreev Date: Tue, 18 Oct 2022 15:11:35 +0300 Subject: [PATCH 1/2] IndexImpl to IdMapIndex --- src/index/id_map.rs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/index/id_map.rs b/src/index/id_map.rs index 8831efe..799ef2b 100644 --- a/src/index/id_map.rs +++ b/src/index/id_map.rs @@ -67,6 +67,8 @@ use std::mem; use std::os::raw::c_int; use std::ptr; +use super::IndexImpl; + /// Wrapper for implementing arbitrary ID mapping to an index. /// /// See the [module level documentation] for more information. @@ -364,6 +366,21 @@ where } } +impl IndexImpl { + pub fn into_id_map(self) -> Result> { + unsafe { + let new_inner = faiss_IndexIDMap_cast(self.inner_ptr()); + if new_inner.is_null() { + Err(Error::BadCast) + } else { + mem::forget(self); + let index_inner = faiss_IndexIDMap_sub_index(new_inner); + Ok(IdMap { inner: new_inner, index_inner, phantom: PhantomData }) + } + } + } +} + #[cfg(test)] mod tests { use super::IdMap; @@ -471,4 +488,12 @@ mod tests { let flat_index: FlatIndexImpl = id_index.try_into_inner().unwrap(); assert_eq!(flat_index.d(), 4); } + + #[test] + fn index_impl_to_id_map() { + let index = index_factory(4, "IDMap,Flat", MetricType::L2).unwrap(); + let id_map = index.into_id_map().unwrap(); + + assert_eq!(id_map.d(), 4); + } } From e22ee4f934812e61573136d57e5cd640e8bbf896 Mon Sep 17 00:00:00 2001 From: Alexander A Date: Wed, 19 Oct 2022 10:56:46 +0300 Subject: [PATCH 2/2] Update src/index/id_map.rs Co-authored-by: Eduardo Pinho --- src/index/id_map.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/index/id_map.rs b/src/index/id_map.rs index 799ef2b..a55fe76 100644 --- a/src/index/id_map.rs +++ b/src/index/id_map.rs @@ -367,6 +367,9 @@ where } impl IndexImpl { + /// Attempt a dynamic cast of the index to one that is [ID-mapped][1]. + /// + /// [1]: crate::IdMap pub fn into_id_map(self) -> Result> { unsafe { let new_inner = faiss_IndexIDMap_cast(self.inner_ptr());