Skip to content

Commit 0e740ed

Browse files
committed
Fix: MetricFunction example
1 parent 6b022fd commit 0e740ed

1 file changed

Lines changed: 15 additions & 39 deletions

File tree

rust/lib.rs

Lines changed: 15 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -425,8 +425,7 @@ pub use ffi::{IndexOptions, MetricKind, ScalarKind};
425425
///
426426
/// - `B1X8Metric`: A metric function for binary vectors packed in `u8` containers, represented here by `b1x8`.
427427
/// - `I8Metric`: A metric function for vectors of 8-bit signed integers (`i8`).
428-
/// - `F16Metric`: A metric function for vectors of 16-bit floating-point numbers, using a custom `f16` type
429-
/// to represent half-precision floats.
428+
/// - `F16Metric`: A metric function for vectors of 16-bit half-precision floating-point numbers (`f16`).
430429
/// - `F32Metric`: A metric function for vectors of 32-bit floating-point numbers (`f32`).
431430
/// - `F64Metric`: A metric function for vectors of 64-bit floating-point numbers (`f64`).
432431
///
@@ -447,19 +446,17 @@ pub use ffi::{IndexOptions, MetricKind, ScalarKind};
447446
/// # Examples
448447
///
449448
/// ```
450-
/// use usearch::{MetricFunction, Distance, f16, b1x8};
449+
/// use usearch::{Distance, f16, b1x8};
451450
///
452-
/// // Example of defining a custom Euclidean distance function for f32 vectors
453-
/// let euclidean: MetricFunction = MetricFunction::F32Metric(Box::new(|a, b| {
454-
/// // Safety: Assume a and b are valid for the number of dimensions in context.
451+
/// let euclidean_fn = Box::new(|a: *const f32, b: *const f32| -> f32 {
455452
/// let dimensions = 256;
456453
/// let a = unsafe { std::slice::from_raw_parts(a, dimensions) };
457454
/// let b = unsafe { std::slice::from_raw_parts(b, dimensions) };
458455
/// a.iter().zip(b.iter())
459-
/// .map(|(a, b)| (a - b).powi(2))
456+
/// .map(|(a, b)| (*a - *b).powi(2))
460457
/// .sum::<f32>()
461458
/// .sqrt()
462-
/// }));
459+
/// });
463460
/// ```
464461
///
465462
/// In this example, `dimensions` should be defined and valid for the vectors `a` and `b`.
@@ -539,7 +536,6 @@ impl Drop for Index {
539536
}
540537
}
541538

542-
543539
impl Default for ffi::IndexOptions {
544540
fn default() -> Self {
545541
Self {
@@ -714,14 +710,10 @@ impl VectorType for f32 {
714710

715711
let trampoline_fn: usize = trampoline as *const () as usize;
716712
let closure_address = match index.metric_fn {
717-
Some(MetricFunction::F32Metric(metric)) => {
718-
metric as *mut () as usize
719-
}
713+
Some(MetricFunction::F32Metric(metric)) => metric as *mut () as usize,
720714
_ => panic!("Expected F32Metric"),
721715
};
722-
index
723-
.inner
724-
.change_metric(trampoline_fn, closure_address);
716+
index.inner.change_metric(trampoline_fn, closure_address);
725717

726718
Ok(())
727719
}
@@ -783,14 +775,10 @@ impl VectorType for i8 {
783775

784776
let trampoline_fn: usize = trampoline as *const () as usize;
785777
let closure_address = match index.metric_fn {
786-
Some(MetricFunction::I8Metric(metric)) => {
787-
metric as *mut () as usize
788-
}
778+
Some(MetricFunction::I8Metric(metric)) => metric as *mut () as usize,
789779
_ => panic!("Expected I8Metric"),
790780
};
791-
index
792-
.inner
793-
.change_metric(trampoline_fn, closure_address);
781+
index.inner.change_metric(trampoline_fn, closure_address);
794782

795783
Ok(())
796784
}
@@ -852,14 +840,10 @@ impl VectorType for f64 {
852840

853841
let trampoline_fn: usize = trampoline as *const () as usize;
854842
let closure_address = match index.metric_fn {
855-
Some(MetricFunction::F64Metric(metric)) => {
856-
metric as *mut () as usize
857-
}
843+
Some(MetricFunction::F64Metric(metric)) => metric as *mut () as usize,
858844
_ => panic!("Expected F64Metric"),
859845
};
860-
index
861-
.inner
862-
.change_metric(trampoline_fn, closure_address);
846+
index.inner.change_metric(trampoline_fn, closure_address);
863847

864848
Ok(())
865849
}
@@ -925,14 +909,10 @@ impl VectorType for f16 {
925909

926910
let trampoline_fn: usize = trampoline as *const () as usize;
927911
let closure_address = match index.metric_fn {
928-
Some(MetricFunction::F16Metric(metric)) => {
929-
metric as *mut () as usize
930-
}
912+
Some(MetricFunction::F16Metric(metric)) => metric as *mut () as usize,
931913
_ => panic!("Expected F16Metric"),
932914
};
933-
index
934-
.inner
935-
.change_metric(trampoline_fn, closure_address);
915+
index.inner.change_metric(trampoline_fn, closure_address);
936916

937917
Ok(())
938918
}
@@ -998,14 +978,10 @@ impl VectorType for b1x8 {
998978

999979
let trampoline_fn: usize = trampoline as *const () as usize;
1000980
let closure_address = match index.metric_fn {
1001-
Some(MetricFunction::B1X8Metric(metric)) => {
1002-
metric as *mut () as usize
1003-
}
981+
Some(MetricFunction::B1X8Metric(metric)) => metric as *mut () as usize,
1004982
_ => panic!("Expected F1X8Metric"),
1005983
};
1006-
index
1007-
.inner
1008-
.change_metric(trampoline_fn, closure_address);
984+
index.inner.change_metric(trampoline_fn, closure_address);
1009985

1010986
Ok(())
1011987
}

0 commit comments

Comments
 (0)