Enet4 / faiss-rs

Rust language bindings for Faiss
Apache License 2.0
200 stars 36 forks source link

Add Box impl #41

Closed ava57r closed 3 years ago

ava57r commented 3 years ago

For erase type of index.

Rust std. https://doc.rust-lang.org/beta/std/boxed/struct.Box.html#impl-Iterator

For code:

let mut v: Vec<Box<dyn faiss::Index>> = vec![];

let f1 = FlatIndexImpl::new();
v.push(f1);

let f2 = IndexImpl::new();
v.push(f2);
Enet4 commented 3 years ago

What if we also had a way to upcast a specific index type back to an IndexImpl?

let f1 = FlatIndex::new_l2(128);
let f2 = index_factory(128, "Flat", MetricType::L2);
let v: Vec<faiss::IndexImpl> = vec![
    f1.upcast(),
    f2,
];
ava57r commented 3 years ago

What if we also had a way to upcast a specific index type back to an IndexImpl?

let f1 = FlatIndex::new_l2(128);
let f2 = index_factory(128, "Flat", MetricType::L2);
let v: Vec<faiss::IndexImpl> = vec![
    f1.upcast(),
    f2,
];

I agree.

Box<dyn faiss::Index> is Rust idiomatic path.

ava57r commented 3 years ago

What if we also had a way to upcast a specific index type back to an IndexImpl?

let f1 = FlatIndex::new_l2(128);
let f2 = index_factory(128, "Flat", MetricType::L2);
let v: Vec<faiss::IndexImpl> = vec![
    f1.upcast(),
    f2,
];

Do upcast in C_API?

Enet4 commented 3 years ago

Do upcast in C_API?

I don't imagine this to be necessary. The operation is a reinterpret_cast between two index pointers, which is a no-op and can be done in Rust. Having functions for a downcast was important because then we could dynamic_cast, which checks at run-time that the target type is acceptable.