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

Make IndexableNum sealed #24

Merged
merged 2 commits into from
Jan 15, 2024
Merged
Changes from all commits
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
30 changes: 29 additions & 1 deletion src/type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,23 @@ use std::fmt::Debug;

use num_traits::{Bounded, Num, NumCast, ToPrimitive};

/// A trait for types that can be used for indexed coordinates.
///
/// This trait is sealed and cannot be implemented for external types. This is because we want to
/// ensure FFI compatibility with other implementations, including the reference implementations in
/// JavaScript ([rtree](https://github.com/mourner/flatbush),
/// [kdtree](https://github.com/mourner/kdbush))
pub trait IndexableNum:
Num + NumCast + ToPrimitive + PartialOrd + Debug + Send + Sync + bytemuck::Pod + Bounded
private::Sealed
+ Num
+ NumCast
+ ToPrimitive
+ PartialOrd
+ Debug
+ Send
+ Sync
+ bytemuck::Pod
+ Bounded
{
/// The type index to match the array order of `ARRAY_TYPES` in flatbush JS
const TYPE_INDEX: u8;
Expand Down Expand Up @@ -50,3 +65,16 @@ impl IndexableNum for f64 {
const TYPE_INDEX: u8 = 8;
const BYTES_PER_ELEMENT: usize = 8;
}
// https://rust-lang.github.io/api-guidelines/future-proofing.html#sealed-traits-protect-against-downstream-implementations-c-sealed
mod private {
pub trait Sealed {}

impl Sealed for i8 {}
impl Sealed for u8 {}
impl Sealed for i16 {}
impl Sealed for u16 {}
impl Sealed for i32 {}
impl Sealed for u32 {}
impl Sealed for f32 {}
impl Sealed for f64 {}
}