faiss_next/index/
pre_transform.rs1use std::ptr;
2
3use faiss_next_sys::{self, FaissIndex, FaissIndexPreTransform};
4
5use crate::error::{check_return_code, Error, Result};
6use crate::index::native::InnerPtr;
7use crate::index::traits::Index;
8
9pub struct IndexPreTransform {
10 inner: InnerPtr<FaissIndexPreTransform>,
11}
12
13impl IndexPreTransform {
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_IndexPreTransform_new_with(
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 pt_ptr = faiss_next_sys::faiss_IndexPreTransform_cast(index.inner_ptr());
31 if pt_ptr.is_null() {
32 return Err(Error::invalid_cast(
33 "IndexPreTransform",
34 "index is not a PreTransform index",
35 ));
36 }
37 std::mem::forget(index);
38 Ok(Self {
39 inner: InnerPtr::new(pt_ptr)?,
40 })
41 }
42 }
43
44 pub fn sub_index(&self) -> *mut FaissIndex {
45 unsafe { faiss_next_sys::faiss_IndexPreTransform_index(self.inner.as_ptr()) }
46 }
47}
48
49impl Index for IndexPreTransform {
50 fn inner_ptr(&self) -> *mut FaissIndex {
51 self.inner.as_ptr() as *mut FaissIndex
52 }
53}
54
55impl Drop for IndexPreTransform {
56 fn drop(&mut self) {
57 tracing::trace!("dropping IndexPreTransform");
58 unsafe {
59 faiss_next_sys::faiss_IndexPreTransform_free(self.inner.as_ptr());
60 }
61 }
62}
63
64unsafe impl Send for IndexPreTransform {}
65unsafe impl Sync for IndexPreTransform {}