faiss_next/index/
id_map.rs1use std::ptr;
2
3use faiss_next_sys::{self, FaissIndex, FaissIndexIDMap};
4
5use crate::error::{check_return_code, Error, Result};
6use crate::index::native::InnerPtr;
7use crate::index::traits::Index;
8
9pub struct IndexIDMap {
10 inner: InnerPtr<FaissIndexIDMap>,
11}
12
13impl IndexIDMap {
14 pub fn new(index: super::IndexImpl) -> Result<Self> {
15 unsafe {
16 let mut inner = ptr::null_mut();
17 check_return_code(faiss_next_sys::faiss_IndexIDMap_new(
18 &mut inner,
19 index.inner_ptr(),
20 ))?;
21 std::mem::forget(index);
22 Ok(Self {
23 inner: InnerPtr::new(inner)?,
24 })
25 }
26 }
27
28 pub fn from_index(index: super::IndexImpl) -> Result<Self> {
29 unsafe {
30 let idmap_ptr = faiss_next_sys::faiss_IndexIDMap_cast(index.inner_ptr());
31 if idmap_ptr.is_null() {
32 return Err(Error::invalid_cast(
33 "IndexIDMap",
34 "index is not an IndexIDMap",
35 ));
36 }
37 std::mem::forget(index);
38 Ok(Self {
39 inner: InnerPtr::new(idmap_ptr)?,
40 })
41 }
42 }
43
44 pub fn sub_index(&self) -> *mut FaissIndex {
45 unsafe { faiss_next_sys::faiss_IndexIDMap_sub_index(self.inner.as_ptr()) }
46 }
47}
48
49impl Index for IndexIDMap {
50 fn inner_ptr(&self) -> *mut FaissIndex {
51 self.inner.as_ptr() as *mut FaissIndex
52 }
53}
54
55impl Drop for IndexIDMap {
56 fn drop(&mut self) {
57 tracing::trace!("dropping IndexIDMap");
58 unsafe {
59 faiss_next_sys::faiss_Index_free(self.inner.as_ptr() as *mut FaissIndex);
60 }
61 }
62}
63
64unsafe impl Send for IndexIDMap {}
65unsafe impl Sync for IndexIDMap {}