Skip to main content

faiss_next/
io.rs

1use std::ffi::CString;
2use std::ptr;
3
4use crate::error::{check_return_code, Result};
5use crate::index::{BinaryIndex, Index, IndexImpl};
6
7pub fn write_index(index: &impl Index, path: &str) -> Result<()> {
8    let c_path = CString::new(path)?;
9    check_return_code(unsafe {
10        faiss_next_sys::faiss_write_index_fname(index.inner_ptr(), c_path.as_ptr())
11    })
12}
13
14pub fn read_index(path: &str) -> Result<IndexImpl> {
15    let c_path = CString::new(path)?;
16    let mut inner = ptr::null_mut();
17    check_return_code(unsafe {
18        faiss_next_sys::faiss_read_index_fname(c_path.as_ptr(), 0, &mut inner)
19    })?;
20    IndexImpl::from_raw(inner)
21}
22
23pub fn write_index_binary(index: &crate::index::IndexBinary, path: &str) -> Result<()> {
24    let c_path = CString::new(path)?;
25    check_return_code(unsafe {
26        faiss_next_sys::faiss_write_index_binary_fname(index.inner_ptr(), c_path.as_ptr())
27    })
28}
29
30pub fn read_index_binary(path: &str) -> Result<crate::index::IndexBinary> {
31    let c_path = CString::new(path)?;
32    let mut inner = ptr::null_mut();
33    check_return_code(unsafe {
34        faiss_next_sys::faiss_read_index_binary_fname(c_path.as_ptr(), 0, &mut inner)
35    })?;
36    crate::index::IndexBinary::from_raw(inner)
37}