1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
//! Traits that enable building stable and private algorithms.
use crate::metrics::IntDistance;
use num::{NumCast, One, Zero};
use std::hash::Hash;
use std::ops::{AddAssign, DivAssign, MulAssign, SubAssign};
mod bounded;
pub use bounded::*;
mod arithmetic;
pub use arithmetic::*;
mod cast;
pub use cast::*;
mod operations;
pub use operations::*;
use self::samplers::CastInternalRational;
pub mod samplers;
/// A type that can be used as a stability or privacy constant to scale a distance.
///
/// Encapsulates the necessary traits for the new_from_constant method on maps.
/// Making a map from a constant has the general form:
///
/// ```text
/// d_out = TO::distance_cast(d_in.clone())?.inf_mul(c)?
/// ```
/// (where d_out and c are of type TO, which implements DistanceConstant)
///
/// - `InfCast<TI>` is for casting where the distance after the cast is gte the distance before the cast
/// - `InfMul` is to multiply with the constant `c` in a way that doesn't round down
/// - `ProductOrd` is now only for convenience
///
/// # Example
/// ```
/// use opendp::traits::DistanceConstant;
/// use opendp::error::Fallible;
/// fn example_map<TI, TO: DistanceConstant<TI>>(d_in: TI, c: TO) -> Fallible<TO> {
/// TO::inf_cast(d_in)?.inf_mul(&c)
/// }
///
/// assert_eq!(example_map(3.14159_f32, 2_i8).ok(), Some(8_i8));
/// // same thing, but annotate types in a different way
/// assert_eq!(example_map::<f32, i8>(3.14159, 2).ok(), Some(8));
/// ```
pub trait DistanceConstant<TI>:
'static + InfCast<TI> + InfMul + ProductOrd + Zero + Send + Sync
{
}
impl<TI, TO> DistanceConstant<TI> for TO where
TO: 'static + InfCast<TI> + InfMul + ProductOrd + Zero + Send + Sync
{
}
/// A shorthand to indicate the set of types that implement the most common traits, like Clone and Debug.
///
/// The other rollup traits [`Hashable`], [`Number`], [`Integer`] and [`Float`] inherit from this trait.
///
/// Examples: u8, u16, u32, u64, u128, i8, i16, i32, i64, i128, usize, f32, f64, bool, String.
///
/// Refer to the constituent traits to see proof definitions on methods.
///
/// # Example
/// ```
/// use opendp::traits::Primitive;
/// fn test_func<T: Primitive>(value: T) {
/// // can be debugged
/// println!("{value:?}");
///
/// // default values exist and members of type T can be compared
/// assert_eq!(T::default(), T::default());
///
/// // can check if is null
/// value.is_null();
/// }
///
/// test_func(1i8);
/// ```
pub trait Primitive:
'static + Clone + std::fmt::Debug + CheckNull + PartialEq + Default + CheckAtom + Send + Sync
{
}
impl<T> Primitive for T where
T: 'static
+ Clone
+ std::fmt::Debug
+ CheckNull
+ PartialEq
+ Default
+ CheckAtom
+ Send
+ Sync
{
}
/// The subset of [`Primitive`] types that implement Eq and Hash.
///
/// Hashable types can be used as HashMap keys and in HashSets.
///
/// Examples: u8, u16, u32, u64, u128, i8, i16, i32, i64, i128, usize, bool, String
///
/// This trait lists the traits that are implemented for hashable types.
/// Refer to the constituent traits to see proof definitions on methods.
///
/// # Example
/// ```
/// use opendp::traits::Hashable;
/// use std::collections::HashSet;
/// fn test_func<T: Hashable>(value: T) {
/// // can be debugged, as Hashable inherits all traits from Primitive
/// println!("{value:?}");
///
/// // can be used in hash sets and in the keys of hashmaps
/// let mut hashset = HashSet::new();
/// hashset.insert(value);
/// }
///
/// test_func("apple".to_string());
/// ```
pub trait Hashable: Primitive + Eq + Hash {}
impl<T> Hashable for T where T: Primitive + Eq + Hash {}
/// The subset of [`Primitive`] types that have numerical operations.
///
/// Examples: u8, u16, u32, u64, u128, i8, i16, i32, i64, i128, usize, f32, f64
///
/// This trait lists many traits that are implemented for numerical types.
/// It is a shorthand to provide broad numerical functionality to a generic type,
/// without polluting trait bounds with a large number of highly-specific traits.
///
/// Refer to the constituent traits to see proof definitions on methods.
///
/// # Example
/// ```
/// use opendp::traits::Number;
/// fn test_func<T: Number>(value: T) {
/// // can be debugged, as Number inherits all traits from Primitive:
/// println!("{value:?}");
///
/// // supports basic arithmetic and numerical properties
/// assert_eq!(T::zero().inf_mul(&value).ok(), Some(T::zero()));
/// }
///
/// test_func(1i8);
/// ```
pub trait Number:
Primitive
+ Copy
+ NumCast
+ AlertingAbs
+ num::traits::NumOps
+ SaturatingAdd
+ SaturatingMul
+ InfAdd
+ InfSub
+ InfMul
+ InfDiv
+ ProductOrd
+ Zero
+ One
+ PartialEq
+ AddAssign
+ SubAssign
+ MulAssign
+ DivAssign
+ FiniteBounds
+ ExactIntCast<usize>
+ ExactIntCast<i32>
+ InfCast<IntDistance>
+ InfCast<usize>
+ std::iter::Sum<Self>
+ for<'a> std::iter::Sum<&'a Self>
+ DistanceConstant<Self>
{
}
impl<T> Number for T where
T: Primitive
+ Copy
+ NumCast
+ AlertingAbs
+ num::traits::NumOps
+ SaturatingAdd
+ SaturatingMul
+ InfAdd
+ InfSub
+ InfMul
+ InfDiv
+ ProductOrd
+ Zero
+ One
+ PartialEq
+ AddAssign
+ SubAssign
+ MulAssign
+ DivAssign
+ FiniteBounds
+ ExactIntCast<usize>
+ ExactIntCast<i32>
+ InfCast<IntDistance>
+ InfCast<usize>
+ std::iter::Sum<Self>
+ for<'a> std::iter::Sum<&'a Self>
+ DistanceConstant<Self>
{
}
/// The intersection of [`Number`] types and [`Hashable`] types.
/// This happens to be integers.
///
/// Examples: u8, u16, u32, u64, u128, i8, i16, i32, i64, i128, usize
///
/// This trait lists many traits that are implemented for integer types.
/// It is a shorthand to provide broad integer functionality to a generic type,
/// without polluting trait bounds with a large number of highly-specific traits.
///
/// Refer to the constituent traits to see proof definitions on methods.
///
/// # Example
/// ```
/// use opendp::traits::Integer;
/// use std::collections::HashSet;
/// fn test_func<T: Integer>(value: T) {
/// // can be debugged
/// println!("{value:?}");
///
/// // supports arithmetic and has numerical properties
/// assert_eq!(T::zero().inf_mul(&value).ok(), Some(T::zero()));
///
/// // can be used in hash sets and in the keys of hashmaps:
/// let mut hashset = HashSet::new();
/// hashset.insert(value);
/// }
///
/// test_func(1i8);
/// ```
pub trait Integer: Number + Hashable + Ord {}
impl<T> Integer for T where T: Number + Hashable + Ord {}
/// Floating-point types.
///
/// Examples: f32, f64
///
/// This trait lists many traits that are implemented for floating-point types.
/// It is a shorthand to provide broad floating-point functionality to a generic type,
/// without polluting trait bounds with a large number of highly-specific traits.
///
/// Refer to the constituent traits to see proof definitions on methods.
///
/// # Example
/// ```
/// use opendp::traits::Float;
/// fn test_func<T: Float>(value: T) {
/// // can be debugged, as Integer inherits all traits from Primitive:
/// println!("{value:?}");
///
/// // supports arithmetic and has numerical properties
/// assert_eq!(T::zero().inf_mul(&value).ok(), Some(T::zero()));
/// }
///
/// test_func(3.14159);
/// ```
pub trait Float:
Number
+ num::Float
+ InherentNull
+ InfLn
+ InfLn1P
+ InfLog2
+ InfExp
+ InfExpM1
+ InfPowI
+ InfSqrt
+ FloatBits
+ CastInternalRational
+ ExactIntCast<Self::Bits>
+ RoundCast<f64>
{
}
impl<T> Float for T where
T: Number
+ num::Float
+ InherentNull
+ InfLn
+ InfLn1P
+ InfLog2
+ InfExp
+ InfExpM1
+ InfPowI
+ InfSqrt
+ FloatBits
+ CastInternalRational
+ ExactIntCast<Self::Bits>
+ RoundCast<f64>
{
}