Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
763703c
Remove redundant check for promoteds
tmiasko Oct 11, 2021
d5a91f3
Use `IndexVec::indices` instead of reimplementing it
tmiasko Oct 11, 2021
c3e71d8
Fix a variant index and variant discriminant confusion
tmiasko Oct 11, 2021
cd626fe
Stabilize -Z print-link-args as --print link-args
joshtriplett Dec 6, 2021
371bd46
Document `--print link-args`
joshtriplett Dec 6, 2021
36a1141
Make message for &T -> &mut T transmute more accurate
Jan 9, 2022
dc1c39b
rustdoc: decouple stability and const-stability
euclio Dec 9, 2021
c280752
Help optimize out backtraces when disabled
kornelski Jan 17, 2022
a8b7116
change `ct_infer` to a delay_span_bug
lcnr Jan 5, 2022
621e60a
remove unnecessary fixme
lcnr Jan 5, 2022
b2d8f0c
generic_arg_infer: placeholder in signature err
lcnr Jan 5, 2022
cbc6d35
privacy: update `visit_infer`
lcnr Jan 10, 2022
217458b
intra-doc links
lcnr Jan 17, 2022
29a2d6b
intra-doc: Use the impl's assoc item where possible
camelid Jan 8, 2022
2938be6
Correctly handle starts in block doc comments
GuillaumeGomez Jan 18, 2022
06b00ad
Add test for block doc comments
GuillaumeGomez Jan 18, 2022
5ab67bf
Fix CVE-2022-21658 for Windows
ChrisDenton Jan 6, 2022
54e22eb
Fix CVE-2022-21658 for UNIX-like
hkratz Dec 18, 2021
cb748a2
Fix CVE-2022-21658 for WASI
alexcrichton Jan 4, 2022
32080ad
Update std::fs::remove_dir_all documentation
pietroalbini Jan 19, 2022
5c96dcf
Add MaybeUninit::as_bytes
Amanieu Oct 10, 2021
6487845
Properly account for binders in get_impl_future_output_ty
tmandry Dec 21, 2021
5c15ad7
NiceRegionError: Use written return type for async fn
tmandry Nov 20, 2021
698631e
Simplify error reporting code, remove await point wording
tmandry Dec 10, 2021
152e888
Rustdoc mobile: put out-of-band on its own line
jsha Jan 13, 2022
3b10045
:arrow_up: rust-analyzer
lnicola Jan 20, 2022
0a6c9ad
Fix compilation for a few tier 2 targets
hkratz Jan 20, 2022
98cb338
Rollup merge of #89747 - Amanieu:maybeuninit_bytes, r=m-ou-se
matthiaskrgr Jan 20, 2022
d188287
Rollup merge of #89764 - tmiasko:uninhabited-enums, r=wesleywiser
matthiaskrgr Jan 20, 2022
02379e9
Rollup merge of #91606 - joshtriplett:stabilize-print-link-args, r=pn…
matthiaskrgr Jan 20, 2022
405cf20
Rollup merge of #91694 - euclio:stability-improvements, r=GuillaumeGomez
matthiaskrgr Jan 20, 2022
413f490
Rollup merge of #92183 - tmandry:issue-74256, r=estebank
matthiaskrgr Jan 20, 2022
db1253f
Rollup merge of #92582 - lcnr:generic-arg-infer, r=BoxyUwU
matthiaskrgr Jan 20, 2022
1839829
Rollup merge of #92680 - camelid:assoc-item-cleanup, r=petrochenkov
matthiaskrgr Jan 20, 2022
5c10dbd
Rollup merge of #92704 - 5225225:std_mem_transmute_ref_t_mut_t, r=mic…
matthiaskrgr Jan 20, 2022
ed3bf67
Rollup merge of #92861 - jsha:mobile-column-flex, r=GuillaumeGomez
matthiaskrgr Jan 20, 2022
1cb57e2
Rollup merge of #92992 - kornelski:backtraceopt, r=Mark-Simulacrum
matthiaskrgr Jan 20, 2022
6c627d2
Rollup merge of #93038 - GuillaumeGomez:block-doc-comments, r=notriddle
matthiaskrgr Jan 20, 2022
d893b0a
Rollup merge of #93108 - lnicola:rust-analyzer-2022-01-20, r=lnicola
matthiaskrgr Jan 20, 2022
dbc9749
Rollup merge of #93112 - pietroalbini:pa-cve-2022-21658-nightly, r=pi…
matthiaskrgr Jan 20, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add MaybeUninit::as_bytes
  • Loading branch information
Amanieu committed Jan 19, 2022
commit 5c96dcf96100d67370e794d3ffd59762dbc102ed
125 changes: 124 additions & 1 deletion library/core/src/mem/maybe_uninit.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::any::type_name;
use crate::fmt;
use crate::intrinsics;
use crate::mem::ManuallyDrop;
use crate::mem::{self, ManuallyDrop};
use crate::ptr;
use crate::slice;

/// A wrapper type to construct uninitialized instances of `T`.
///
Expand Down Expand Up @@ -1160,4 +1161,126 @@ impl<T> MaybeUninit<T> {
// SAFETY: Valid elements have just been written into `this` so it is initialized
unsafe { MaybeUninit::slice_assume_init_mut(this) }
}

