Skip to content

Commit 8c3a9c1

Browse files
std: Unsafe-wrap std::io
1 parent 91b7331 commit 8c3a9c1

File tree

4 files changed

+13
-9
lines changed

4 files changed

+13
-9
lines changed

std/src/io/buffered/bufwriter.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -433,9 +433,11 @@ impl<W: ?Sized + Write> BufWriter<W> {
433433
let old_len = self.buf.len();
434434
let buf_len = buf.len();
435435
let src = buf.as_ptr();
436-
let dst = self.buf.as_mut_ptr().add(old_len);
437-
ptr::copy_nonoverlapping(src, dst, buf_len);
438-
self.buf.set_len(old_len + buf_len);
436+
unsafe {
437+
let dst = self.buf.as_mut_ptr().add(old_len);
438+
ptr::copy_nonoverlapping(src, dst, buf_len);
439+
self.buf.set_len(old_len + buf_len);
440+
}
439441
}
440442

441443
#[inline]

std/src/io/cursor.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ where
482482
A: Allocator,
483483
{
484484
debug_assert!(vec.capacity() >= pos + buf.len());
485-
vec.as_mut_ptr().add(pos).copy_from(buf.as_ptr(), buf.len());
485+
unsafe { vec.as_mut_ptr().add(pos).copy_from(buf.as_ptr(), buf.len()) };
486486
pos + buf.len()
487487
}
488488

std/src/io/error/repr_bitpacked.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -267,11 +267,14 @@ where
267267
// Using this rather than unwrap meaningfully improves the code
268268
// for callers which only care about one variant (usually
269269
// `Custom`)
270-
core::hint::unreachable_unchecked();
270+
unsafe { core::hint::unreachable_unchecked() };
271271
});
272272
ErrorData::Simple(kind)
273273
}
274-
TAG_SIMPLE_MESSAGE => ErrorData::SimpleMessage(&*ptr.cast::<SimpleMessage>().as_ptr()),
274+
TAG_SIMPLE_MESSAGE => {
275+
// SAFETY: per tag
276+
unsafe { ErrorData::SimpleMessage(&*ptr.cast::<SimpleMessage>().as_ptr()) }
277+
}
275278
TAG_CUSTOM => {
276279
// It would be correct for us to use `ptr::byte_sub` here (see the
277280
// comment above the `wrapping_add` call in `new_custom` for why),

std/src/io/mod.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,6 @@
293293
//! [`Arc`]: crate::sync::Arc
294294
295295
#![stable(feature = "rust1", since = "1.0.0")]
296-
#![allow(unsafe_op_in_unsafe_fn)]
297296

298297
#[cfg(test)]
299298
mod tests;
@@ -383,11 +382,11 @@ pub(crate) unsafe fn append_to_string<F>(buf: &mut String, f: F) -> Result<usize
383382
where
384383
F: FnOnce(&mut Vec<u8>) -> Result<usize>,
385384
{
386-
let mut g = Guard { len: buf.len(), buf: buf.as_mut_vec() };
385+
let mut g = Guard { len: buf.len(), buf: unsafe { buf.as_mut_vec() } };
387386
let ret = f(g.buf);
388387

389388
// SAFETY: the caller promises to only append data to `buf`
390-
let appended = g.buf.get_unchecked(g.len..);
389+
let appended = unsafe { g.buf.get_unchecked(g.len..) };
391390
if str::from_utf8(appended).is_err() {
392391
ret.and_then(|_| Err(Error::INVALID_UTF8))
393392
} else {

0 commit comments

Comments
 (0)