Skip to content

Commit

Permalink
Add support for Rust 1.60
Browse files Browse the repository at this point in the history
  • Loading branch information
jhpratt committed Apr 7, 2022
1 parent 65b52e4 commit efd0e4c
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 3 deletions.
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "standback"
version = "0.4.3"
version = "0.4.4"
authors = ["Jacob Pratt <open-source@jhpratt.dev>", "The Rust Project Developers"]
edition = "2018"
repository = "https://github.com/jhpratt/standback"
Expand Down Expand Up @@ -43,7 +43,8 @@ msrv-1-54 = ["msrv-1-55"]
msrv-1-55 = ["msrv-1-56"]
msrv-1-56 = ["msrv-1-57"]
msrv-1-57 = ["msrv-1-58"]
msrv-1-58 = []
msrv-1-58 = ["msrv-1-59"]
msrv-1-59 = []

[dependencies]
easy-ext = "1.0.0"
Expand Down
3 changes: 2 additions & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use version_check::{Channel, Version};
// We assume that features are never stabilized in patch versions.
// If a "Rust 2.0" is ever released, we'll have to handle that explicitly.
const MSRV_MINOR: u16 = 36;
const CURRENT_MINOR: u16 = 59;
const CURRENT_MINOR: u16 = 60;

fn main() {
let msrv = Version::from_mmp(1, MSRV_MINOR, 0);
Expand Down Expand Up @@ -74,6 +74,7 @@ fn main() {
"msrv-1-56" 56,
"msrv-1-57" 57,
"msrv-1-58" 58,
"msrv-1-59" 59,
];

if !explicit_msrv_set {
Expand Down
4 changes: 4 additions & 0 deletions src/inherent/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#[cfg(shim = "1.57")] mod v1_57;
#[cfg(shim = "1.58")] mod v1_58;
#[cfg(shim = "1.59")] mod v1_59;
#[cfg(shim = "1.60")] mod v1_60;

#[allow(unused_imports)] use sealed::Sealed;
mod sealed {
Expand Down Expand Up @@ -90,3 +91,6 @@ pub use v1_58::*;
#[cfg(shim = "1.59")]
#[allow(unreachable_pub)]
pub use v1_59::*;
#[cfg(shim = "1.60")]
#[allow(unreachable_pub)]
pub use v1_60::*;
84 changes: 84 additions & 0 deletions src/inherent/v1_60.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#[cfg(feature = "alloc")] use alloc::vec::Vec;
use core::mem::MaybeUninit;
use core::{ascii, mem, ptr, slice};

use easy_ext::ext;

use crate::inherent::sealed::Sealed;

#[ext]
pub impl u8
where Self: Sealed<u8>
{
fn escape_ascii(self) -> ascii::EscapeDefault {
ascii::escape_default(self)
}
}

#[cfg(feature = "alloc")]
#[ext]
pub impl<T> Vec<T>
where Self: Sealed<Vec<T>>
{
fn spare_capacity_mut(&mut self) -> &mut [MaybeUninit<T>] {
unsafe {
slice::from_raw_parts_mut(
self.as_mut_ptr().add(self.len()) as *mut MaybeUninit<T>,
self.capacity() - self.len(),
)
}
}
}

#[ext]
pub impl<T> MaybeUninit<T>
where Self: Sealed<MaybeUninit<T>>
{
unsafe fn assume_init_drop(&mut self) {
ptr::drop_in_place(self.as_mut_ptr())
}

unsafe fn assume_init_read(&self) -> T {
self.as_ptr().read()
}
}

macro_rules! impl_abs_diff {
($($unsigned:ident $signed:ident)*) => {$(
#[ext]
pub impl $unsigned
where Self: Sealed<$unsigned>
{
fn abs_diff(self, other: $unsigned) -> $unsigned {
if mem::size_of::<Self>() == 1 {
(self as i32).wrapping_sub(other as i32).abs() as Self
} else if self < other {
other - self
} else {
self - other
}
}
}

#[ext]
pub impl $signed
where Self: Sealed<$signed> {
fn abs_diff(self, other: $signed) -> $unsigned {
if self < other {
(other as $unsigned).wrapping_sub(self as $unsigned)
} else {
(self as $unsigned).wrapping_sub(other as $unsigned)
}
}
}
)*};
}

impl_abs_diff![
u8 i8
u16 i16
u32 i32
u64 i64
u128 i128
usize isize
];
10 changes: 10 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ Note that items stabilized prior to the declared MSRV _will not_ be re-exported.
The following methods and constants are available via the shim. For brevity, `i*` is `i8`, `i16`,
`i32`, `i64`, `i128`, and `isize`; `u*` is `u8`, `u16`, `u32`, `u64`, `u128`, and `usize`.
## 1.60
```text
u8::escape_ascii
Vec::spare_capacity_mut
MaybeUninit::assume_init_drop
MaybeUninit::assume_init_read
{i*, u*}::abs_diff
```
## 1.59
```text
Expand Down

0 comments on commit efd0e4c

Please sign in to comment.