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 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
// Copyright 2019 DFINITY // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. use crate::displayer::{DisplayProxy, DisplayerOf}; #[cfg(feature="serde")] use serde::{Deserialize, Deserializer, Serialize, Serializer}; use std::cmp::Ordering; use std::fmt; use std::hash::{Hash, Hasher}; use std::marker::PhantomData; use std::ops::{Add, AddAssign, Div, Mul, MulAssign, Sub, SubAssign}; /// `Amount<Unit>` provides a type-safe way to keep an amount of /// some `Unit`. /// /// E.g. the following code must not compile: /// /// ```compile_fail /// use phantom_newtype::Amount; /// /// // These structs are just markers and have no semantic meaning. /// enum Apples {} /// enum Oranges {} /// /// let trois_pommes = Amount::<Apples, u64>::from(3); /// let five_oranges = Amount::<Oranges, u64>::from(5); /// /// assert_eq!(8, (trois_pommes + five_oranges).get()) /// ``` /// /// `Amount<Unit, Repr>` defines common boilerplate to make type-safe /// amounts more convenient. For example, you can compare amounts: /// /// ``` /// use phantom_newtype::Amount; /// /// enum Apples {} /// type NumApples = Amount<Apples, u64>; /// /// assert_eq!(true, NumApples::from(3) < NumApples::from(5)); /// assert_eq!(false, NumApples::from(3) > NumApples::from(5)); /// assert_eq!(true, NumApples::from(3) != NumApples::from(5)); /// assert_eq!(true, NumApples::from(5) == NumApples::from(5)); /// assert_eq!(false, NumApples::from(5) != NumApples::from(5)); /// /// assert_eq!(vec![NumApples::from(3), NumApples::from(5)].iter().max().unwrap(), /// &NumApples::from(5)); /// ``` /// /// You can do simple arithmetics with amounts: /// /// ``` /// use phantom_newtype::Amount; /// /// enum Apples {} /// enum Oranges {} /// /// let x = Amount::<Apples, u64>::from(3); /// let y = Amount::<Oranges, u64>::from(5); /// /// assert_eq!(x + x, Amount::<Apples, u64>::from(6)); /// assert_eq!(y - y, Amount::<Oranges, u64>::from(0)); /// ``` /// /// Multiplication of amounts is not supported: multiplying meters by /// meters gives square meters. However, you can scale an amount by a /// scalar or divide amounts: /// /// ``` /// use phantom_newtype::Amount; /// /// enum Apples {} /// /// let x = Amount::<Apples, u64>::from(3); /// assert_eq!(x * 3, Amount::<Apples, u64>::from(9)); /// assert_eq!(1, x / x); /// assert_eq!(3, (x * 3) / x); /// ``` /// /// Note that the unit is only available at compile time, thus using /// `Amount` instead of `u64` doesn't incur any runtime penalty: /// /// ``` /// use phantom_newtype::Amount; /// /// enum Meters {} /// /// let ms = Amount::<Meters, u64>::from(10); /// assert_eq!(std::mem::size_of_val(&ms), std::mem::size_of::<u64>()); /// ``` /// /// Amounts can be serialized and deserialized with `serde`. Serialized /// forms of `Amount<Unit, Repr>` and `Repr` are identical. /// /// ``` /// #[cfg(feature = "serde")] { /// use phantom_newtype::Amount; /// use serde::{Serialize, Deserialize}; /// use serde_json; /// enum Meters {} /// /// let repr: u64 = 10; /// let m_10 = Amount::<Meters, u64>::from(repr); /// assert_eq!(serde_json::to_string(&m_10).unwrap(), serde_json::to_string(&repr).unwrap()); /// /// let copy: Amount<Meters, u64> = serde_json::from_str(&serde_json::to_string(&m_10).unwrap()).unwrap(); /// assert_eq!(copy, m_10); /// } /// ``` /// /// You can also declare constants of `Amount<Unit, Repr>` using `new` /// function: /// ``` /// use phantom_newtype::Amount; /// enum Meters {} /// type Distance = Amount<Meters, u64>; /// const ASTRONOMICAL_UNIT: Distance = Distance::new(149_597_870_700); /// /// assert!(ASTRONOMICAL_UNIT > Distance::from(0)); /// ``` /// /// Amounts can be sent between threads if the `Repr` allows it, no /// matter which `Unit` is used. /// /// ``` /// use phantom_newtype::Amount; /// /// type Cell = std::cell::RefCell<i64>; /// type NumCells = Amount<Cell, i64>; /// const N: NumCells = NumCells::new(1); /// /// let n_from_thread = std::thread::spawn(|| &N).join().unwrap(); /// assert_eq!(N, *n_from_thread); /// ``` pub struct Amount<Unit, Repr>(Repr, PhantomData<std::sync::Mutex<Unit>>); impl<Unit, Repr: Copy> Amount<Unit, Repr> { /// Returns the wrapped value. /// /// ``` /// use phantom_newtype::Amount; /// /// enum Apples {} /// /// let three_apples = Amount::<Apples, u64>::from(3); /// assert_eq!(9, (three_apples * 3).get()); /// ``` pub fn get(&self) -> Repr { self.0 } } impl<Unit, Repr> Amount<Unit, Repr> { /// `new` is a synonym for `from` that can be evaluated in /// compile time. The main use-case of this functions is defining /// constants. pub const fn new(repr: Repr) -> Amount<Unit, Repr> { Amount(repr, PhantomData) } } impl<Unit: Default, Repr: Copy> Amount<Unit, Repr> { /// Provides a useful shortcut to access units of an amount if /// they implement the `Default` trait: /// /// ``` /// use phantom_newtype::Amount; /// /// #[derive(Debug, Default)] /// struct Seconds; /// let duration = Amount::<Seconds, u64>::from(5); /// /// assert_eq!("5 Seconds", format!("{} {:?}", duration, duration.unit())); /// ``` pub fn unit(&self) -> Unit { Default::default() } } impl<Unit, Repr> Amount<Unit, Repr> where Unit: DisplayerOf<Amount<Unit, Repr>>, { /// `display` provides a machanism to implement a custom display /// for phantom types. /// /// ``` /// use phantom_newtype::{Amount, DisplayerOf}; /// use std::fmt; /// /// struct Cents; /// type Money = Amount<Cents, u64>; /// /// impl DisplayerOf<Money> for Cents { /// fn display(amount: &Money, f: &mut fmt::Formatter<'_>) -> fmt::Result { /// write!(f, "${}.{:02}", amount.get() / 100, amount.get() % 100) /// } /// } /// /// assert_eq!(format!("{}", Money::from(1005).display()), "$10.05"); /// ``` pub fn display(&self) -> DisplayProxy<'_, Self, Unit> { DisplayProxy::new(self) } } impl<Unit, Repr: Copy> From<Repr> for Amount<Unit, Repr> { fn from(repr: Repr) -> Self { Self::new(repr) } } // Note that we only have to write the boilerplate trait // implementation below because default implementations of traits put // unnecessary restrictions on the type parameters. E.g. deriving // `PartialEq<Wrapper<T>>` require `T` to implement `PartialEq`, which // is not what we want: `T` is phantom in our case. impl<Unit, Repr: Copy> Clone for Amount<Unit, Repr> { fn clone(&self) -> Self { Amount(self.0, PhantomData) } } impl<Unit, Repr: Copy> Copy for Amount<Unit, Repr> {} impl<Unit, Repr: PartialEq> PartialEq for Amount<Unit, Repr> { fn eq(&self, rhs: &Self) -> bool { self.0.eq(&rhs.0) } } impl<Unit, Repr: Eq> Eq for Amount<Unit, Repr> {} impl<Unit, Repr: PartialOrd> PartialOrd for Amount<Unit, Repr> { fn partial_cmp(&self, rhs: &Self) -> Option<Ordering> { self.0.partial_cmp(&rhs.0) } } impl<Unit, Repr: Ord> Ord for Amount<Unit, Repr> { fn cmp(&self, rhs: &Self) -> Ordering { self.0.cmp(&rhs.0) } } impl<Unit, Repr: Hash> Hash for Amount<Unit, Repr> { fn hash<H: Hasher>(&self, state: &mut H) { self.0.hash(state) } } impl<Unit, Repr> Add for Amount<Unit, Repr> where Repr: AddAssign + Copy, { type Output = Self; fn add(mut self, rhs: Self) -> Self { self.add_assign(rhs); self } } impl<Unit, Repr> AddAssign for Amount<Unit, Repr> where Repr: AddAssign + Copy, { fn add_assign(&mut self, rhs: Self) { self.0 += rhs.get() } } impl<Unit, Repr> SubAssign for Amount<Unit, Repr> where Repr: SubAssign + Copy, { fn sub_assign(&mut self, rhs: Self) { self.0 -= rhs.get() } } impl<Unit, Repr> Sub for Amount<Unit, Repr> where Repr: SubAssign + Copy, { type Output = Self; fn sub(mut self, rhs: Self) -> Self { self.sub_assign(rhs); self } } impl<Unit, Repr> MulAssign<Repr> for Amount<Unit, Repr> where Repr: MulAssign + Copy, { fn mul_assign(&mut self, rhs: Repr) { self.0 *= rhs; } } impl<Unit, Repr> Mul<Repr> for Amount<Unit, Repr> where Repr: MulAssign + Copy, { type Output = Self; fn mul(mut self, rhs: Repr) -> Self { self.mul_assign(rhs); self } } impl<Unit, Repr> Div<Self> for Amount<Unit, Repr> where Repr: Div<Repr> + Copy, { type Output = <Repr as Div>::Output; fn div(self, rhs: Self) -> Self::Output { self.0.div(rhs.0) } } impl<Unit, Repr> fmt::Debug for Amount<Unit, Repr> where Repr: fmt::Debug, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{:?}", self.0) } } impl<Unit, Repr> fmt::Display for Amount<Unit, Repr> where Repr: fmt::Display, { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}", self.0) } } // Derived serde `impl Serialize` produces an extra `unit` value for // phantom data, e.g. `Amount::<Meters>::from(10)` is serialized // into json as `[10, null]` by default. // // We want serialization format of `Repr` and the `Amount` to match // exactly, that's why we have to provide custom instances. #[cfg(feature="serde")] impl<Unit, Repr: Serialize> Serialize for Amount<Unit, Repr> { fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> { self.0.serialize(serializer) } } #[cfg(feature="serde")] impl<'de, Unit, Repr> Deserialize<'de> for Amount<Unit, Repr> where Repr: Deserialize<'de>, { fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> { Repr::deserialize(deserializer).map(Amount::<Unit, Repr>::new) } }