Skip to main content

faiss_next/index/
impl_.rs

1use faiss_next_sys::{self, FaissIndex};
2
3use crate::error::{Error, Result};
4use crate::index::native::InnerPtr;
5use crate::index::traits::Index;
6
7pub struct IndexImpl {
8    inner: InnerPtr<FaissIndex>,
9}
10
11impl IndexImpl {
12    pub(crate) fn from_raw(ptr: *mut FaissIndex) -> Result<Self> {
13        Ok(Self {
14            inner: InnerPtr::new(ptr)?,
15        })
16    }
17
18    pub fn into_flat(self) -> Result<super::IndexFlat> {
19        unsafe {
20            let flat_ptr = faiss_next_sys::faiss_IndexFlat_cast(self.inner.as_ptr());
21            if flat_ptr.is_null() {
22                return Err(Error::invalid_cast(
23                    "IndexFlat",
24                    "index is not a flat index",
25                ));
26            }
27            std::mem::forget(self);
28            Ok(super::IndexFlat::from_raw(flat_ptr))
29        }
30    }
31}
32
33impl Index for IndexImpl {
34    fn inner_ptr(&self) -> *mut FaissIndex {
35        self.inner.as_ptr()
36    }
37}
38
39impl Drop for IndexImpl {
40    fn drop(&mut self) {
41        tracing::trace!("dropping IndexImpl");
42        unsafe {
43            faiss_next_sys::faiss_Index_free(self.inner.as_ptr());
44        }
45    }
46}
47
48unsafe impl Send for IndexImpl {}
49unsafe impl Sync for IndexImpl {}