Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add guarded transmute to mut bytes #19

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ pub mod util;
pub mod guard;

pub use self::error::{Error, ErrorReason, GuardError};
pub use self::to_bytes::{guarded_transmute_to_bytes_pod_many, guarded_transmute_to_bytes_many, guarded_transmute_to_bytes_pod, guarded_transmute_to_bytes};
pub use self::to_bytes::{guarded_transmute_to_bytes_pod_many, guarded_transmute_to_bytes_many, guarded_transmute_to_bytes_pod, guarded_transmute_to_bytes, guarded_transmute_to_mut_bytes_pod, guarded_transmute_to_mut_bytes};
pub use self::pod::{PodTransmutable, guarded_transmute_pod_many_permissive, guarded_transmute_pod_vec_permissive, guarded_transmute_pod_many_pedantic,
guarded_transmute_pod_vec_pedantic, guarded_transmute_pod_pedantic, guarded_transmute_pod_many, guarded_transmute_pod_vec,
guarded_transmute_pod};
Expand Down
9 changes: 9 additions & 0 deletions src/to_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ pub unsafe fn guarded_transmute_to_bytes<T>(from: &T) -> &[u8] {
slice::from_raw_parts(from as *const T as *const u8, size_of::<T>())
}

/// Transmute a single instance of an arbitrary type into a mutable slice of its bytes.
pub unsafe fn guarded_transmute_to_mut_bytes<T>(from: &mut T) -> &mut [u8] {
slice::from_raw_parts_mut(from as *mut T as *mut u8, size_of::<T>())
}

/// Transmute a slice of arbitrary types into a slice of their bytes.
///
/// # Examples
Expand Down Expand Up @@ -175,3 +180,7 @@ pub fn guarded_transmute_to_bytes_pod<T: PodTransmutable>(from: &T) -> &[u8] {
pub fn guarded_transmute_to_bytes_pod_many<T: PodTransmutable>(from: &[T]) -> &[u8] {
unsafe { guarded_transmute_to_bytes_many(from) }
}

pub fn guarded_transmute_to_mut_bytes_pod<T: PodTransmutable>(from: &mut T) -> &mut [u8] {
unsafe { guarded_transmute_to_mut_bytes(from) }
}