Skip to content

Commit

Permalink
restore CoordTrait::nth_unchecked and mark it as unsafe.
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelkirk committed Oct 30, 2024
1 parent fc6e61a commit 8fa9776
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
2 changes: 1 addition & 1 deletion geo-traits/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
- <https://github.com/georust/geo/pull/1242>

## 0.1.1
Expand Down
25 changes: 24 additions & 1 deletion geo-traits/src/coord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self::T> {
if n < self.dim().size() {
Some(self.nth_or_panic(n))
Expand All @@ -40,6 +46,23 @@ 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.
///
/// # Safety
///
/// Though it may panic, the default implementation actually is safe. However, implementors
/// are allowed to implement this method themselves with an unsafe implementation. See the
/// individual implementations for more information on their own Safety considerations.
unsafe fn nth_unchecked(&self, n: usize) -> Self::T {
self.nth_or_panic(n)
}
}

impl<T: CoordNum> CoordTrait for Coord<T> {
Expand Down

0 comments on commit 8fa9776

Please sign in to comment.