Skip to content

Commit ac2ba16

Browse files
joshlfjswrenn
andcommitted
Remove Immutable where it's no longer needed
There are some APIs which still spuriously require `Immutable`, but which require more work to update. These will be fixed in a separate commit. Makes progress on #251 Co-authored-by: Jack Wrenn <[email protected]>
1 parent 0477bfb commit ac2ba16

18 files changed

+16
-294
lines changed

src/lib.rs

+10-23
Original file line numberDiff line numberDiff line change
@@ -1427,7 +1427,7 @@ pub unsafe trait TryFromBytes {
14271427
#[inline]
14281428
fn try_mut_from(bytes: &mut [u8]) -> Result<&mut Self, TryCastError<&mut [u8], Self>>
14291429
where
1430-
Self: KnownLayout + Immutable, // TODO(#251): Remove the `Immutable` bound.
1430+
Self: KnownLayout,
14311431
{
14321432
util::assert_dst_is_not_zst::<Self>();
14331433
match Ptr::from_mut(bytes).try_cast_into_no_leftover::<Self, BecauseExclusive>() {
@@ -1532,7 +1532,7 @@ pub unsafe trait TryFromBytes {
15321532
candidate: &mut [u8],
15331533
) -> Result<(&mut Self, &mut [u8]), TryCastError<&mut [u8], Self>>
15341534
where
1535-
Self: KnownLayout + Immutable,
1535+
Self: KnownLayout,
15361536
{
15371537
util::assert_dst_is_not_zst::<Self>();
15381538
try_mut_from_prefix_suffix(candidate, CastType::Prefix)
@@ -1619,7 +1619,7 @@ pub unsafe trait TryFromBytes {
16191619
candidate: &mut [u8],
16201620
) -> Result<(&mut [u8], &mut Self), TryCastError<&mut [u8], Self>>
16211621
where
1622-
Self: KnownLayout + Immutable,
1622+
Self: KnownLayout,
16231623
{
16241624
util::assert_dst_is_not_zst::<Self>();
16251625
try_mut_from_prefix_suffix(candidate, CastType::Suffix).map(swap)
@@ -2535,7 +2535,7 @@ pub unsafe trait FromBytes: FromZeros {
25352535
#[inline]
25362536
fn mut_from(bytes: &mut [u8]) -> Result<&mut Self, CastError<&mut [u8], Self>>
25372537
where
2538-
Self: IntoBytes + KnownLayout + Immutable,
2538+
Self: IntoBytes + KnownLayout,
25392539
{
25402540
util::assert_dst_is_not_zst::<Self>();
25412541
match Ptr::from_mut(bytes).try_cast_into_no_leftover::<_, BecauseExclusive>() {
@@ -2610,7 +2610,7 @@ pub unsafe trait FromBytes: FromZeros {
26102610
bytes: &mut [u8],
26112611
) -> Result<(&mut Self, &mut [u8]), CastError<&mut [u8], Self>>
26122612
where
2613-
Self: IntoBytes + KnownLayout + Immutable,
2613+
Self: IntoBytes + KnownLayout,
26142614
{
26152615
util::assert_dst_is_not_zst::<Self>();
26162616
let (slf, suffix) = Ptr::from_mut(bytes)
@@ -2679,7 +2679,7 @@ pub unsafe trait FromBytes: FromZeros {
26792679
bytes: &mut [u8],
26802680
) -> Result<(&mut [u8], &mut Self), CastError<&mut [u8], Self>>
26812681
where
2682-
Self: IntoBytes + KnownLayout + Immutable,
2682+
Self: IntoBytes + KnownLayout,
26832683
{
26842684
util::assert_dst_is_not_zst::<Self>();
26852685
let (slf, prefix) = Ptr::from_mut(bytes)
@@ -3483,7 +3483,7 @@ pub unsafe trait IntoBytes {
34833483
#[inline(always)]
34843484
fn as_mut_bytes(&mut self) -> &mut [u8]
34853485
where
3486-
Self: FromBytes + Immutable,
3486+
Self: FromBytes,
34873487
{
34883488
// Note that this method does not have a `Self: Sized` bound;
34893489
// `size_of_val` works for unsized values too.
@@ -3718,7 +3718,7 @@ pub unsafe trait IntoBytes {
37183718
#[inline]
37193719
fn as_bytes_mut(&mut self) -> &mut [u8]
37203720
where
3721-
Self: FromBytes + Immutable,
3721+
Self: FromBytes,
37223722
{
37233723
self.as_mut_bytes()
37243724
}
@@ -5076,20 +5076,16 @@ macro_rules! transmute_mut {
50765076
struct AssertSrcIsSized<'a, T: ::core::marker::Sized>(&'a T);
50775077
struct AssertSrcIsFromBytes<'a, T: ?::core::marker::Sized + $crate::FromBytes>(&'a T);
50785078
struct AssertSrcIsIntoBytes<'a, T: ?::core::marker::Sized + $crate::IntoBytes>(&'a T);
5079-
struct AssertSrcIsImmutable<'a, T: ?::core::marker::Sized + $crate::Immutable>(&'a T);
50805079
struct AssertDstIsSized<'a, T: ::core::marker::Sized>(&'a T);
50815080
struct AssertDstIsFromBytes<'a, T: ?::core::marker::Sized + $crate::FromBytes>(&'a T);
50825081
struct AssertDstIsIntoBytes<'a, T: ?::core::marker::Sized + $crate::IntoBytes>(&'a T);
5083-
struct AssertDstIsImmutable<'a, T: ?::core::marker::Sized + $crate::Immutable>(&'a T);
50845082

50855083
if true {
50865084
let _ = AssertSrcIsSized(&*e);
50875085
} else if true {
50885086
let _ = AssertSrcIsFromBytes(&*e);
5089-
} else if true {
5090-
let _ = AssertSrcIsIntoBytes(&*e);
50915087
} else {
5092-
let _ = AssertSrcIsImmutable(&*e);
5088+
let _ = AssertSrcIsIntoBytes(&*e);
50935089
}
50945090

50955091
if true {
@@ -5100,13 +5096,9 @@ macro_rules! transmute_mut {
51005096
#[allow(unused, unreachable_code)]
51015097
let u = AssertDstIsFromBytes(loop {});
51025098
&mut *u.0
5103-
} else if true {
5104-
#[allow(unused, unreachable_code)]
5105-
let u = AssertDstIsIntoBytes(loop {});
5106-
&mut *u.0
51075099
} else {
51085100
#[allow(unused, unreachable_code)]
5109-
let u = AssertDstIsImmutable(loop {});
5101+
let u = AssertDstIsIntoBytes(loop {});
51105102
&mut *u.0
51115103
}
51125104
} else if false {
@@ -5129,11 +5121,6 @@ macro_rules! transmute_mut {
51295121
&mut u
51305122
} else {
51315123
// SAFETY: For source type `Src` and destination type `Dst`:
5132-
// - We know that `Src: FromBytes + IntoBytes + Immutable` and `Dst:
5133-
// FromBytes + IntoBytes + Immutable` thanks to the uses of
5134-
// `AssertSrcIsFromBytes`, `AssertSrcIsIntoBytes`,
5135-
// `AssertSrcIsImmutable`, `AssertDstIsFromBytes`,
5136-
// `AssertDstIsIntoBytes`, and `AssertDstIsImmutable` above.
51375124
// - We know that `size_of::<Src>() == size_of::<Dst>()` thanks to
51385125
// the use of `assert_size_eq!` above.
51395126
// - We know that `align_of::<Src>() >= align_of::<Dst>()` thanks to

src/macro_util.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -386,8 +386,8 @@ pub const unsafe fn transmute_ref<'dst, 'src: 'dst, Src: 'src, Dst: 'dst>(
386386
/// # Safety
387387
///
388388
/// The caller must guarantee that:
389-
/// - `Src: FromBytes + IntoBytes + Immutable`
390-
/// - `Dst: FromBytes + IntoBytes + Immutable`
389+
/// - `Src: FromBytes + IntoBytes`
390+
/// - `Dst: FromBytes + IntoBytes`
391391
/// - `size_of::<Src>() == size_of::<Dst>()`
392392
/// - `align_of::<Src>() >= align_of::<Dst>()`
393393
// TODO(#686): Consider removing the `Immutable` requirement.
@@ -403,9 +403,6 @@ pub unsafe fn transmute_mut<'dst, 'src: 'dst, Src: 'src, Dst: 'dst>(
403403
// vice-versa because the caller has guaranteed that `Src: FromBytes +
404404
// IntoBytes`, `Dst: FromBytes + IntoBytes`, and `size_of::<Src>() ==
405405
// size_of::<Dst>()`.
406-
// - We know that there are no `UnsafeCell`s, and thus we don't have to
407-
// worry about `UnsafeCell` overlap, because `Src: Immutable`
408-
// and `Dst: Immutable`.
409406
// - The caller has guaranteed that alignment is not increased.
410407
// - We know that the returned lifetime will not outlive the input lifetime
411408
// thanks to the lifetime bounds on this function.

src/ref.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,7 @@ where
677677
impl<'a, B, T> Ref<B, T>
678678
where
679679
B: 'a + IntoByteSliceMut<'a>,
680-
T: FromBytes + IntoBytes + KnownLayout + Immutable + ?Sized,
680+
T: FromBytes + IntoBytes + KnownLayout + ?Sized,
681681
{
682682
/// Converts this `Ref` into a mutable reference.
683683
///
@@ -802,6 +802,9 @@ where
802802
impl<B, T> DerefMut for Ref<B, T>
803803
where
804804
B: ByteSliceMut,
805+
// TODO(#251): We can't remove `Immutable` here because it's required by
806+
// the impl of `Deref`, which is a super-trait of `DerefMut`. Maybe we can
807+
// add a separate inherent method for this?
805808
T: FromBytes + IntoBytes + KnownLayout + Immutable + ?Sized,
806809
{
807810
#[inline]

tests/ui-msrv/transmute-mut-dst-not-a-reference.stderr

-10
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,3 @@ error[E0308]: mismatched types
4747
= note: expected type `usize`
4848
found mutable reference `&mut _`
4949
= note: this error originates in the macro `transmute_mut` (in Nightly builds, run with -Z macro-backtrace for more info)
50-
51-
error[E0308]: mismatched types
52-
--> tests/ui-msrv/transmute-mut-dst-not-a-reference.rs:17:36
53-
|
54-
17 | const DST_NOT_A_REFERENCE: usize = transmute_mut!(&mut 0u8);
55-
| ^^^^^^^^^^^^^^^^^^^^^^^^ expected `usize`, found `&mut _`
56-
|
57-
= note: expected type `usize`
58-
found mutable reference `&mut _`
59-
= note: this error originates in the macro `transmute_mut` (in Nightly builds, run with -Z macro-backtrace for more info)

tests/ui-msrv/transmute-mut-dst-not-nocell.rs

-1
This file was deleted.

tests/ui-msrv/transmute-mut-dst-not-nocell.stderr

-12
This file was deleted.

tests/ui-msrv/transmute-mut-src-not-nocell.rs

-1
This file was deleted.

tests/ui-msrv/transmute-mut-src-not-nocell.stderr

-25
This file was deleted.

tests/ui-nightly/transmute-mut-dst-not-a-reference.stderr

-10
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,6 @@ error[E0308]: mismatched types
3838
found mutable reference `&mut _`
3939
= note: this error originates in the macro `transmute_mut` (in Nightly builds, run with -Z macro-backtrace for more info)
4040

41-
error[E0308]: mismatched types
42-
--> tests/ui-nightly/transmute-mut-dst-not-a-reference.rs:17:36
43-
|
44-
17 | const DST_NOT_A_REFERENCE: usize = transmute_mut!(&mut 0u8);
45-
| ^^^^^^^^^^^^^^^^^^^^^^^^ expected `usize`, found `&mut _`
46-
|
47-
= note: expected type `usize`
48-
found mutable reference `&mut _`
49-
= note: this error originates in the macro `transmute_mut` (in Nightly builds, run with -Z macro-backtrace for more info)
50-
5141
error[E0308]: mismatched types
5242
--> tests/ui-nightly/transmute-mut-dst-not-a-reference.rs:17:36
5343
|

tests/ui-nightly/transmute-mut-dst-not-nocell.rs

-24
This file was deleted.

tests/ui-nightly/transmute-mut-dst-not-nocell.stderr

-25
This file was deleted.

tests/ui-nightly/transmute-mut-src-not-nocell.rs

-24
This file was deleted.

tests/ui-nightly/transmute-mut-src-not-nocell.stderr

-48
This file was deleted.

tests/ui-stable/transmute-mut-dst-not-a-reference.stderr

-10
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,6 @@ error[E0308]: mismatched types
3838
found mutable reference `&mut _`
3939
= note: this error originates in the macro `transmute_mut` (in Nightly builds, run with -Z macro-backtrace for more info)
4040

41-
error[E0308]: mismatched types
42-
--> tests/ui-stable/transmute-mut-dst-not-a-reference.rs:17:36
43-
|
44-
17 | const DST_NOT_A_REFERENCE: usize = transmute_mut!(&mut 0u8);
45-
| ^^^^^^^^^^^^^^^^^^^^^^^^ expected `usize`, found `&mut _`
46-
|
47-
= note: expected type `usize`
48-
found mutable reference `&mut _`
49-
= note: this error originates in the macro `transmute_mut` (in Nightly builds, run with -Z macro-backtrace for more info)
50-
5141
error[E0308]: mismatched types
5242
--> tests/ui-stable/transmute-mut-dst-not-a-reference.rs:17:36
5343
|

tests/ui-stable/transmute-mut-dst-not-nocell.rs

-1
This file was deleted.

0 commit comments

Comments
 (0)