Function std::intrinsics::write_bytes 1.0.0[−][src]
pub unsafe extern "rust-intrinsic" fn write_bytes<T>(
dst: *mut T,
val: u8,
count: usize
)
Sets count * size_of::<T>()
bytes of memory starting at dst
to
val
.
write_bytes
is similar to C's memset
, but sets count * size_of::<T>()
bytes to val
.
Safety
Behavior is undefined if any of the following conditions are violated:
-
dst
must be valid. -
dst.offset(count)
must be valid. In other words, the region of memory which begins atdst
and has a length ofcount * size_of::<T>()
bytes must belong to a single, live allocation. -
dst
must be properly aligned.
Additionally, the caller must ensure that writing count * size_of::<T>()
bytes to the given region of memory results in a valid
value of T
. Creating an invalid value of T
can result in undefined
behavior.
Examples
Basic usage:
use std::ptr; let mut vec = vec![0u32; 4]; unsafe { let vec_ptr = vec.as_mut_ptr(); ptr::write_bytes(vec_ptr, 0xfe, 2); } assert_eq!(vec, [0xfefefefe, 0xfefefefe, 0, 0]);Run
Creating an invalid value:
use std::ptr; let mut v = Box::new(0i32); unsafe { // Leaks the previously held value by overwriting the `Box<T>` with // a null pointer. ptr::write_bytes(&mut v, 0, 1); } // At this point, using or dropping `v` results in undefined behavior. // v = Box::new(0i32); // ERRORRun