Skip to content

Commit 6702b23

Browse files
committed
Make Unalign's field pub
This allows for directly writing to the field safely. Since Rust ensures that unsafe uses of this field require `unsafe`, we should additionally consider deprecating many of the methods of `Unalign`. Fixes #836
1 parent 83c68a0 commit 6702b23

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

src/lib.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9099,7 +9099,15 @@ mod tests {
90999099
Unaligned,
91009100
!FromBytes
91019101
);
9102-
assert_impls!([NotZerocopy]: KnownLayout, !NoCell, !TryFromBytes, !FromZeros, !FromBytes, !IntoBytes, !Unaligned);
9102+
assert_impls!(
9103+
[NotZerocopy]: KnownLayout,
9104+
!NoCell,
9105+
!TryFromBytes,
9106+
!FromZeros,
9107+
!FromBytes,
9108+
!IntoBytes,
9109+
!Unaligned
9110+
);
91039111
assert_impls!(
91049112
[u8; 0]: KnownLayout,
91059113
NoCell,

src/wrappers.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,12 @@ use super::*;
5555
any(feature = "derive", test),
5656
derive(NoCell, KnownLayout, FromBytes, IntoBytes, Unaligned)
5757
)]
58+
#[non_exhaustive]
5859
#[repr(C, packed)]
59-
pub struct Unalign<T>(T);
60+
pub struct Unalign<T> {
61+
/// A possibly-unaligned value.
62+
pub unaligned: T,
63+
}
6064

6165
#[cfg(not(any(feature = "derive", test)))]
6266
impl_known_layout!(T => Unalign<T>);
@@ -102,7 +106,7 @@ impl<T> Unalign<T> {
102106
/// Constructs a new `Unalign`.
103107
#[inline(always)]
104108
pub const fn new(val: T) -> Unalign<T> {
105-
Unalign(val)
109+
Unalign { unaligned: val }
106110
}
107111

108112
/// Consumes `self`, returning the inner `T`.
@@ -220,7 +224,7 @@ impl<T> Unalign<T> {
220224
/// [`read_unaligned`]: core::ptr::read_unaligned
221225
#[inline(always)]
222226
pub const fn get_ptr(&self) -> *const T {
223-
ptr::addr_of!(self.0)
227+
ptr::addr_of!(self.unaligned)
224228
}
225229

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

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

0 commit comments

Comments
 (0)