Skip to content

Mixed integer ops #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ name: CI
on:
push:
branches:
- main
- "*"
pull_request:
branches:
- main
types: [opened, synchronize, reopened]
workflow_dispatch:

Expand Down
10 changes: 10 additions & 0 deletions Makefile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ args = ["clippy", "--all-features"]
[tasks.docs]
args = ["doc", "--no-deps", "--all-features"]

# Build package in no_std environment.
[tasks.no-std]
command = "cargo"
args = ["build", "--target", "thumbv7m-none-eabi"]
Expand Down Expand Up @@ -55,6 +56,11 @@ run_task = "chkall"
env = { "RUSTFLAGS" = "--cfg=cnst8bitonly" }
run_task = "coverage-tarpaulin"

# Linter check with 8bit types only.
[tasks.clp8]
env = { "RUSTFLAGS" = "--cfg=cnst8bitonly" }
run_task = "clippy"

# Run all 8bit related unit tests.
[tasks.t8]
env = { "RUSTFLAGS" = "--cfg=cnst8bitonly" }
Expand Down Expand Up @@ -129,3 +135,7 @@ args = [
"i128",
"isize",
]

# Run all 8bit related doc examples only.
[tasks.td8]
run_task = { name = ["tdu8", "tdi8"] }
35 changes: 18 additions & 17 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@
#![allow(incomplete_features)]
#![feature(generic_const_exprs)]
//
// Tracking issue for `mixed_integer_ops`:
// https://github.com/rust-lang/rust/issues/87840.
#![feature(mixed_integer_ops)]
//
// Tracking issue for `doc_auto_cfg` feature:
// https://github.com/rust-lang/rust/issues/43781.
#![feature(doc_auto_cfg)]
Expand All @@ -88,23 +92,20 @@ mod macros;
// default values for doc examples.
//
// Format:
// { uint, uint_mod, TypeName, ErrorName, MinErrorName, MaxErrorName },+
// { uint, sint, uint_mod, sint_mod, TypeName, ErrorName, MinErrorName, MaxErrorName },+
//
// Builds `u8` only, which is significantly faster. Useful for most development
// purposes.
#[cfg(cnst8bitonly)]
// Building 8bit related types exclusively is faster, useful for most development purposes.
#[cfg(any(cnst8bitonly, not(cnst8bitonly)))]
constrained_uint_def_impl! {
{ u8, u8, ConstrainedU8, ConstrainedU8Error, MinU8Error, MaxU8Error }
{ u8, i8, u8, i8, ConstrainedU8, ConstrainedU8Error, MinU8Error, MaxU8Error }
}
// Builds all unsigned types by default.
#[cfg(not(cnst8bitonly))]
constrained_uint_def_impl! {
{ u8, u8, ConstrainedU8, ConstrainedU8Error, MinU8Error, MaxU8Error },
{ u16, u16, ConstrainedU16, ConstrainedU16Error, MinU16Error, MaxU16Error },
{ u32, u32, ConstrainedU32, ConstrainedU32Error, MinU32Error, MaxU32Error },
{ u64, u64, ConstrainedU64, ConstrainedU64Error, MinU64Error, Max64Error },
{ u128, u128, ConstrainedU128, ConstrainedU128Error, Min128Error, Max128Error },
{ usize, usize, ConstrainedUsize, ConstrainedUsizeError, MinUsizeError, MaxUsizeError },
{ u16, i16, u16, i16, ConstrainedU16, ConstrainedU16Error, MinU16Error, MaxU16Error },
{ u32, i32, u32, i32, ConstrainedU32, ConstrainedU32Error, MinU32Error, MaxU32Error },
{ u64, i64, u64, i64, ConstrainedU64, ConstrainedU64Error, MinU64Error, Max64Error },
{ u128, i128, u128, i128, ConstrainedU128, ConstrainedU128Error, Min128Error, Max128Error },
{ usize, isize, usize, isize, ConstrainedUsize, ConstrainedUsizeError, MinUsizeError, MaxUsizeError },
}

// Define mods, containers, errors, tests and impls for signed integers with
Expand All @@ -113,19 +114,19 @@ constrained_uint_def_impl! {
// Format:
// { sint, uint, sint_mod, uint_mod, TypeName, ErrorName, MinErrorName, MaxErrorName },+
//
// Builds `i8` only, which is significantly faster. Useful for most development
// purposes.
#[cfg(cnst8bitonly)]
// Building 8bit related types exclusively is faster, useful for most development purposes.
#[cfg(any(cnst8bitonly, not(cnst8bitonly)))]
constrained_int_def_impl! {
{ i8, u8, i8, u8, ConstrainedI8, ConstrainedI8Error, MinI8Error, MaxI8Error },
}
// Builds all signed types by default.
#[cfg(not(cnst8bitonly))]
constrained_int_def_impl! {
{ i8, u8, i8, u8, ConstrainedI8, ConstrainedI8Error, MinI8Error, MaxI8Error },
{ i16, u16, i16, u16, ConstrainedI16, ConstrainedI16Error, MinI16Error, MaxI16Error },
{ i32, u32, i32, u32, ConstrainedI32, ConstrainedI32Error, MinI32Error, MaxI32Error },
{ i64, u64, i64, u64, ConstrainedI64, ConstrainedI64Error, MinI64Error, MaxI64Error },
{ i128, u128, i128, u128, ConstrainedI128, ConstrainedI128Error, MinI128Error, MaxI128Error },
{ isize, usize, isize, usize, ConstrainedIsize, ConstrainedIsizeError, MinIsizeError, MaxIsizeError },
}

#[cfg(test)]
mod proptest;
15 changes: 12 additions & 3 deletions src/macros/common.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Defines containers, errors, common impls and doc values for integers.
macro_rules! constrained_def_impl {
($Int:ty, $md:ident, $Ty:ident, $Err:ident, $MinErr:ident, $MaxErr:ident,
$min:literal..=$max:literal, ($l:literal, $h:literal)) =>
{
( $Int:ty, $md:ident, $Ty:ident, $Err:ident, $MinErr:ident, $MaxErr:ident,
$min:literal..=$max:literal, ($l:literal, $h:literal)
) => {
// This const function is used to enforce constraints for the range definition.
// Relevant const generics are: `MIN`, `MAX`.
// The constraints are:
Expand Down Expand Up @@ -418,6 +418,15 @@ macro_rules! constrained_def_impl {
pub const fn get(&self) -> $Int {
self.0
}

/// **Not** part of the public API, implementation detail.
// Unfortunate workaround.
// Issue: https://github.com/Mari-W/const_guards/issues/2.
#[doc(hidden)]
#[cfg(test)]
pub const fn __new(value: $Int) -> Result<Self, $Err<MIN, MAX>> {
Self::new_unguarded(value)
}
}

// Implements some ::core::fmt traits for `Constrained` types.
Expand Down
Loading