Skip to content
This repository has been archived by the owner on Jan 9, 2024. It is now read-only.

Commit

Permalink
Remove support for saturating shifts
Browse files Browse the repository at this point in the history
The implementation in stdlib has been removed as part of stabilizing
rust-lang/rust#87920. Support for saturating
shifts will be added back once rust-lang/libs-team#230
is fixed.
  • Loading branch information
mauricelam committed Nov 3, 2023
1 parent e7a5b19 commit e07e255
Show file tree
Hide file tree
Showing 7 changed files with 8 additions and 49 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
- name: Run tests
run: cargo test --verbose
- run: rustup install nightly
- run: cargo +nightly test --verbose --features saturating_int_impl
- run: cargo +nightly test --verbose

build-windows:

Expand All @@ -34,4 +34,4 @@ jobs:
- name: Run tests
run: cargo test --verbose
- run: rustup install nightly
- run: cargo +nightly test --verbose --features saturating_int_impl
- run: cargo +nightly test --verbose
1 change: 0 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"rust-analyzer.cargo.features": [
"saturating_int_impl"
]
}
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ proc-macro = true

[features]
default = []
saturating_int_impl = [] # Requires nightly and the lang-feature of the same name

[dependencies]
anyhow = "1.0.75"
Expand Down
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ wrapping! { 1_i32 + 2_i32 - 3_i32 };
* Sub `-`
* Mul `*`
* Div `/`
* Shl `<<` (except `saturating`, which is only supported with the feature
`saturating_int_impl`, and requires nightly)
* Shr `>>` (except `saturating`, which is only supported with the feature
`saturating_int_impl`, and requires nightly)
* Shl `<<` (except `saturating`, due to /~https://github.com/rust-lang/libs-team/issues/230)
* Shr `>>` (except `saturating`, due to /~https://github.com/rust-lang/libs-team/issues/230)
## Known issues
* For most operations, constraining the numeric literals are required (e.g.
`2_i32` instead of `2`), due to
Expand Down
26 changes: 4 additions & 22 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@
//! * Sub `-`
//! * Mul `*`
//! * Div `/`
//! * Shl `<<` (except `saturating`, which is only supported with the feature
//! `saturating_int_impl`, and requires nightly)
//! * Shr `>>` (except `saturating`, which is only supported with the feature
//! `saturating_int_impl`, and requires nightly)
//! * Shl `<<` (except `saturating`, due to /~https://github.com/rust-lang/libs-team/issues/230)
//! * Shr `>>` (except `saturating`, due to /~https://github.com/rust-lang/libs-team/issues/230)
//!
//! ## Known issues
//! * For most operations, constraining the numeric literals are required (e.g.
Expand Down Expand Up @@ -175,26 +173,10 @@ fn saturating_impl(item: TokenStream) -> anyhow::Result<TokenStream> {
syn::BinOp::Div(_) => quote! { (#new_left).saturating_div(#new_right) },
syn::BinOp::Rem(_) => quote! { (#new_left).saturating_rem(#new_right) },
syn::BinOp::Shl(_) => {
#[cfg(feature = "saturating_int_impl")]
{
quote! { (::core::num::Saturating(#new_left) #op (#new_right)).0 }
}

#[cfg(not(feature = "saturating_int_impl"))]
{
bail!("Saturating bit shifts are not supported (/~https://github.com/rust-lang/libs-team/issues/230)")
}
bail!("Saturating bit shifts are not supported (/~https://github.com/rust-lang/libs-team/issues/230)")
}
syn::BinOp::Shr(_) => {
#[cfg(feature = "saturating_int_impl")]
{
quote! { (::core::num::Saturating(#new_left) #op (#new_right)).0 }
}

#[cfg(not(feature = "saturating_int_impl"))]
{
bail!("Saturating bit shifts are not supported (/~https://github.com/rust-lang/libs-team/issues/230)")
}
bail!("Saturating bit shifts are not supported (/~https://github.com/rust-lang/libs-team/issues/230)")
}
syn::BinOp::And(_)
| syn::BinOp::Or(_)
Expand Down
9 changes: 0 additions & 9 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,7 @@ fn test_bitshift() {
});
}

#[cfg_attr(feature = "saturating_int_impl", ignore)]
#[test]
fn test_bitshift_no_saturating() {
saturating_impl(quote! { 1 << 2 >> 3 }).unwrap_err();
}

#[test]
#[cfg_attr(not(feature = "saturating_int_impl"), ignore)]
fn test_bitshift_saturating() {
assert_expansion!(saturating_impl! { 1 << 2 >> 3 }.unwrap(), {
(::core::num::Saturating((::core::num::Saturating(1) << (2)).0) >> (3)).0
});
}
10 changes: 0 additions & 10 deletions tests/test.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#![no_std]
#![cfg_attr(feature="saturating_int_impl", feature(saturating_int_impl))]
#![allow(clippy::precedence)]

use arithmetic_mode::{checked, panicking, saturating, wrapping};
Expand Down Expand Up @@ -74,15 +73,6 @@ fn test_checked_operator_precedence() {
assert_eq!(Some(11), checked! { 1_u8 + 2_u8 * 3_u8 + 4_u8 });
}

#[cfg(feature="saturating_int_impl")]
#[test]
fn test_saturating_shift() {
// Looks like the implementation of shift is just wrapping_shl / shr
// https://doc.rust-lang.org/src/core/num/saturating.rs.html#146
assert_eq!(2, saturating! { 1_u8 << 9_usize });
assert_eq!(127, saturating! { 255_u8 >> 9_usize });
}

macro_rules! test_unchanging {
($ident:ident, $expr:expr) => {
::paste::paste! {
Expand Down

0 comments on commit e07e255

Please sign in to comment.