(index<- ) ./libcore/num/mod.rs
git branch: * master 5200215 auto merge of #14035 : alexcrichton/rust/experimental, r=huonw
modified: Fri May 9 13:02:28 2014
1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! Numeric traits and functions for generic mathematics
12 //!
13 //! These are implemented for the primitive numeric types in `std::{u8, u16,
14 //! u32, u64, uint, i8, i16, i32, i64, int, f32, f64, float}`.
15
16 #![allow(missing_doc)]
17
18 use clone::Clone;
19 use cmp::{Eq, Ord};
20 use kinds::Copy;
21 use mem::size_of;
22 use ops::{Add, Sub, Mul, Div, Rem, Neg};
23 use ops::{Not, BitAnd, BitOr, BitXor, Shl, Shr};
24 use option::{Option, Some, None};
25
26 /// The base trait for numeric types
27 pub trait Num: Eq + Zero + One
28 + Neg<Self>
29 + Add<Self,Self>
30 + Sub<Self,Self>
31 + Mul<Self,Self>
32 + Div<Self,Self>
33 + Rem<Self,Self> {}
34
35 /// Simultaneous division and remainder
36 #[inline]
37 pub fn div_rem<T: Div<T, T> + Rem<T, T>>(x: T, y: T) -> (T, T) {
38 (x / y, x % y)
39 }
40
41 /// Defines an additive identity element for `Self`.
42 ///
43 /// # Deriving
44 ///
45 /// This trait can be automatically be derived using `#[deriving(Zero)]`
46 /// attribute. If you choose to use this, make sure that the laws outlined in
47 /// the documentation for `Zero::zero` still hold.
48 pub trait Zero: Add<Self, Self> {
49 /// Returns the additive identity element of `Self`, `0`.
50 ///
51 /// # Laws
52 ///
53 /// ~~~notrust
54 /// a + 0 = a â a â Self
55 /// 0 + a = a â a â Self
56 /// ~~~
57 ///
58 /// # Purity
59 ///
60 /// This function should return the same result at all times regardless of
61 /// external mutable state, for example values stored in TLS or in
62 /// `static mut`s.
63 // FIXME (#5527): This should be an associated constant
64 fn zero() -> Self;
65
66 /// Returns `true` if `self` is equal to the additive identity.
67 fn is_zero(&self) -> bool;
68 }
69
70 /// Returns the additive identity, `0`.
71 #[inline(always)] pub fn zero<T: Zero>() -> T { Zero::zero() }
72
73 /// Defines a multiplicative identity element for `Self`.
74 pub trait One: Mul<Self, Self> {
75 /// Returns the multiplicative identity element of `Self`, `1`.
76 ///
77 /// # Laws
78 ///
79 /// ~~~notrust
80 /// a * 1 = a â a â Self
81 /// 1 * a = a â a â Self
82 /// ~~~
83 ///
84 /// # Purity
85 ///
86 /// This function should return the same result at all times regardless of
87 /// external mutable state, for example values stored in TLS or in
88 /// `static mut`s.
89 // FIXME (#5527): This should be an associated constant
90 fn one() -> Self;
91 }
92
93 /// Returns the multiplicative identity, `1`.
94 #[inline(always)] pub fn one<T: One>() -> T { One::one() }
95
96 /// Useful functions for signed numbers (i.e. numbers that can be negative).
97 pub trait Signed: Num + Neg<Self> {
98 /// Computes the absolute value.
99 ///
100 /// For float, f32, and f64, `NaN` will be returned if the number is `NaN`.
101 fn abs(&self) -> Self;
102
103 /// The positive difference of two numbers.
104 ///
105 /// Returns `zero` if the number is less than or equal to `other`, otherwise the difference
106 /// between `self` and `other` is returned.
107 fn abs_sub(&self, other: &Self) -> Self;
108
109 /// Returns the sign of the number.
110 ///
111 /// For `float`, `f32`, `f64`:
112 /// * `1.0` if the number is positive, `+0.0` or `INFINITY`
113 /// * `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
114 /// * `NaN` if the number is `NaN`
115 ///
116 /// For `int`:
117 /// * `0` if the number is zero
118 /// * `1` if the number is positive
119 /// * `-1` if the number is negative
120 fn signum(&self) -> Self;
121
122 /// Returns true if the number is positive and false if the number is zero or negative.
123 fn is_positive(&self) -> bool;
124
125 /// Returns true if the number is negative and false if the number is zero or positive.
126 fn is_negative(&self) -> bool;
127 }
128
129 /// Computes the absolute value.
130 ///
131 /// For float, f32, and f64, `NaN` will be returned if the number is `NaN`
132 #[inline(always)]
133 pub fn abs<T: Signed>(value: T) -> T {
134 value.abs()
135 }
136
137 /// The positive difference of two numbers.
138 ///
139 /// Returns `zero` if the number is less than or equal to `other`,
140 /// otherwise the difference between `self` and `other` is returned.
141 #[inline(always)]
142 pub fn abs_sub<T: Signed>(x: T, y: T) -> T {
143 x.abs_sub(&y)
144 }
145
146 /// Returns the sign of the number.
147 ///
148 /// For float, f32, f64:
149 /// - `1.0` if the number is positive, `+0.0` or `INFINITY`
150 /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
151 /// - `NAN` if the number is `NAN`
152 ///
153 /// For int:
154 /// - `0` if the number is zero
155 /// - `1` if the number is positive
156 /// - `-1` if the number is negative
157 #[inline(always)] pub fn signum<T: Signed>(value: T) -> T { value.signum() }
158
159 /// A trait for values which cannot be negative
160 pub trait Unsigned: Num {}
161
162 /// Raises a value to the power of exp, using exponentiation by squaring.
163 ///
164 /// # Example
165 ///
166 /// ```rust
167 /// use std::num;
168 ///
169 /// assert_eq!(num::pow(2, 4), 16);
170 /// ```
171 #[inline]
172 pub fn pow<T: One + Mul<T, T>>(mut base: T, mut exp: uint) -> T {
173 if exp == 1 { base }
174 else {
175 let mut acc = one::<T>();
176 while exp > 0 {
177 if (exp & 1) == 1 {
178 acc = acc * base;
179 }
180 base = base * base;
181 exp = exp >> 1;
182 }
183 acc
184 }
185 }
186
187 /// Numbers which have upper and lower bounds
188 pub trait Bounded {
189 // FIXME (#5527): These should be associated constants
190 /// returns the smallest finite number this type can represent
191 fn min_value() -> Self;
192 /// returns the largest finite number this type can represent
193 fn max_value() -> Self;
194 }
195
196 /// Numbers with a fixed binary representation.
197 pub trait Bitwise: Bounded
198 + Not<Self>
199 + BitAnd<Self,Self>
200 + BitOr<Self,Self>
201 + BitXor<Self,Self>
202 + Shl<Self,Self>
203 + Shr<Self,Self> {
204 /// Returns the number of ones in the binary representation of the number.
205 ///
206 /// # Example
207 ///
208 /// ```rust
209 /// use std::num::Bitwise;
210 ///
211 /// let n = 0b01001100u8;
212 /// assert_eq!(n.count_ones(), 3);
213 /// ```
214 fn count_ones(&self) -> Self;
215
216 /// Returns the number of zeros in the binary representation of the number.
217 ///
218 /// # Example
219 ///
220 /// ```rust
221 /// use std::num::Bitwise;
222 ///
223 /// let n = 0b01001100u8;
224 /// assert_eq!(n.count_zeros(), 5);
225 /// ```
226 #[inline]
227 fn count_zeros(&self) -> Self {
228 (!*self).count_ones()
229 }
230
231 /// Returns the number of leading zeros in the in the binary representation
232 /// of the number.
233 ///
234 /// # Example
235 ///
236 /// ```rust
237 /// use std::num::Bitwise;
238 ///
239 /// let n = 0b0101000u16;
240 /// assert_eq!(n.leading_zeros(), 10);
241 /// ```
242 fn leading_zeros(&self) -> Self;
243
244 /// Returns the number of trailing zeros in the in the binary representation
245 /// of the number.
246 ///
247 /// # Example
248 ///
249 /// ```rust
250 /// use std::num::Bitwise;
251 ///
252 /// let n = 0b0101000u16;
253 /// assert_eq!(n.trailing_zeros(), 3);
254 /// ```
255 fn trailing_zeros(&self) -> Self;
256 }
257
258 /// Specifies the available operations common to all of Rust's core numeric primitives.
259 /// These may not always make sense from a purely mathematical point of view, but
260 /// may be useful for systems programming.
261 pub trait Primitive: Copy
262 + Clone
263 + Num
264 + NumCast
265 + Ord
266 + Bounded {}
267
268 /// A collection of traits relevant to primitive signed and unsigned integers
269 pub trait Int: Primitive
270 + Bitwise
271 + CheckedAdd
272 + CheckedSub
273 + CheckedMul
274 + CheckedDiv {}
275
276 /// Returns the smallest power of 2 greater than or equal to `n`.
277 #[inline]
278 pub fn next_power_of_two<T: Unsigned + Int>(n: T) -> T {
279 let halfbits: T = cast(size_of::<T>() * 4).unwrap();
280 let mut tmp: T = n - one();
281 let mut shift: T = one();
282 while shift <= halfbits {
283 tmp = tmp | (tmp >> shift);
284 shift = shift << one();
285 }
286 tmp + one()
287 }
288
289 // Returns `true` iff `n == 2^k` for some k.
290 #[inline]
291 pub fn is_power_of_two<T: Unsigned + Int>(n: T) -> bool {
292 (n - one()) & n == zero()
293 }
294
295 /// Returns the smallest power of 2 greater than or equal to `n`. If the next
296 /// power of two is greater than the type's maximum value, `None` is returned,
297 /// otherwise the power of 2 is wrapped in `Some`.
298 #[inline]
299 pub fn checked_next_power_of_two<T: Unsigned + Int>(n: T) -> Option<T> {
300 let halfbits: T = cast(size_of::<T>() * 4).unwrap();
301 let mut tmp: T = n - one();
302 let mut shift: T = one();
303 while shift <= halfbits {
304 tmp = tmp | (tmp >> shift);
305 shift = shift << one();
306 }
307 tmp.checked_add(&one())
308 }
309
310 /// A generic trait for converting a value to a number.
311 pub trait ToPrimitive {
312 /// Converts the value of `self` to an `int`.
313 #[inline]
314 fn to_int(&self) -> Option<int> {
315 self.to_i64().and_then(|x| x.to_int())
316 }
317
318 /// Converts the value of `self` to an `i8`.
319 #[inline]
320 fn to_i8(&self) -> Option<i8> {
321 self.to_i64().and_then(|x| x.to_i8())
322 }
323
324 /// Converts the value of `self` to an `i16`.
325 #[inline]
326 fn to_i16(&self) -> Option<i16> {
327 self.to_i64().and_then(|x| x.to_i16())
328 }
329
330 /// Converts the value of `self` to an `i32`.
331 #[inline]
332 fn to_i32(&self) -> Option<i32> {
333 self.to_i64().and_then(|x| x.to_i32())
334 }
335
336 /// Converts the value of `self` to an `i64`.
337 fn to_i64(&self) -> Option<i64>;
338
339 /// Converts the value of `self` to an `uint`.
340 #[inline]
341 fn to_uint(&self) -> Option<uint> {
342 self.to_u64().and_then(|x| x.to_uint())
343 }
344
345 /// Converts the value of `self` to an `u8`.
346 #[inline]
347 fn to_u8(&self) -> Option<u8> {
348 self.to_u64().and_then(|x| x.to_u8())
349 }
350
351 /// Converts the value of `self` to an `u16`.
352 #[inline]
353 fn to_u16(&self) -> Option<u16> {
354 self.to_u64().and_then(|x| x.to_u16())
355 }
356
357 /// Converts the value of `self` to an `u32`.
358 #[inline]
359 fn to_u32(&self) -> Option<u32> {
360 self.to_u64().and_then(|x| x.to_u32())
361 }
362
363 /// Converts the value of `self` to an `u64`.
364 #[inline]
365 fn to_u64(&self) -> Option<u64>;
366
367 /// Converts the value of `self` to an `f32`.
368 #[inline]
369 fn to_f32(&self) -> Option<f32> {
370 self.to_f64().and_then(|x| x.to_f32())
371 }
372
373 /// Converts the value of `self` to an `f64`.
374 #[inline]
375 fn to_f64(&self) -> Option<f64> {
376 self.to_i64().and_then(|x| x.to_f64())
377 }
378 }
379
380 macro_rules! impl_to_primitive_int_to_int(
381 ($SrcT:ty, $DstT:ty) => (
382 {
383 if size_of::<$SrcT>() <= size_of::<$DstT>() {
384 Some(*self as $DstT)
385 } else {
386 let n = *self as i64;
387 let min_value: $DstT = Bounded::min_value();
388 let max_value: $DstT = Bounded::max_value();
389 if min_value as i64 <= n && n <= max_value as i64 {
390 Some(*self as $DstT)
391 } else {
392 None
393 }
394 }
395 }
396 )
397 )
398
399 macro_rules! impl_to_primitive_int_to_uint(
400 ($SrcT:ty, $DstT:ty) => (
401 {
402 let zero: $SrcT = Zero::zero();
403 let max_value: $DstT = Bounded::max_value();
404 if zero <= *self && *self as u64 <= max_value as u64 {
405 Some(*self as $DstT)
406 } else {
407 None
408 }
409 }
410 )
411 )
412
413 macro_rules! impl_to_primitive_int(
414 ($T:ty) => (
415 impl ToPrimitive for $T {
416 #[inline]
417 fn to_int(&self) -> Option<int> { impl_to_primitive_int_to_int!($T, int) }
418 #[inline]
419 fn to_i8(&self) -> Option<i8> { impl_to_primitive_int_to_int!($T, i8) }
420 #[inline]
421 fn to_i16(&self) -> Option<i16> { impl_to_primitive_int_to_int!($T, i16) }
422 #[inline]
423 fn to_i32(&self) -> Option<i32> { impl_to_primitive_int_to_int!($T, i32) }
424 #[inline]
425 fn to_i64(&self) -> Option<i64> { impl_to_primitive_int_to_int!($T, i64) }
426
427 #[inline]
428 fn to_uint(&self) -> Option<uint> { impl_to_primitive_int_to_uint!($T, uint) }
429 #[inline]
430 fn to_u8(&self) -> Option<u8> { impl_to_primitive_int_to_uint!($T, u8) }
431 #[inline]
432 fn to_u16(&self) -> Option<u16> { impl_to_primitive_int_to_uint!($T, u16) }
433 #[inline]
434 fn to_u32(&self) -> Option<u32> { impl_to_primitive_int_to_uint!($T, u32) }
435 #[inline]
436 fn to_u64(&self) -> Option<u64> { impl_to_primitive_int_to_uint!($T, u64) }
437
438 #[inline]
439 fn to_f32(&self) -> Option<f32> { Some(*self as f32) }
440 #[inline]
441 fn to_f64(&self) -> Option<f64> { Some(*self as f64) }
442 }
443 )
444 )
445
446 impl_to_primitive_int!(int)
447 impl_to_primitive_int!(i8)
448 impl_to_primitive_int!(i16)
449 impl_to_primitive_int!(i32)
450 impl_to_primitive_int!(i64)
451
452 macro_rules! impl_to_primitive_uint_to_int(
453 ($DstT:ty) => (
454 {
455 let max_value: $DstT = Bounded::max_value();
456 if *self as u64 <= max_value as u64 {
457 Some(*self as $DstT)
458 } else {
459 None
460 }
461 }
462 )
463 )
464
465 macro_rules! impl_to_primitive_uint_to_uint(
466 ($SrcT:ty, $DstT:ty) => (
467 {
468 if size_of::<$SrcT>() <= size_of::<$DstT>() {
469 Some(*self as $DstT)
470 } else {
471 let zero: $SrcT = Zero::zero();
472 let max_value: $DstT = Bounded::max_value();
473 if zero <= *self && *self as u64 <= max_value as u64 {
474 Some(*self as $DstT)
475 } else {
476 None
477 }
478 }
479 }
480 )
481 )
482
483 macro_rules! impl_to_primitive_uint(
484 ($T:ty) => (
485 impl ToPrimitive for $T {
486 #[inline]
487 fn to_int(&self) -> Option<int> { impl_to_primitive_uint_to_int!(int) }
488 #[inline]
489 fn to_i8(&self) -> Option<i8> { impl_to_primitive_uint_to_int!(i8) }
490 #[inline]
491 fn to_i16(&self) -> Option<i16> { impl_to_primitive_uint_to_int!(i16) }
492 #[inline]
493 fn to_i32(&self) -> Option<i32> { impl_to_primitive_uint_to_int!(i32) }
494 #[inline]
495 fn to_i64(&self) -> Option<i64> { impl_to_primitive_uint_to_int!(i64) }
496
497 #[inline]
498 fn to_uint(&self) -> Option<uint> { impl_to_primitive_uint_to_uint!($T, uint) }
499 #[inline]
500 fn to_u8(&self) -> Option<u8> { impl_to_primitive_uint_to_uint!($T, u8) }
501 #[inline]
502 fn to_u16(&self) -> Option<u16> { impl_to_primitive_uint_to_uint!($T, u16) }
503 #[inline]
504 fn to_u32(&self) -> Option<u32> { impl_to_primitive_uint_to_uint!($T, u32) }
505 #[inline]
506 fn to_u64(&self) -> Option<u64> { impl_to_primitive_uint_to_uint!($T, u64) }
507
508 #[inline]
509 fn to_f32(&self) -> Option<f32> { Some(*self as f32) }
510 #[inline]
511 fn to_f64(&self) -> Option<f64> { Some(*self as f64) }
512 }
513 )
514 )
515
516 impl_to_primitive_uint!(uint)
517 impl_to_primitive_uint!(u8)
518 impl_to_primitive_uint!(u16)
519 impl_to_primitive_uint!(u32)
520 impl_to_primitive_uint!(u64)
521
522 macro_rules! impl_to_primitive_float_to_float(
523 ($SrcT:ty, $DstT:ty) => (
524 if size_of::<$SrcT>() <= size_of::<$DstT>() {
525 Some(*self as $DstT)
526 } else {
527 let n = *self as f64;
528 let max_value: $SrcT = Bounded::max_value();
529 if -max_value as f64 <= n && n <= max_value as f64 {
530 Some(*self as $DstT)
531 } else {
532 None
533 }
534 }
535 )
536 )
537
538 macro_rules! impl_to_primitive_float(
539 ($T:ty) => (
540 impl ToPrimitive for $T {
541 #[inline]
542 fn to_int(&self) -> Option<int> { Some(*self as int) }
543 #[inline]
544 fn to_i8(&self) -> Option<i8> { Some(*self as i8) }
545 #[inline]
546 fn to_i16(&self) -> Option<i16> { Some(*self as i16) }
547 #[inline]
548 fn to_i32(&self) -> Option<i32> { Some(*self as i32) }
549 #[inline]
550 fn to_i64(&self) -> Option<i64> { Some(*self as i64) }
551
552 #[inline]
553 fn to_uint(&self) -> Option<uint> { Some(*self as uint) }
554 #[inline]
555 fn to_u8(&self) -> Option<u8> { Some(*self as u8) }
556 #[inline]
557 fn to_u16(&self) -> Option<u16> { Some(*self as u16) }
558 #[inline]
559 fn to_u32(&self) -> Option<u32> { Some(*self as u32) }
560 #[inline]
561 fn to_u64(&self) -> Option<u64> { Some(*self as u64) }
562
563 #[inline]
564 fn to_f32(&self) -> Option<f32> { impl_to_primitive_float_to_float!($T, f32) }
565 #[inline]
566 fn to_f64(&self) -> Option<f64> { impl_to_primitive_float_to_float!($T, f64) }
567 }
568 )
569 )
570
571 impl_to_primitive_float!(f32)
572 impl_to_primitive_float!(f64)
573
574 /// A generic trait for converting a number to a value.
575 pub trait FromPrimitive {
576 /// Convert an `int` to return an optional value of this type. If the
577 /// value cannot be represented by this value, the `None` is returned.
578 #[inline]
579 fn from_int(n: int) -> Option<Self> {
580 FromPrimitive::from_i64(n as i64)
581 }
582
583 /// Convert an `i8` to return an optional value of this type. If the
584 /// type cannot be represented by this value, the `None` is returned.
585 #[inline]
586 fn from_i8(n: i8) -> Option<Self> {
587 FromPrimitive::from_i64(n as i64)
588 }
589
590 /// Convert an `i16` to return an optional value of this type. If the
591 /// type cannot be represented by this value, the `None` is returned.
592 #[inline]
593 fn from_i16(n: i16) -> Option<Self> {
594 FromPrimitive::from_i64(n as i64)
595 }
596
597 /// Convert an `i32` to return an optional value of this type. If the
598 /// type cannot be represented by this value, the `None` is returned.
599 #[inline]
600 fn from_i32(n: i32) -> Option<Self> {
601 FromPrimitive::from_i64(n as i64)
602 }
603
604 /// Convert an `i64` to return an optional value of this type. If the
605 /// type cannot be represented by this value, the `None` is returned.
606 fn from_i64(n: i64) -> Option<Self>;
607
608 /// Convert an `uint` to return an optional value of this type. If the
609 /// type cannot be represented by this value, the `None` is returned.
610 #[inline]
611 fn from_uint(n: uint) -> Option<Self> {
612 FromPrimitive::from_u64(n as u64)
613 }
614
615 /// Convert an `u8` to return an optional value of this type. If the
616 /// type cannot be represented by this value, the `None` is returned.
617 #[inline]
618 fn from_u8(n: u8) -> Option<Self> {
619 FromPrimitive::from_u64(n as u64)
620 }
621
622 /// Convert an `u16` to return an optional value of this type. If the
623 /// type cannot be represented by this value, the `None` is returned.
624 #[inline]
625 fn from_u16(n: u16) -> Option<Self> {
626 FromPrimitive::from_u64(n as u64)
627 }
628
629 /// Convert an `u32` to return an optional value of this type. If the
630 /// type cannot be represented by this value, the `None` is returned.
631 #[inline]
632 fn from_u32(n: u32) -> Option<Self> {
633 FromPrimitive::from_u64(n as u64)
634 }
635
636 /// Convert an `u64` to return an optional value of this type. If the
637 /// type cannot be represented by this value, the `None` is returned.
638 fn from_u64(n: u64) -> Option<Self>;
639
640 /// Convert a `f32` to return an optional value of this type. If the
641 /// type cannot be represented by this value, the `None` is returned.
642 #[inline]
643 fn from_f32(n: f32) -> Option<Self> {
644 FromPrimitive::from_f64(n as f64)
645 }
646
647 /// Convert a `f64` to return an optional value of this type. If the
648 /// type cannot be represented by this value, the `None` is returned.
649 #[inline]
650 fn from_f64(n: f64) -> Option<Self> {
651 FromPrimitive::from_i64(n as i64)
652 }
653 }
654
655 /// A utility function that just calls `FromPrimitive::from_int`.
656 pub fn from_int<A: FromPrimitive>(n: int) -> Option<A> {
657 FromPrimitive::from_int(n)
658 }
659
660 /// A utility function that just calls `FromPrimitive::from_i8`.
661 pub fn from_i8<A: FromPrimitive>(n: i8) -> Option<A> {
662 FromPrimitive::from_i8(n)
663 }
664
665 /// A utility function that just calls `FromPrimitive::from_i16`.
666 pub fn from_i16<A: FromPrimitive>(n: i16) -> Option<A> {
667 FromPrimitive::from_i16(n)
668 }
669
670 /// A utility function that just calls `FromPrimitive::from_i32`.
671 pub fn from_i32<A: FromPrimitive>(n: i32) -> Option<A> {
672 FromPrimitive::from_i32(n)
673 }
674
675 /// A utility function that just calls `FromPrimitive::from_i64`.
676 pub fn from_i64<A: FromPrimitive>(n: i64) -> Option<A> {
677 FromPrimitive::from_i64(n)
678 }
679
680 /// A utility function that just calls `FromPrimitive::from_uint`.
681 pub fn from_uint<A: FromPrimitive>(n: uint) -> Option<A> {
682 FromPrimitive::from_uint(n)
683 }
684
685 /// A utility function that just calls `FromPrimitive::from_u8`.
686 pub fn from_u8<A: FromPrimitive>(n: u8) -> Option<A> {
687 FromPrimitive::from_u8(n)
688 }
689
690 /// A utility function that just calls `FromPrimitive::from_u16`.
691 pub fn from_u16<A: FromPrimitive>(n: u16) -> Option<A> {
692 FromPrimitive::from_u16(n)
693 }
694
695 /// A utility function that just calls `FromPrimitive::from_u32`.
696 pub fn from_u32<A: FromPrimitive>(n: u32) -> Option<A> {
697 FromPrimitive::from_u32(n)
698 }
699
700 /// A utility function that just calls `FromPrimitive::from_u64`.
701 pub fn from_u64<A: FromPrimitive>(n: u64) -> Option<A> {
702 FromPrimitive::from_u64(n)
703 }
704
705 /// A utility function that just calls `FromPrimitive::from_f32`.
706 pub fn from_f32<A: FromPrimitive>(n: f32) -> Option<A> {
707 FromPrimitive::from_f32(n)
708 }
709
710 /// A utility function that just calls `FromPrimitive::from_f64`.
711 pub fn from_f64<A: FromPrimitive>(n: f64) -> Option<A> {
712 FromPrimitive::from_f64(n)
713 }
714
715 macro_rules! impl_from_primitive(
716 ($T:ty, $to_ty:expr) => (
717 impl FromPrimitive for $T {
718 #[inline] fn from_int(n: int) -> Option<$T> { $to_ty }
719 #[inline] fn from_i8(n: i8) -> Option<$T> { $to_ty }
720 #[inline] fn from_i16(n: i16) -> Option<$T> { $to_ty }
721 #[inline] fn from_i32(n: i32) -> Option<$T> { $to_ty }
722 #[inline] fn from_i64(n: i64) -> Option<$T> { $to_ty }
723
724 #[inline] fn from_uint(n: uint) -> Option<$T> { $to_ty }
725 #[inline] fn from_u8(n: u8) -> Option<$T> { $to_ty }
726 #[inline] fn from_u16(n: u16) -> Option<$T> { $to_ty }
727 #[inline] fn from_u32(n: u32) -> Option<$T> { $to_ty }
728 #[inline] fn from_u64(n: u64) -> Option<$T> { $to_ty }
729
730 #[inline] fn from_f32(n: f32) -> Option<$T> { $to_ty }
731 #[inline] fn from_f64(n: f64) -> Option<$T> { $to_ty }
732 }
733 )
734 )
735
736 impl_from_primitive!(int, n.to_int())
737 impl_from_primitive!(i8, n.to_i8())
738 impl_from_primitive!(i16, n.to_i16())
739 impl_from_primitive!(i32, n.to_i32())
740 impl_from_primitive!(i64, n.to_i64())
741 impl_from_primitive!(uint, n.to_uint())
742 impl_from_primitive!(u8, n.to_u8())
743 impl_from_primitive!(u16, n.to_u16())
744 impl_from_primitive!(u32, n.to_u32())
745 impl_from_primitive!(u64, n.to_u64())
746 impl_from_primitive!(f32, n.to_f32())
747 impl_from_primitive!(f64, n.to_f64())
748
749 /// Cast from one machine scalar to another.
750 ///
751 /// # Example
752 ///
753 /// ```
754 /// use std::num;
755 ///
756 /// let twenty: f32 = num::cast(0x14).unwrap();
757 /// assert_eq!(twenty, 20f32);
758 /// ```
759 ///
760 #[inline]
761 pub fn cast<T: NumCast,U: NumCast>(n: T) -> Option<U> {
762 NumCast::from(n)
763 }
764
765 /// An interface for casting between machine scalars.
766 pub trait NumCast: ToPrimitive {
767 /// Creates a number from another value that can be converted into a primitive via the
768 /// `ToPrimitive` trait.
769 fn from<T: ToPrimitive>(n: T) -> Option<Self>;
770 }
771
772 macro_rules! impl_num_cast(
773 ($T:ty, $conv:ident) => (
774 impl NumCast for $T {
775 #[inline]
776 fn from<N: ToPrimitive>(n: N) -> Option<$T> {
777 // `$conv` could be generated using `concat_idents!`, but that
778 // macro seems to be broken at the moment
779 n.$conv()
780 }
781 }
782 )
783 )
784
785 impl_num_cast!(u8, to_u8)
786 impl_num_cast!(u16, to_u16)
787 impl_num_cast!(u32, to_u32)
788 impl_num_cast!(u64, to_u64)
789 impl_num_cast!(uint, to_uint)
790 impl_num_cast!(i8, to_i8)
791 impl_num_cast!(i16, to_i16)
792 impl_num_cast!(i32, to_i32)
793 impl_num_cast!(i64, to_i64)
794 impl_num_cast!(int, to_int)
795 impl_num_cast!(f32, to_f32)
796 impl_num_cast!(f64, to_f64)
797
798 /// Saturating math operations
799 pub trait Saturating {
800 /// Saturating addition operator.
801 /// Returns a+b, saturating at the numeric bounds instead of overflowing.
802 fn saturating_add(self, v: Self) -> Self;
803
804 /// Saturating subtraction operator.
805 /// Returns a-b, saturating at the numeric bounds instead of overflowing.
806 fn saturating_sub(self, v: Self) -> Self;
807 }
808
809 impl<T: CheckedAdd + CheckedSub + Zero + Ord + Bounded> Saturating for T {
810 #[inline]
811 fn saturating_add(self, v: T) -> T {
812 match self.checked_add(&v) {
813 Some(x) => x,
814 None => if v >= Zero::zero() {
815 Bounded::max_value()
816 } else {
817 Bounded::min_value()
818 }
819 }
820 }
821
822 #[inline]
823 fn saturating_sub(self, v: T) -> T {
824 match self.checked_sub(&v) {
825 Some(x) => x,
826 None => if v >= Zero::zero() {
827 Bounded::min_value()
828 } else {
829 Bounded::max_value()
830 }
831 }
832 }
833 }
834
835 /// Performs addition that returns `None` instead of wrapping around on overflow.
836 pub trait CheckedAdd: Add<Self, Self> {
837 /// Adds two numbers, checking for overflow. If overflow happens, `None` is returned.
838 fn checked_add(&self, v: &Self) -> Option<Self>;
839 }
840
841 /// Performs subtraction that returns `None` instead of wrapping around on underflow.
842 pub trait CheckedSub: Sub<Self, Self> {
843 /// Subtracts two numbers, checking for underflow. If underflow happens, `None` is returned.
844 fn checked_sub(&self, v: &Self) -> Option<Self>;
845 }
846
847 /// Performs multiplication that returns `None` instead of wrapping around on underflow or
848 /// overflow.
849 pub trait CheckedMul: Mul<Self, Self> {
850 /// Multiplies two numbers, checking for underflow or overflow. If underflow or overflow
851 /// happens, `None` is returned.
852 fn checked_mul(&self, v: &Self) -> Option<Self>;
853 }
854
855 /// Performs division that returns `None` instead of wrapping around on underflow or overflow.
856 pub trait CheckedDiv: Div<Self, Self> {
857 /// Divides two numbers, checking for underflow or overflow. If underflow or overflow happens,
858 /// `None` is returned.
859 fn checked_div(&self, v: &Self) -> Option<Self>;
860 }
861
862 /// Helper function for testing numeric operations
863 #[cfg(test)]
864 pub fn test_num<T:Num + NumCast + ::std::fmt::Show>(ten: T, two: T) {
865 assert_eq!(ten.add(&two), cast(12).unwrap());
866 assert_eq!(ten.sub(&two), cast(8).unwrap());
867 assert_eq!(ten.mul(&two), cast(20).unwrap());
868 assert_eq!(ten.div(&two), cast(5).unwrap());
869 assert_eq!(ten.rem(&two), cast(0).unwrap());
870
871 assert_eq!(ten.add(&two), ten + two);
872 assert_eq!(ten.sub(&two), ten - two);
873 assert_eq!(ten.mul(&two), ten * two);
874 assert_eq!(ten.div(&two), ten / two);
875 assert_eq!(ten.rem(&two), ten % two);
876 }
libcore/num/mod.rs:196:48-196:48 -trait- definition:
/// Numbers with a fixed binary representation.
pub trait Bitwise: Bounded
+ Not<Self>
references:- 26198: + Not<Self>
199: + BitAnd<Self,Self>
200: + BitOr<Self,Self>
--
202: + Shl<Self,Self>
203: + Shr<Self,Self> {
204: /// Returns the number of ones in the binary representation of the number.
--
213: /// ```
214: fn count_ones(&self) -> Self;
--
254: /// ```
255: fn trailing_zeros(&self) -> Self;
256: }
--
269: pub trait Int: Primitive
270: + Bitwise
271: + CheckedAdd
libcore/num/uint_macros.rs:
141: impl Bitwise for $T {
142: /// Returns the number of ones in the binary representation of the number.
libcore/num/int.rs:
47: impl Bitwise for int {
48: /// Returns the number of ones in the binary representation of the number.
libcore/num/i8.rs:
28: impl Bitwise for i8 {
29: /// Returns the number of ones in the binary representation of the number.
libcore/num/i16.rs:
28: impl Bitwise for i16 {
29: /// Returns the number of ones in the binary representation of the number.
libcore/num/i32.rs:
28: impl Bitwise for i32 {
29: /// Returns the number of ones in the binary representation of the number.
libcore/num/i64.rs:
28: impl Bitwise for i64 {
29: /// Returns the number of ones in the binary representation of the number.
libcore/num/uint_macros.rs:
141: impl Bitwise for $T {
142: /// Returns the number of ones in the binary representation of the number.
libcore/num/mod.rs:47:51-47:51 -trait- definition:
/// the documentation for `Zero::zero` still hold.
pub trait Zero: Add<Self, Self> {
/// Returns the additive identity element of `Self`, `0`.
references:- 2163: // FIXME (#5527): This should be an associated constant
64: fn zero() -> Self;
--
70: /// Returns the additive identity, `0`.
--
809: impl<T: CheckedAdd + CheckedSub + Zero + Ord + Bounded> Saturating for T {
810: #[inline]
libcore/iter.rs:
2084: pub fn range_step<A: CheckedAdd + Ord + Clone + Zero>(start: A, stop: A, step: A) -> RangeStep<A> {
2085: let rev = step < Zero::zero();
--
2117: pub fn range_step_inclusive<A: CheckedAdd + Ord + Clone + Zero>(start: A, stop: A,
2118: step: A) -> RangeStepInclusive<A> {
libcore/num/int_macros.rs:
55: impl Zero for $T {
56: #[inline]
libcore/num/uint_macros.rs:
46: impl Zero for $T {
47: #[inline]
libcore/num/f64.rs:
136: impl Zero for f64 {
137: #[inline]
libcore/num/f32.rs:
130: impl Zero for f32 {
131: #[inline]
libcore/num/mod.rs:26:37-26:37 -trait- definition:
/// The base trait for numeric types
pub trait Num: Eq + Zero + One
+ Neg<Self>
references:- 2631: + Mul<Self,Self>
32: + Div<Self,Self>
33: + Rem<Self,Self> {}
--
159: /// A trait for values which cannot be negative
160: pub trait Unsigned: Num {}
--
262: + Clone
263: + Num
264: + NumCast
libcore/num/int_macros.rs:
53: impl Num for $T {}
libcore/num/uint_macros.rs:
44: impl Num for $T {}
libcore/num/f32.rs:
121: impl Num for f32 {}
libcore/num/f64.rs:
134: impl Num for f64 {}
libcore/num/uint_macros.rs:
44: impl Num for $T {}
libcore/num/mod.rs:260:43-260:43 -trait- definition:
/// may be useful for systems programming.
pub trait Primitive: Copy
+ Clone
references:- 13268: /// A collection of traits relevant to primitive signed and unsigned integers
269: pub trait Int: Primitive
270: + Bitwise
libcore/num/int_macros.rs:
243: impl Primitive for $T {}
libcore/num/uint_macros.rs:
176: impl Primitive for $T {}
libcore/num/f64.rs:
132: impl Primitive for f64 {}
libcore/num/f32.rs:
128: impl Primitive for f32 {}
libcore/num/mod.rs:96:77-96:77 -trait- definition:
/// Useful functions for signed numbers (i.e. numbers that can be negative).
pub trait Signed: Num + Neg<Self> {
/// Computes the absolute value.
references:- 15100: /// For float, f32, and f64, `NaN` will be returned if the number is `NaN`.
101: fn abs(&self) -> Self;
--
106: /// between `self` and `other` is returned.
107: fn abs_sub(&self, other: &Self) -> Self;
--
133: pub fn abs<T: Signed>(value: T) -> T {
134: value.abs()
--
142: pub fn abs_sub<T: Signed>(x: T, y: T) -> T {
143: x.abs_sub(&y)
--
156: /// - `-1` if the number is negative
libcore/num/int_macros.rs:
140: impl Signed for $T {
141: /// Computes the absolute value
libcore/num/f32.rs:
183: impl Signed for f32 {
184: /// Computes the absolute value. Returns `NAN` if the number is `NAN`.
libcore/num/f64.rs:
184: impl Signed for f64 {
185: /// Computes the absolute value. Returns `NAN` if the number is `NAN`.
libcore/num/mod.rs:
96: /// Useful functions for signed numbers (i.e. numbers that can be negative).
97: pub trait Signed: Num + Neg<Self> {
98: /// Computes the absolute value.
libcore/num/mod.rs:187:46-187:46 -trait- definition:
/// Numbers which have upper and lower bounds
pub trait Bounded {
// FIXME (#5527): These should be associated constants
references:- 17265: + Ord
266: + Bounded {}
--
809: impl<T: CheckedAdd + CheckedSub + Zero + Ord + Bounded> Saturating for T {
810: #[inline]
libcore/num/int_macros.rs:
217: impl Bounded for $T {
218: #[inline]
libcore/num/uint_macros.rs:
133: impl Bounded for $T {
134: #[inline]
libcore/num/f32.rs:
220: impl Bounded for f32 {
221: // NOTE: this is the smallest non-infinite f32 value, *not* MIN_VALUE
libcore/num/f64.rs:
220: impl Bounded for f64 {
221: // NOTE: this is the smallest non-infinite f32 value, *not* MIN_VALUE
libcore/num/uint_macros.rs:
133: impl Bounded for $T {
134: #[inline]
libcore/num/mod.rs:798:31-798:31 -trait- definition:
/// Saturating math operations
pub trait Saturating {
/// Saturating addition operator.
references:- 5805: /// Returns a-b, saturating at the numeric bounds instead of overflowing.
806: fn saturating_sub(self, v: Self) -> Self;
807: }
809: impl<T: CheckedAdd + CheckedSub + Zero + Ord + Bounded> Saturating for T {
810: #[inline]
libcore/num/mod.rs:159:48-159:48 -trait- definition:
/// A trait for values which cannot be negative
pub trait Unsigned: Num {}
/// Raises a value to the power of exp, using exponentiation by squaring.
references:- 8291: pub fn is_power_of_two<T: Unsigned + Int>(n: T) -> bool {
292: (n - one()) & n == zero()
--
299: pub fn checked_next_power_of_two<T: Unsigned + Int>(n: T) -> Option<T> {
300: let halfbits: T = cast(size_of::<T>() * 4).unwrap();
libcore/num/uint_macros.rs:
95: impl Unsigned for $T {}
libcore/num/mod.rs:835:82-835:82 -trait- definition:
/// Performs addition that returns `None` instead of wrapping around on overflow.
pub trait CheckedAdd: Add<Self, Self> {
/// Adds two numbers, checking for overflow. If overflow happens, `None` is returned.
references:- 20270: + Bitwise
271: + CheckedAdd
272: + CheckedSub
--
809: impl<T: CheckedAdd + CheckedSub + Zero + Ord + Bounded> Saturating for T {
810: #[inline]
--
835: /// Performs addition that returns `None` instead of wrapping around on overflow.
836: pub trait CheckedAdd: Add<Self, Self> {
837: /// Adds two numbers, checking for overflow. If overflow happens, `None` is returned.
838: fn checked_add(&self, v: &Self) -> Option<Self>;
839: }
libcore/iter.rs:
2123: impl<A: CheckedAdd + Ord + Clone + Eq> Iterator<A> for RangeStepInclusive<A> {
2124: #[inline]
libcore/num/int.rs:
75: impl CheckedAdd for int {
76: #[inline]
libcore/num/i8.rs:
44: impl CheckedAdd for i8 {
45: #[inline]
libcore/num/i16.rs:
44: impl CheckedAdd for i16 {
45: #[inline]
libcore/num/i32.rs:
44: impl CheckedAdd for i32 {
45: #[inline]
libcore/num/i64.rs:
43: impl CheckedAdd for i64 {
44: #[inline]
libcore/num/uint.rs:
40: impl CheckedAdd for uint {
41: #[inline]
libcore/num/u8.rs:
28: impl CheckedAdd for u8 {
29: #[inline]
libcore/num/u16.rs:
28: impl CheckedAdd for u16 {
29: #[inline]
libcore/num/u32.rs:
28: impl CheckedAdd for u32 {
29: #[inline]
libcore/num/u64.rs:
28: impl CheckedAdd for u64 {
29: #[inline]
libcore/num/mod.rs:
835: /// Performs addition that returns `None` instead of wrapping around on overflow.
836: pub trait CheckedAdd: Add<Self, Self> {
837: /// Adds two numbers, checking for overflow. If overflow happens, `None` is returned.
libcore/num/mod.rs:574:56-574:56 -trait- definition:
/// A generic trait for converting a number to a value.
pub trait FromPrimitive {
/// Convert an `int` to return an optional value of this type. If the
references:- 36libcore/num/mod.rs:73:58-73:58 -trait- definition:
/// Defines a multiplicative identity element for `Self`.
pub trait One: Mul<Self, Self> {
/// Returns the multiplicative identity element of `Self`, `1`.
references:- 2173: /// Defines a multiplicative identity element for `Self`.
74: pub trait One: Mul<Self, Self> {
75: /// Returns the multiplicative identity element of `Self`, `1`.
--
93: /// Returns the multiplicative identity, `1`.
--
172: pub fn pow<T: One + Mul<T, T>>(mut base: T, mut exp: uint) -> T {
173: if exp == 1 { base }
libcore/iter.rs:
2019: pub fn range_inclusive<A: Add<A, A> + Ord + Clone + One + ToPrimitive>(start: A, stop: A)
2020: -> RangeInclusive<A> {
libcore/num/int_macros.rs:
63: impl One for $T {
64: #[inline]
libcore/num/uint_macros.rs:
54: impl One for $T {
55: #[inline]
libcore/num/f32.rs:
139: impl One for f32 {
140: #[inline]
libcore/num/f64.rs:
145: impl One for f64 {
146: #[inline]
libcore/num/uint_macros.rs:
54: impl One for $T {
55: #[inline]
libcore/num/mod.rs:848:14-848:14 -trait- definition:
/// overflow.
pub trait CheckedMul: Mul<Self, Self> {
/// Multiplies two numbers, checking for underflow or overflow. If underflow or overflow
references:- 15848: /// overflow.
849: pub trait CheckedMul: Mul<Self, Self> {
850: /// Multiplies two numbers, checking for underflow or overflow. If underflow or overflow
851: /// happens, `None` is returned.
852: fn checked_mul(&self, v: &Self) -> Option<Self>;
853: }
libcore/num/int.rs:
119: impl CheckedMul for int {
120: #[inline]
libcore/num/i16.rs:
64: impl CheckedMul for i16 {
65: #[inline]
libcore/num/i32.rs:
64: impl CheckedMul for i32 {
65: #[inline]
libcore/num/i64.rs:
63: impl CheckedMul for i64 {
64: #[inline]
libcore/num/uint.rs:
84: impl CheckedMul for uint {
85: #[inline]
libcore/num/u8.rs:
48: impl CheckedMul for u8 {
49: #[inline]
libcore/num/u16.rs:
48: impl CheckedMul for u16 {
49: #[inline]
libcore/num/u32.rs:
48: impl CheckedMul for u32 {
49: #[inline]
libcore/num/u64.rs:
48: impl CheckedMul for u64 {
49: #[inline]
libcore/num/i8.rs:
64: impl CheckedMul for i8 {
65: #[inline]
libcore/num/mod.rs:268:78-268:78 -trait- definition:
/// A collection of traits relevant to primitive signed and unsigned integers
pub trait Int: Primitive
+ Bitwise
references:- 16291: pub fn is_power_of_two<T: Unsigned + Int>(n: T) -> bool {
292: (n - one()) & n == zero()
--
299: pub fn checked_next_power_of_two<T: Unsigned + Int>(n: T) -> Option<T> {
300: let halfbits: T = cast(size_of::<T>() * 4).unwrap();
libcore/bool.rs:
47: pub fn to_bit<N: Int>(p: bool) -> N {
48: if p { one() } else { zero() }
libcore/iter.rs:
2056: impl<A: Sub<A, A> + Int + Ord + Clone + ToPrimitive> DoubleEndedIterator<A>
2057: for RangeInclusive<A> {
libcore/num/int_macros.rs:
241: impl Int for $T {}
libcore/num/uint_macros.rs:
174: impl Int for $T {}
libcore/num/mod.rs:760:10-760:10 -fn- definition:
pub fn cast<T: NumCast,U: NumCast>(n: T) -> Option<U> {
NumCast::from(n)
}
references:- 2299: pub fn checked_next_power_of_two<T: Unsigned + Int>(n: T) -> Option<T> {
300: let halfbits: T = cast(size_of::<T>() * 4).unwrap();
301: let mut tmp: T = n - one();
libcore/num/mod.rs:855:95-855:95 -trait- definition:
/// Performs division that returns `None` instead of wrapping around on underflow or overflow.
pub trait CheckedDiv: Div<Self, Self> {
/// Divides two numbers, checking for underflow or overflow. If underflow or overflow happens,
references:- 15273: + CheckedMul
274: + CheckedDiv {}
--
855: /// Performs division that returns `None` instead of wrapping around on underflow or overflow.
856: pub trait CheckedDiv: Div<Self, Self> {
857: /// Divides two numbers, checking for underflow or overflow. If underflow or overflow happens,
858: /// `None` is returned.
859: fn checked_div(&self, v: &Self) -> Option<Self>;
860: }
libcore/num/int_macros.rs:
225: impl CheckedDiv for $T {
226: #[inline]
libcore/num/uint_macros.rs:
163: impl CheckedDiv for $T {
164: #[inline]
libcore/num/int_macros.rs:
225: impl CheckedDiv for $T {
226: #[inline]
libcore/num/mod.rs:310:56-310:56 -trait- definition:
/// A generic trait for converting a value to a number.
pub trait ToPrimitive {
/// Converts the value of `self` to an `int`.
references:- 31libcore/iter.rs:
libcore/num/mod.rs:
libcore/num/mod.rs:36:10-36:10 -fn- definition:
pub fn div_rem<T: Div<T, T> + Rem<T, T>>(x: T, y: T) -> (T, T) {
(x / y, x % y)
}
references:- 2libcore/slice.rs:
1452: } else {
1453: let (n, rem) = div_rem(self.v.len(), self.chunk_size);
1454: let n = if rem > 0 { n + 1 } else { n };
libcore/num/mod.rs:94:18-94:18 -fn- definition:
/// Useful functions for signed numbers (i.e. numbers that can be negative).
pub trait Signed: Num + Neg<Self> {
/// Computes the absolute value.
references:- 11301: let mut tmp: T = n - one();
302: let mut shift: T = one();
303: while shift <= halfbits {
--
306: }
307: tmp.checked_add(&one())
308: }
libcore/bool.rs:
47: pub fn to_bit<N: Int>(p: bool) -> N {
48: if p { one() } else { zero() }
49: }
libcore/num/mod.rs:
291: pub fn is_power_of_two<T: Unsigned + Int>(n: T) -> bool {
292: (n - one()) & n == zero()
293: }
libcore/num/mod.rs:841:86-841:86 -trait- definition:
/// Performs subtraction that returns `None` instead of wrapping around on underflow.
pub trait CheckedSub: Sub<Self, Self> {
/// Subtracts two numbers, checking for underflow. If underflow happens, `None` is returned.
references:- 16841: /// Performs subtraction that returns `None` instead of wrapping around on underflow.
842: pub trait CheckedSub: Sub<Self, Self> {
843: /// Subtracts two numbers, checking for underflow. If underflow happens, `None` is returned.
844: fn checked_sub(&self, v: &Self) -> Option<Self>;
845: }
libcore/num/int.rs:
97: impl CheckedSub for int {
98: #[inline]
libcore/num/i16.rs:
54: impl CheckedSub for i16 {
55: #[inline]
libcore/num/i32.rs:
54: impl CheckedSub for i32 {
55: #[inline]
libcore/num/i64.rs:
53: impl CheckedSub for i64 {
54: #[inline]
libcore/num/uint.rs:
62: impl CheckedSub for uint {
63: #[inline]
libcore/num/u8.rs:
38: impl CheckedSub for u8 {
39: #[inline]
libcore/num/u16.rs:
38: impl CheckedSub for u16 {
39: #[inline]
libcore/num/u32.rs:
38: impl CheckedSub for u32 {
39: #[inline]
libcore/num/u64.rs:
38: impl CheckedSub for u64 {
39: #[inline]
libcore/num/i8.rs:
54: impl CheckedSub for i8 {
55: #[inline]
libcore/num/mod.rs:71:18-71:18 -fn- definition:
/// Defines a multiplicative identity element for `Self`.
pub trait One: Mul<Self, Self> {
/// Returns the multiplicative identity element of `Self`, `1`.
references:- 2291: pub fn is_power_of_two<T: Unsigned + Int>(n: T) -> bool {
292: (n - one()) & n == zero()
293: }
libcore/bool.rs:
47: pub fn to_bit<N: Int>(p: bool) -> N {
48: if p { one() } else { zero() }
49: }
libcore/num/mod.rs:765:54-765:54 -trait- definition:
/// An interface for casting between machine scalars.
pub trait NumCast: ToPrimitive {
/// Creates a number from another value that can be converted into a primitive via the
references:- 16773: ($T:ty, $conv:ident) => (
774: impl NumCast for $T {
775: #[inline]