Skip to content

Commit 0f3f5da

Browse files
committed
api: document a couple panicking preconditions
This brings the API documentation for `Span::offset` and `packed::Searcher::find_in` into consistency with `Input::span`. That is, if you try to set offsets that are incorrect for the provided haystack (or are pathologically invalid), then you get a panic. This mimics how, e.g., `&slice[range]` works. This addresses a [RUSTSEC advisory proposal] for these omissions from the docs. The proposed advisory misses the forest for the trees: if you go out of your way to use invalid offsets, then you're going to have a bad time. On top of that, offsets are rarely provided as inputs from users. That is, offsets are usually _trusted_ information, which is why it's reasonable to establish their correctness as a precondition. [RUSTSEC advisory proposal]: rustsec/advisory-db#2684
1 parent e88e1fc commit 0f3f5da

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

‎src/packed/api.rs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,10 @@ impl Searcher {
502502
/// `0`) in which it was added. The offsets in the `Match` will be relative
503503
/// to the start of `haystack` (and not `at`).
504504
///
505+
/// # Panics
506+
///
507+
/// When `span` does not correspond to a valid range in `haystack`.
508+
///
505509
/// # Example
506510
///
507511
/// Basic usage:

‎src/util/search.rs‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -709,9 +709,20 @@ impl Span {
709709

710710
/// Returns a new span with `offset` added to this span's `start` and `end`
711711
/// values.
712+
///
713+
/// # Panics
714+
///
715+
/// This panics if adding `offset` to either part of this `Span` would
716+
/// result in overflow.
712717
#[inline]
713718
pub fn offset(&self, offset: usize) -> Span {
714-
Span { start: self.start + offset, end: self.end + offset }
719+
Span {
720+
start: self
721+
.start
722+
.checked_add(offset)
723+
.expect("invalid start+offset"),
724+
end: self.end.checked_add(offset).expect("invalid end+offset"),
725+
}
715726
}
716727
}
717728

0 commit comments

Comments
 (0)