Skip to content

Commit

Permalink
Upgrade some code for MSRV 1.65
Browse files Browse the repository at this point in the history
Now that our MSRV is 1.65, we can clean up some code.

Makes progress on #67
  • Loading branch information
joshlf committed Oct 12, 2024
1 parent 2b56c07 commit 2c2e1b3
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 49 deletions.
21 changes: 4 additions & 17 deletions src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,12 +474,8 @@ impl DstLayout {
// invalid type) instead of allowing this panic to be hidden if the cast
// would have failed anyway for runtime reasons (such as a too-small
// memory region).
//
// TODO(#67): Once our MSRV is 1.65, use let-else:
// https://blog.rust-lang.org/2022/11/03/Rust-1.65.0.html#let-else-statements
let size_info = match self.size_info.try_to_nonzero_elem_size() {
Some(size_info) => size_info,
None => panic!("attempted to cast to slice type with zero-sized element"),
let Some(size_info) = self.size_info.try_to_nonzero_elem_size() else {
panic!("attempted to cast to slice type with zero-sized element");
};

// Precondition
Expand Down Expand Up @@ -531,13 +527,9 @@ impl DstLayout {
util::round_down_to_next_multiple_of_alignment(bytes_len, self.align);
// Calculate the maximum number of bytes that could be consumed
// by the trailing slice.
//
// TODO(#67): Once our MSRV is 1.65, use let-else:
// https://blog.rust-lang.org/2022/11/03/Rust-1.65.0.html#let-else-statements
let max_slice_and_padding_bytes = match max_total_bytes.checked_sub(offset) {
Some(max) => max,
let Some(max_slice_and_padding_bytes) = max_total_bytes.checked_sub(offset) else {
// `bytes_len` too small even for 0 trailing slice elements.
None => return Err(MetadataCastError::Size),
return Err(MetadataCastError::Size);
};

// Calculate the number of elements that fit in
Expand Down Expand Up @@ -596,11 +588,6 @@ impl DstLayout {
}
}

// TODO(#67): For some reason, on our MSRV toolchain, this `allow` isn't
// enforced despite having `#![allow(unknown_lints)]` at the crate root, but
// putting it here works. Once our MSRV is high enough that this bug has been
// fixed, remove this `allow`.
#[allow(unknown_lints)]
#[cfg(test)]
mod tests {
use super::*;
Expand Down
3 changes: 0 additions & 3 deletions src/pointer/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1296,9 +1296,6 @@ mod _casts {
U: 'a + ?Sized + KnownLayout + AliasingSafe<[u8], I::Aliasing, R>,
R: AliasingSafeReason,
{
// TODO(#67): Remove this allow. See NonNulSlicelExt for more
// details.
#[allow(unstable_name_collisions)]
match self.try_cast_into(CastType::Prefix, meta) {
Ok((slf, remainder)) => {
if remainder.len() == 0 {
Expand Down
7 changes: 1 addition & 6 deletions src/util/macro_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -469,12 +469,7 @@ pub const unsafe fn transmute_ref<'dst, 'src: 'dst, Src: 'src, Dst: 'dst>(
// - The caller has guaranteed that alignment is not increased.
// - We know that the returned lifetime will not outlive the input lifetime
// thanks to the lifetime bounds on this function.
//
// TODO(#67): Once our MSRV is 1.58, replace this `transmute` with `&*dst`.
#[allow(clippy::transmute_ptr_to_ref)]
unsafe {
mem::transmute(dst)
}
unsafe { &*dst }
}

/// Transmutes a mutable reference of one type to a mutable reference of another
Expand Down
2 changes: 1 addition & 1 deletion src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -725,7 +725,7 @@ pub(crate) mod polyfills {
// toolchain versions, `ptr.slice_from_raw_parts()` resolves to the inherent
// method rather than to this trait, and so this trait is considered unused.
//
// TODO(#67): Once our MSRV is high enough, remove this.
// TODO(#67): Once our MSRV is >= 1.79, remove this.
#[allow(unused)]
pub(crate) trait NumExt {
/// Subtract without checking for underflow.
Expand Down
22 changes: 0 additions & 22 deletions zerocopy-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1349,25 +1349,3 @@ fn impl_block<D: DataExt>(
}
}
}

// A polyfill for `Option::then_some`, which was added after our MSRV.
//
// The `#[allow(unused)]` is necessary because, on sufficiently recent toolchain
// versions, `b.then_some(...)` resolves to the inherent method rather than to
// this trait, and so this trait is considered unused.
//
// TODO(#67): Remove this once our MSRV is >= 1.62.
#[allow(unused)]
trait BoolExt {
fn then_some<T>(self, t: T) -> Option<T>;
}

impl BoolExt for bool {
fn then_some<T>(self, t: T) -> Option<T> {
if self {
Some(t)
} else {
None
}
}
}

0 comments on commit 2c2e1b3

Please sign in to comment.