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

Make Unalign's field pub #1076

Closed
wants to merge 1 commit into from
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
10 changes: 9 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9099,7 +9099,15 @@ mod tests {
Unaligned,
!FromBytes
);
assert_impls!([NotZerocopy]: KnownLayout, !NoCell, !TryFromBytes, !FromZeros, !FromBytes, !IntoBytes, !Unaligned);
assert_impls!(
[NotZerocopy]: KnownLayout,
!NoCell,
!TryFromBytes,
!FromZeros,
!FromBytes,
!IntoBytes,
!Unaligned
);
assert_impls!(
[u8; 0]: KnownLayout,
NoCell,
Expand Down
15 changes: 9 additions & 6 deletions src/wrappers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,12 @@ use super::*;
any(feature = "derive", test),
derive(NoCell, KnownLayout, FromBytes, IntoBytes, Unaligned)
)]
#[non_exhaustive]
#[repr(C, packed)]
pub struct Unalign<T>(T);
pub struct Unalign<T> {
/// A possibly-unaligned value.
pub unaligned: T,
}

#[cfg(not(any(feature = "derive", test)))]
impl_known_layout!(T => Unalign<T>);
Expand Down Expand Up @@ -102,7 +106,7 @@ impl<T> Unalign<T> {
/// Constructs a new `Unalign`.
#[inline(always)]
pub const fn new(val: T) -> Unalign<T> {
Unalign(val)
Unalign { unaligned: val }
}

/// Consumes `self`, returning the inner `T`.
Expand Down Expand Up @@ -220,7 +224,7 @@ impl<T> Unalign<T> {
/// [`read_unaligned`]: core::ptr::read_unaligned
#[inline(always)]
pub const fn get_ptr(&self) -> *const T {
ptr::addr_of!(self.0)
ptr::addr_of!(self.unaligned)
}

/// Gets an unaligned mutable raw pointer to the inner `T`.
Expand All @@ -240,7 +244,7 @@ impl<T> Unalign<T> {
// TODO(https://github.com/rust-lang/rust/issues/57349): Make this `const`.
#[inline(always)]
pub fn get_mut_ptr(&mut self) -> *mut T {
ptr::addr_of_mut!(self.0)
ptr::addr_of_mut!(self.unaligned)
}

/// Sets the inner `T`, dropping the previous value.
Expand Down Expand Up @@ -314,8 +318,7 @@ impl<T: Copy> Unalign<T> {
// TODO(https://github.com/rust-lang/rust/issues/57349): Make this `const`.
#[inline(always)]
pub fn get(&self) -> T {
let Unalign(val) = *self;
val
self.unaligned
}
}

Expand Down
Loading