From 20a6db534a197c78bf8b065801d01552458dc2ec Mon Sep 17 00:00:00 2001 From: Michael Kirk Date: Wed, 30 Oct 2024 12:08:20 -0700 Subject: [PATCH] restore CoordTrait::nth_unchecked and mark it as unsafe. --- geo-traits/CHANGES.md | 2 +- geo-traits/src/coord.rs | 19 ++++++++++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/geo-traits/CHANGES.md b/geo-traits/CHANGES.md index 418f2d5d3..eaf09980e 100644 --- a/geo-traits/CHANGES.md +++ b/geo-traits/CHANGES.md @@ -2,7 +2,7 @@ ## Unreleased -- BREAKING: Rename `CoordTrait::nth_unchecked` -> `CoordTrait::nth_or_panic` since it is not `unsafe`. +- BREAKING: Mark `CoordTrait::nth_unchecked` as `unsafe` and add `CoordTrait::nth_or_panic`. - ## 0.1.1 diff --git a/geo-traits/src/coord.rs b/geo-traits/src/coord.rs index 66a0227ba..573ddd4b8 100644 --- a/geo-traits/src/coord.rs +++ b/geo-traits/src/coord.rs @@ -16,7 +16,13 @@ pub trait CoordTrait { /// Access the n'th (0-based) element of the CoordinateTuple. /// Returns `None` if `n >= DIMENSION`. - /// See also [`nth_or_panic()`](Self::nth_or_panic). + /// + /// See also [`nth_or_panic()`](Self::nth_or_panic) and [`nth_unchecked()`](Self::nth_unchecked). + /// + /// # Panics + /// + /// This method may panic if [`dim()`](Self::dim) does not correspond to + /// the actual number of dimensions in this coordinate. fn nth(&self, n: usize) -> Option { if n < self.dim().size() { Some(self.nth_or_panic(n)) @@ -40,6 +46,17 @@ pub trait CoordTrait { /// May panic if n >= DIMENSION. /// See also [`nth()`](Self::nth). fn nth_or_panic(&self, n: usize) -> Self::T; + + /// Access the n'th (0-based) element of the CoordinateTuple. + /// May panic if n >= DIMENSION. + /// + /// See also [`nth()`](Self::nth), [`nth_or_panic()`](Self::nth_or_panic). + /// + /// You might want to override the default implementation of this method + /// if you can provide a more efficient implementation. + unsafe fn nth_unchecked(&self, n: usize) -> Self::T { + self.nth_or_panic(n) + } } impl CoordTrait for Coord {