Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TryClone trait #44

Merged
merged 2 commits into from
Dec 19, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/index/flat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,16 @@ impl TryFromInnerPtr for FlatIndexImpl {

impl_native_index!(FlatIndex);

impl_native_index_clone!(FlatIndex);
impl TryClone for FlatIndexImpl {}

impl_concurrent_index!(FlatIndexImpl);

#[cfg(test)]
mod tests {
use super::FlatIndexImpl;
use crate::index::{
index_factory, ConcurrentIndex, FromInnerPtr, Idx, Index, NativeIndex, UpcastIndex,
index_factory, ConcurrentIndex, FromInnerPtr, Idx, Index, NativeIndex, TryClone,
UpcastIndex,
};
use crate::metric::MetricType;

Expand Down
2 changes: 1 addition & 1 deletion src/index/ivf_flat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ impl FromInnerPtr for IVFFlatIndexImpl {

impl_native_index!(IVFFlatIndex);

impl_native_index_clone!(IVFFlatIndex);
impl TryClone for IVFFlatIndexImpl {}

impl_concurrent_index!(IVFFlatIndexImpl);

Expand Down
4 changes: 2 additions & 2 deletions src/index/lsh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use super::{
AssignSearchResult, CpuIndex, FromInnerPtr, Idx, Index, IndexImpl, NativeIndex,
RangeSearchResult, SearchResult, TryFromInnerPtr,
RangeSearchResult, SearchResult, TryClone, TryFromInnerPtr,
};
use crate::error::{Error, Result};
use crate::faiss_try;
Expand Down Expand Up @@ -110,7 +110,7 @@ impl LshIndex {

impl_native_index!(LshIndex);

impl_native_index_clone!(LshIndex);
impl TryClone for LshIndex {}

impl IndexImpl {
/// Attempt a dynamic cast of an index to the LSH index type.
Expand Down
26 changes: 23 additions & 3 deletions src/index/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,26 @@ pub trait TryFromInnerPtr: NativeIndex {
Self: Sized;
}

/// A macro which provides a Clone implementation to native index types.
ava57r marked this conversation as resolved.
Show resolved Hide resolved
pub trait TryClone: FromInnerPtr {
Enet4 marked this conversation as resolved.
Show resolved Hide resolved
/// Create an independent clone of this index.
///
/// # Errors
///
/// May result in a native error if the clone operation is not
/// supported for the internal type of index.
fn try_clone(&self) -> Result<Self>
where
Self: Sized,
{
unsafe {
let mut new_index_ptr = ::std::ptr::null_mut();
faiss_try(faiss_clone_index(self.inner_ptr(), &mut new_index_ptr))?;
Ok(crate::index::FromInnerPtr::from_inner_ptr(new_index_ptr))
}
}
}

/// The outcome of an index assign operation.
#[derive(Debug, Clone, PartialEq)]
pub struct AssignSearchResult {
Expand Down Expand Up @@ -468,7 +488,7 @@ impl TryFromInnerPtr for IndexImpl {
}

/// Index upcast trait.
///
///
/// If you need to store several different types of indexes in one collection,
/// you can cast all indexes to the common type `IndexImpl`.
/// # Examples
Expand Down Expand Up @@ -499,7 +519,7 @@ impl<NI: NativeIndex> UpcastIndex for NI {

impl_native_index!(IndexImpl);

impl_native_index_clone!(IndexImpl);
impl TryClone for IndexImpl {}

/// Use the index factory to create a native instance of a Faiss index, for `d`-dimensional
/// vectors. `description` should follow the exact guidelines as the native Faiss interface
Expand Down Expand Up @@ -530,7 +550,7 @@ where

#[cfg(test)]
mod tests {
use super::{index_factory, Idx, Index};
use super::{index_factory, Idx, Index, TryClone};
use crate::metric::MetricType;

#[test]
Expand Down
16 changes: 1 addition & 15 deletions src/index/pretransform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,21 +218,7 @@ impl<I> Index for PreTransformIndexImpl<I> {
}
}

impl<I> PreTransformIndexImpl<I> {
/// Create an independent clone of this index.
///
/// # Errors
///
/// May result in a native error if the clone operation is not
/// supported for the internal type of index.
pub fn try_clone(&self) -> Result<Self> {
unsafe {
let mut new_index_ptr = ::std::ptr::null_mut();
faiss_try(faiss_clone_index(self.inner_ptr(), &mut new_index_ptr))?;
Ok(crate::index::FromInnerPtr::from_inner_ptr(new_index_ptr))
}
}
}
impl<I: TryClone> TryClone for PreTransformIndexImpl<I> {}

impl<I> ConcurrentIndex for PreTransformIndexImpl<I>
where
Expand Down
16 changes: 1 addition & 15 deletions src/index/refine_flat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,21 +200,7 @@ impl<BI> Index for RefineFlatIndexImpl<BI> {
}
}

impl<BI> RefineFlatIndexImpl<BI> {
/// Create an independent clone of this index.
///
/// # Errors
///
/// May result in a native error if the clone operation is not
/// supported for the internal type of index.
pub fn try_clone(&self) -> Result<Self> {
unsafe {
let mut new_index_ptr = ::std::ptr::null_mut();
faiss_try(faiss_clone_index(self.inner_ptr(), &mut new_index_ptr))?;
Ok(crate::index::FromInnerPtr::from_inner_ptr(new_index_ptr))
}
}
}
impl<BI: TryClone> TryClone for RefineFlatIndexImpl<BI> {}

impl<BI> ConcurrentIndex for RefineFlatIndexImpl<BI>
where
Expand Down
18 changes: 2 additions & 16 deletions src/index/scalar_quantizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl FromInnerPtr for ScalarQuantizerIndexImpl {

impl_native_index!(ScalarQuantizerIndexImpl);

impl_native_index_clone!(ScalarQuantizerIndexImpl);
impl TryClone for ScalarQuantizerIndexImpl {}

impl IndexImpl {
/// Attempt a dynamic cast of an index to the Scalar Quantizer index type.
Expand Down Expand Up @@ -403,21 +403,7 @@ impl<Q> Index for IVFScalarQuantizerIndexImpl<Q> {
}
}

impl<Q> IVFScalarQuantizerIndexImpl<Q> {
/// Create an independent clone of this index.
///
/// # Errors
///
/// May result in a native error if the clone operation is not
/// supported for the internal type of index.
pub fn try_clone(&self) -> Result<Self> {
unsafe {
let mut new_index_ptr = ::std::ptr::null_mut();
faiss_try(faiss_clone_index(self.inner_ptr(), &mut new_index_ptr))?;
Ok(crate::index::FromInnerPtr::from_inner_ptr(new_index_ptr))
}
}
}
impl<Q: TryClone> TryClone for IVFScalarQuantizerIndexImpl<Q> {}

impl<Q> ConcurrentIndex for IVFScalarQuantizerIndexImpl<Q>
where
Expand Down
21 changes: 0 additions & 21 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,27 +176,6 @@ macro_rules! impl_native_index {
};
}

/// A macro which provides a Clone implementation to native index types.
macro_rules! impl_native_index_clone {
($t:ty) => {
impl $t {
/// Create an independent clone of this index.
///
/// # Errors
///
/// May result in a native error if the clone operation is not
/// supported for the internal type of index.
pub fn try_clone(&self) -> Result<Self> {
unsafe {
let mut new_index_ptr = ::std::ptr::null_mut();
faiss_try(faiss_clone_index(self.inner_ptr(), &mut new_index_ptr))?;
Ok(crate::index::FromInnerPtr::from_inner_ptr(new_index_ptr))
}
}
}
};
}

/// A macro which provides a concurrent index implementation to the given type.
macro_rules! impl_concurrent_index {
($t:ty) => {
Expand Down