std: Unsafe-wrap std::io

This commit is contained in:
Jubilee Young 2024-07-14 17:14:55 -07:00
parent 83a0fe5396
commit df353a0cc3
4 changed files with 13 additions and 9 deletions

View File

@ -433,9 +433,11 @@ impl<W: ?Sized + Write> BufWriter<W> {
let old_len = self.buf.len(); let old_len = self.buf.len();
let buf_len = buf.len(); let buf_len = buf.len();
let src = buf.as_ptr(); let src = buf.as_ptr();
let dst = self.buf.as_mut_ptr().add(old_len); unsafe {
ptr::copy_nonoverlapping(src, dst, buf_len); let dst = self.buf.as_mut_ptr().add(old_len);
self.buf.set_len(old_len + buf_len); ptr::copy_nonoverlapping(src, dst, buf_len);
self.buf.set_len(old_len + buf_len);
}
} }
#[inline] #[inline]

View File

@ -482,7 +482,7 @@ where
A: Allocator, A: Allocator,
{ {
debug_assert!(vec.capacity() >= pos + buf.len()); debug_assert!(vec.capacity() >= pos + buf.len());
vec.as_mut_ptr().add(pos).copy_from(buf.as_ptr(), buf.len()); unsafe { vec.as_mut_ptr().add(pos).copy_from(buf.as_ptr(), buf.len()) };
pos + buf.len() pos + buf.len()
} }

View File

@ -267,11 +267,14 @@ where
// Using this rather than unwrap meaningfully improves the code // Using this rather than unwrap meaningfully improves the code
// for callers which only care about one variant (usually // for callers which only care about one variant (usually
// `Custom`) // `Custom`)
core::hint::unreachable_unchecked(); unsafe { core::hint::unreachable_unchecked() };
}); });
ErrorData::Simple(kind) ErrorData::Simple(kind)
} }
TAG_SIMPLE_MESSAGE => ErrorData::SimpleMessage(&*ptr.cast::<SimpleMessage>().as_ptr()), TAG_SIMPLE_MESSAGE => {
// SAFETY: per tag
unsafe { ErrorData::SimpleMessage(&*ptr.cast::<SimpleMessage>().as_ptr()) }
}
TAG_CUSTOM => { TAG_CUSTOM => {
// It would be correct for us to use `ptr::byte_sub` here (see the // It would be correct for us to use `ptr::byte_sub` here (see the
// comment above the `wrapping_add` call in `new_custom` for why), // comment above the `wrapping_add` call in `new_custom` for why),

View File

@ -293,7 +293,6 @@
//! [`Arc`]: crate::sync::Arc //! [`Arc`]: crate::sync::Arc
#![stable(feature = "rust1", since = "1.0.0")] #![stable(feature = "rust1", since = "1.0.0")]
#![allow(unsafe_op_in_unsafe_fn)]
#[cfg(test)] #[cfg(test)]
mod tests; mod tests;
@ -383,11 +382,11 @@ pub(crate) unsafe fn append_to_string<F>(buf: &mut String, f: F) -> Result<usize
where where
F: FnOnce(&mut Vec<u8>) -> Result<usize>, F: FnOnce(&mut Vec<u8>) -> Result<usize>,
{ {
let mut g = Guard { len: buf.len(), buf: buf.as_mut_vec() }; let mut g = Guard { len: buf.len(), buf: unsafe { buf.as_mut_vec() } };
let ret = f(g.buf); let ret = f(g.buf);
// SAFETY: the caller promises to only append data to `buf` // SAFETY: the caller promises to only append data to `buf`
let appended = g.buf.get_unchecked(g.len..); let appended = unsafe { g.buf.get_unchecked(g.len..) };
if str::from_utf8(appended).is_err() { if str::from_utf8(appended).is_err() {
ret.and_then(|_| Err(Error::INVALID_UTF8)) ret.and_then(|_| Err(Error::INVALID_UTF8))
} else { } else {