Skip to content

Commit

Permalink
Created Rust wrapper for C++ Exceptions, fixed segfault when Dimensio…
Browse files Browse the repository at this point in the history
…ns don't add up
  • Loading branch information
Julius Brummack committed May 19, 2024
1 parent 5ea48c8 commit 6b40c33
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions rust/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ pub enum BitAddressableError {
/// Error indicating the specified index is out of the allowable range.
IndexOutOfRange,
}
#[derive(Debug)]
pub enum USearchError {
CxxException(cxx::Exception),
WrongDimensionSize(usize, usize),
}

impl std::fmt::Display for BitAddressableError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
Expand Down Expand Up @@ -1072,8 +1077,15 @@ impl Index {
///
/// * `key` - The key associated with the vector.
/// * `vector` - A slice containing the vector data.
pub fn add<T: VectorType>(self: &Index, key: Key, vector: &[T]) -> Result<(), cxx::Exception> {
T::add(self, key, vector)
pub fn add<T: VectorType>(self: &Index, key: Key, vector: &[T]) -> Result<(), USearchError> {
if vector.len() == self.dimensions() {
T::add(self, key, vector).map_err(|cxx_error| USearchError::CxxException(cxx_error))
} else {
Err(USearchError::WrongDimensionSize(
vector.len(),
self.dimensions(),
))
}
}

/// Extracts one or more vectors matching the specified key.
Expand Down

0 comments on commit 6b40c33

Please sign in to comment.