Skip to main content

ranged_integers/
value_check.rs

1//! The compile-time infrastructure for Ranged.
2//! 
3//! The entities inside are public, but are not intended to be used by
4//! the end-user, unless the user is implementing a generic logics over Ranged.
5
6use crate::irang;
7
8/// The helper type allowing to restrict the const generic input parameters
9/// 
10/// Usage example. Allow `myfunction` only for positive values of `SOME_PARAM`:
11/// ```
12/// #![feature(generic_const_exprs)]
13/// #![feature(adt_const_params)]
14/// # use ranged_integers::value_check::*;
15///
16/// const fn myfunction_allowed(some_param: i32) -> OperationPossibility {
17///     allow_if(some_param>0)
18/// }
19///
20/// fn myfunction<const SOME_PARAM: i32>()
21/// where Assert<{myfunction_allowed(SOME_PARAM)}>: IsAllowed,
22/// {
23///    // code
24/// }
25/// ```
26pub enum Assert<const COND: OperationPossibility> {}
27
28/// A trait to be used with [`Assert`] type to restrict the const generic input parameters
29pub trait IsAllowed {}
30impl IsAllowed for Assert<{OperationPossibility::Allowed}> {}
31
32/// Used with the [`Assert`] and [`IsAllowed`] trait to restrict the const generic input parameters
33/// 
34/// The reason to use this enum instead of just a simple bool is that the error
35/// messages generated when the `Assert: IsAllowed` bounds are violated are
36/// non-informative when a bool is used.
37#[derive(PartialEq, Eq, core::marker::ConstParamTy)]
38pub enum OperationPossibility {
39    /// `Assert<Forbidden>` does not satisfy `IsAllowed`
40    Forbidden,
41
42    /// `Assert<Allowed>` satisfies `IsAllowed`
43    Allowed
44}
45
46/// Convert bool to [`OperationPossibility`]
47#[must_use]
48pub const fn allow_if(cond: bool)->OperationPossibility {
49    if cond { OperationPossibility::Allowed } else { OperationPossibility::Forbidden }
50}
51
52/// The layout selector for [`Ranged`](crate::Ranged) based on bounds
53/// 
54/// Pick out the "smallest" layout that fits the min..=max range.
55/// To be evaluated at compile time. Used with the [`allow_range`] function
56#[must_use]
57pub const fn memlayout(min: i128, max: i128) -> usize {
58    macro_rules! layout_variants {
59        ($($t:ident:$n:literal)+) => {
60            $(   if $t::MIN as i128 <= min && max <= $t::MAX as i128 {return $n}   )+
61        }
62    }
63    if min > max {
64        return 16;  // This will be forbidden by allow_range constraint
65    }
66    if min == max {return 0;}
67    layout_variants! {u8:1 i8:1 u16:2 i16:2 u32:4 i32:4 u64:8 i64:8}
68    16  // This will be forbidden by allow_range constraint
69}
70
71
72/// Top-level constraint for [`Ranged`](crate::Ranged) min/max bounds.
73/// 
74/// The [`Ranged`](crate::Ranged) is constrained to `allow_range(memlayout(MIN, MAX))`,
75/// which forbids the integers wider than 8 byte and also bounds the
76/// `memlayout(MIN, MAX)` constant, so the compiler is satisfied to use
77/// the internal representation of Ranged.
78#[must_use]
79pub const fn allow_range(sz: usize) -> OperationPossibility {
80    allow_if(sz <= 8)
81}
82
83/// Constraint of [`crate::Ranged::create_const`] method.
84/// 
85/// Checks if `v` is in range between `min` and `max`
86#[must_use]
87pub const fn allow_creation(min: irang, v: irang, max: irang) -> OperationPossibility {
88    allow_if(min <= v && v <= max)
89}