File tree 4 files changed +47
-25
lines changed
4 files changed +47
-25
lines changed Original file line number Diff line number Diff line change @@ -582,15 +582,21 @@ impl OsString {
582
582
#[ unstable( feature = "os_string_truncate" , issue = "133262" ) ]
583
583
pub fn truncate ( & mut self , len : usize ) {
584
584
self . as_os_str ( ) . inner . check_public_boundary ( len) ;
585
- self . inner . truncate ( len) ;
585
+ // SAFETY: The length was just checked to be at a valid boundary.
586
+ unsafe { self . inner . truncate_unchecked ( len) } ;
586
587
}
587
588
588
- /// Provides plumbing to core `Vec::extend_from_slice`.
589
- /// More well behaving alternative to allowing outer types
590
- /// full mutable access to the core `Vec`.
589
+ /// Provides plumbing to `Vec::extend_from_slice` without giving full
590
+ /// mutable access to the `Vec`.
591
+ ///
592
+ /// # Safety
593
+ ///
594
+ /// Extending the buffer with this slice must not require encoding-dependent
595
+ /// surrogate joining.
591
596
#[ inline]
592
- pub ( crate ) fn extend_from_slice ( & mut self , other : & [ u8 ] ) {
593
- self . inner . extend_from_slice ( other) ;
597
+ pub ( crate ) unsafe fn extend_from_slice_unchecked ( & mut self , other : & [ u8 ] ) {
598
+ // SAFETY: Guaranteed by caller.
599
+ unsafe { self . inner . extend_from_slice_unchecked ( other) } ;
594
600
}
595
601
}
596
602
Original file line number Diff line number Diff line change @@ -2759,7 +2759,8 @@ impl Path {
2759
2759
} ;
2760
2760
2761
2761
let mut new_path = PathBuf :: with_capacity ( new_capacity) ;
2762
- new_path. inner . extend_from_slice ( slice_to_copy) ;
2762
+ // SAFETY: The path is empty, so cannot have surrogate halves.
2763
+ unsafe { new_path. inner . extend_from_slice_unchecked ( slice_to_copy) } ;
2763
2764
new_path. set_extension ( extension) ;
2764
2765
new_path
2765
2766
}
Original file line number Diff line number Diff line change @@ -216,19 +216,26 @@ impl Buf {
216
216
self . as_slice ( ) . into_rc ( )
217
217
}
218
218
219
- /// Provides plumbing to core `Vec::truncate`.
220
- /// More well behaving alternative to allowing outer types
221
- /// full mutable access to the core `Vec`.
222
- #[ inline]
223
- pub ( crate ) fn truncate ( & mut self , len : usize ) {
219
+ /// Provides plumbing to `Vec::truncate` without giving full mutable access
220
+ /// to the `Vec`.
221
+ ///
222
+ /// # Safety
223
+ ///
224
+ /// The length must be at an `OsStr` boundary, according to
225
+ /// `Slice::check_public_boundary`.
226
+ #[ inline]
227
+ pub unsafe fn truncate_unchecked ( & mut self , len : usize ) {
224
228
self . inner . truncate ( len) ;
225
229
}
226
230
227
- /// Provides plumbing to core `Vec::extend_from_slice`.
228
- /// More well behaving alternative to allowing outer types
229
- /// full mutable access to the core `Vec`.
231
+ /// Provides plumbing to `Vec::extend_from_slice` without giving full
232
+ /// mutable access to the `Vec`.
233
+ ///
234
+ /// # Safety
235
+ ///
236
+ /// This encoding has no safety requirements.
230
237
#[ inline]
231
- pub ( crate ) fn extend_from_slice ( & mut self , other : & [ u8 ] ) {
238
+ pub unsafe fn extend_from_slice_unchecked ( & mut self , other : & [ u8 ] ) {
232
239
self . inner . extend_from_slice ( other) ;
233
240
}
234
241
}
Original file line number Diff line number Diff line change @@ -195,19 +195,27 @@ impl Buf {
195
195
self . as_slice ( ) . into_rc ( )
196
196
}
197
197
198
- /// Provides plumbing to core `Vec::truncate`.
199
- /// More well behaving alternative to allowing outer types
200
- /// full mutable access to the core `Vec`.
201
- #[ inline]
202
- pub ( crate ) fn truncate ( & mut self , len : usize ) {
198
+ /// Provides plumbing to `Vec::truncate` without giving full mutable access
199
+ /// to the `Vec`.
200
+ ///
201
+ /// # Safety
202
+ ///
203
+ /// The length must be at an `OsStr` boundary, according to
204
+ /// `Slice::check_public_boundary`.
205
+ #[ inline]
206
+ pub unsafe fn truncate_unchecked ( & mut self , len : usize ) {
203
207
self . inner . truncate ( len) ;
204
208
}
205
209
206
- /// Provides plumbing to core `Vec::extend_from_slice`.
207
- /// More well behaving alternative to allowing outer types
208
- /// full mutable access to the core `Vec`.
210
+ /// Provides plumbing to `Vec::extend_from_slice` without giving full
211
+ /// mutable access to the `Vec`.
212
+ ///
213
+ /// # Safety
214
+ ///
215
+ /// Extending the buffer with this slice must not require joining
216
+ /// surrogates.
209
217
#[ inline]
210
- pub ( crate ) fn extend_from_slice ( & mut self , other : & [ u8 ] ) {
218
+ pub unsafe fn extend_from_slice_unchecked ( & mut self , other : & [ u8 ] ) {
211
219
self . inner . extend_from_slice ( other) ;
212
220
}
213
221
}
You can’t perform that action at this time.
0 commit comments