Skip to content

Commit 370bd60

Browse files
committed
Make Hay and Haystack unsafe.
1 parent 5f00210 commit 370bd60

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

text/0000-pattern-3.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ A `Hay` is the core type which the search algorithm will run on.
8686
It is implemented on the slice-like types like `str`, `OsStr` and `[T]`.
8787

8888
```rust
89-
pub trait Hay {
89+
pub unsafe trait Hay {
9090
type Index: Copy + Debug + Eq;
9191

9292
fn empty<'a>() -> &'a Self;
@@ -101,6 +101,9 @@ pub trait Hay {
101101
}
102102
```
103103

104+
The trait is unsafe to implement because it needs to guarantee all methods (esp. `.start_index()`
105+
and `.end_index()`) follow the documented requirements, which cannot be checked automatically.
106+
104107
We allow a hay to customize the `Index` type. While `str`, `[T]` and `OsStr` all use `usize` as
105108
the index, we do want the Pattern API to support other linear structures like `LinkedList<T>`,
106109
where a cursor/pointer would be more suitable for allowing sub-linear splitting.
@@ -128,7 +131,7 @@ Haystack is implemented on the reference or collection itself e.g. `&[T]`, `&mut
128131
A hay can *borrowed* from a haystack.
129132

130133
```rust
131-
pub trait Haystack: Deref<Target: Hay> + Sized {
134+
pub unsafe trait Haystack: Deref<Target: Hay> + Sized {
132135
fn empty() -> Self;
133136
unsafe fn split_around(self, range: Range<Self::Target::Index>) -> [Self; 3];
134137

@@ -1008,7 +1011,7 @@ the hay, instead of haystack.
10081011

10091012
```rust
10101013
// v3.0-alpha.5
1011-
trait Haystack: Deref<Target: Hay> {
1014+
unsafe trait Haystack: Deref<Target: Hay> {
10121015
...
10131016
}
10141017
trait Searcher<A: Hay + ?Sized> {
@@ -1126,7 +1129,7 @@ We share a searcher implementation by introducing the `Hay` trait, as the derefe
11261129
`Haystack` trait, i.e. `&[T]`, `&mut [T]` and `Vec<T>` will all be delegated to `[T]`:
11271130

11281131
```rust
1129-
trait Haystack: Deref<Target: Hay> + Sized {
1132+
unsafe trait Haystack: Deref<Target: Hay> + Sized {
11301133
...
11311134
}
11321135
unsafe trait Searcher<A: Hay + ?Sized> {
@@ -1140,7 +1143,7 @@ would require custom dynamic-sized types (DSTs).
11401143
An alternative formation is delegating to a shared haystack by generic associated types (GATs):
11411144

11421145
```rust
1143-
trait Haystack: Sized {
1146+
unsafe trait Haystack: Sized {
11441147
type Shared<'a>: SharedHaystack;
11451148
fn borrow(&self) -> Self::Shared<'_>;
11461149
...
@@ -1165,7 +1168,7 @@ The `Haystack` trait inherits `Deref` and requires its `Target` to implement `Ha
11651168
is extending `Borrow` instead:
11661169

11671170
```rust
1168-
trait Haystack: Borrow<Self::Hay> + Sized {
1171+
unsafe trait Haystack: Borrow<Self::Hay> + Sized {
11691172
type Hay: Hay + ?Sized;
11701173
...
11711174
}

0 commit comments

Comments
 (0)