/// Returns the contents of this `MaybeUninit` as a slice of potentially uninitialized bytes.
///
/// Note that even if the contents of a `MaybeUninit` have been initialized, the value may still
/// contain padding bytes which are left uninitialized.
///
/// # Examples
///
/// ```
/// #![feature(maybe_uninit_as_bytes, maybe_uninit_slice)]
/// use std::mem::MaybeUninit;
///
/// let val = 0x12345678i32;
/// let uninit = MaybeUninit::new(val);
/// let uninit_bytes = uninit.as_bytes();
/// let bytes = unsafe { MaybeUninit::slice_assume_init_ref(uninit_bytes) };
/// assert_eq!(bytes, val.to_ne_bytes());
/// ```
#[unstable(feature = "maybe_uninit_as_bytes", issue = "93092")]
pub fn as_bytes(&self) -> &[MaybeUninit<u8>] {
// SAFETY: MaybeUninit<u8> is always valid, even for padding bytes
unsafe {
slice::from_raw_parts(self.as_ptr() as *const MaybeUninit<u8>, mem::size_of::<T>())
}
}

/// Returns the contents of this `MaybeUninit` as a mutable slice of potentially uninitialized
/// bytes.
///
/// Note that even if the contents of a `MaybeUninit` have been initialized, the value may still
/// contain padding bytes which are left uninitialized.
///
/// # Examples
///
/// ```
/// #![feature(maybe_uninit_as_bytes)]
/// use std::mem::MaybeUninit;
///
/// let val = 0x12345678i32;
/// let mut uninit = MaybeUninit::new(val);
/// let uninit_bytes = uninit.as_bytes_mut();
/// if cfg!(target_endian = "little") {
/// uninit_bytes[0].write(0xcd);
/// } else {
/// uninit_bytes[3].write(0xcd);
/// }
/// let val2 = unsafe { uninit.assume_init() };
/// assert_eq!(val2, 0x123456cd);
/// ```
#[unstable(feature = "maybe_uninit_as_bytes", issue = "93092")]
pub fn as_bytes_mut(&mut self) -> &mut [MaybeUninit<u8>] {
// SAFETY: MaybeUninit<u8> is always valid, even for padding bytes
unsafe {
slice::from_raw_parts_mut(
self.as_mut_ptr() as *mut MaybeUninit<u8>,
mem::size_of::<T>(),
)
}
}

/// Returns the contents of this slice of `MaybeUninit` as a slice of potentially uninitialized
/// bytes.
///
/// Note that even if the contents of a `MaybeUninit` have been initialized, the value may still
/// contain padding bytes which are left uninitialized.
///
/// # Examples
///
/// ```
/// #![feature(maybe_uninit_as_bytes, maybe_uninit_write_slice, maybe_uninit_slice)]
/// use std::mem::MaybeUninit;
///
/// let uninit = [MaybeUninit::new(0x1234u16), MaybeUninit::new(0x5678u16)];
/// let uninit_bytes = MaybeUninit::slice_as_bytes(&uninit);
/// let bytes = unsafe { MaybeUninit::slice_assume_init_ref(&uninit_bytes) };
/// let val1 = u16::from_ne_bytes(bytes[0..2].try_into().unwrap());
/// let val2 = u16::from_ne_bytes(bytes[2..4].try_into().unwrap());
/// assert_eq!(&[val1, val2], &[0x1234u16, 0x5678u16]);
/// ```
#[unstable(feature = "maybe_uninit_as_bytes", issue = "93092")]
pub fn slice_as_bytes(this: &[MaybeUninit<T>]) -> &[MaybeUninit<u8>] {
// SAFETY: MaybeUninit<u8> is always valid, even for padding bytes
unsafe {
slice::from_raw_parts(
this.as_ptr() as *const MaybeUninit<u8>,
this.len() * mem::size_of::<T>(),
)
}
}

/// Returns the contents of this mutable slice of `MaybeUninit` as a mutable slice of
/// potentially uninitialized bytes.
///
/// Note that even if the contents of a `MaybeUninit` have been initialized, the value may still
/// contain padding bytes which are left uninitialized.
///
/// # Examples
///
/// ```
/// #![feature(maybe_uninit_as_bytes, maybe_uninit_write_slice, maybe_uninit_slice)]
/// use std::mem::MaybeUninit;
///
/// let mut uninit = [MaybeUninit::<u16>::uninit(), MaybeUninit::<u16>::uninit()];
/// let uninit_bytes = MaybeUninit::slice_as_bytes_mut(&mut uninit);
/// MaybeUninit::write_slice(uninit_bytes, &[0x12, 0x34, 0x56, 0x78]);
/// let vals = unsafe { MaybeUninit::slice_assume_init_ref(&uninit) };
/// if cfg!(target_endian = "little") {
/// assert_eq!(vals, &[0x3412u16, 0x7856u16]);
/// } else {
/// assert_eq!(vals, &[0x1234u16, 0x5678u16]);
/// }
/// ```
#[unstable(feature = "maybe_uninit_as_bytes", issue = "93092")]
pub fn slice_as_bytes_mut(this: &mut [MaybeUninit<T>]) -> &mut [MaybeUninit<u8>] {
// SAFETY: MaybeUninit<u8> is always valid, even for padding bytes
unsafe {
slice::from_raw_parts_mut(
this.as_mut_ptr() as *mut MaybeUninit<u8>,
this.len() * mem::size_of::<T>(),
)
}
}
}