@@ -86,7 +86,7 @@ A `Hay` is the core type which the search algorithm will run on.
86
86
It is implemented on the slice-like types like ` str ` , ` OsStr ` and ` [T] ` .
87
87
88
88
``` rust
89
- pub trait Hay {
89
+ pub unsafe trait Hay {
90
90
type Index : Copy + Debug + Eq ;
91
91
92
92
fn empty <'a >() -> & 'a Self ;
@@ -101,6 +101,9 @@ pub trait Hay {
101
101
}
102
102
```
103
103
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
+
104
107
We allow a hay to customize the ` Index ` type. While ` str ` , ` [T] ` and ` OsStr ` all use ` usize ` as
105
108
the index, we do want the Pattern API to support other linear structures like ` LinkedList<T> ` ,
106
109
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
128
131
A hay can * borrowed* from a haystack.
129
132
130
133
``` rust
131
- pub trait Haystack : Deref <Target : Hay > + Sized {
134
+ pub unsafe trait Haystack : Deref <Target : Hay > + Sized {
132
135
fn empty () -> Self ;
133
136
unsafe fn split_around (self , range : Range <Self :: Target :: Index >) -> [Self ; 3 ];
134
137
@@ -1008,7 +1011,7 @@ the hay, instead of haystack.
1008
1011
1009
1012
``` rust
1010
1013
// v3.0-alpha.5
1011
- trait Haystack : Deref <Target : Hay > {
1014
+ unsafe trait Haystack : Deref <Target : Hay > {
1012
1015
...
1013
1016
}
1014
1017
trait Searcher <A : Hay + ? Sized > {
@@ -1126,7 +1129,7 @@ We share a searcher implementation by introducing the `Hay` trait, as the derefe
1126
1129
` Haystack ` trait, i.e. ` &[T] ` , ` &mut [T] ` and ` Vec<T> ` will all be delegated to ` [T] ` :
1127
1130
1128
1131
``` rust
1129
- trait Haystack : Deref <Target : Hay > + Sized {
1132
+ unsafe trait Haystack : Deref <Target : Hay > + Sized {
1130
1133
...
1131
1134
}
1132
1135
unsafe trait Searcher <A : Hay + ? Sized > {
@@ -1140,7 +1143,7 @@ would require custom dynamic-sized types (DSTs).
1140
1143
An alternative formation is delegating to a shared haystack by generic associated types (GATs):
1141
1144
1142
1145
``` rust
1143
- trait Haystack : Sized {
1146
+ unsafe trait Haystack : Sized {
1144
1147
type Shared <'a >: SharedHaystack ;
1145
1148
fn borrow (& self ) -> Self :: Shared <'_ >;
1146
1149
...
@@ -1165,7 +1168,7 @@ The `Haystack` trait inherits `Deref` and requires its `Target` to implement `Ha
1165
1168
is extending ` Borrow ` instead:
1166
1169
1167
1170
``` rust
1168
- trait Haystack : Borrow <Self :: Hay > + Sized {
1171
+ unsafe trait Haystack : Borrow <Self :: Hay > + Sized {
1169
1172
type Hay : Hay + ? Sized ;
1170
1173
...
1171
1174
}
0 commit comments