Trait rust_rpg_toolkit::prelude::Add 1.0.0[−][src]
Expand description
The addition operator +
.
Note that Rhs
is Self
by default, but this is not mandatory. For
example, std::time::SystemTime
implements Add<Duration>
, which permits
operations of the form SystemTime = SystemTime + Duration
.
Examples
Add
able points
use std::ops::Add;
#[derive(Debug, Copy, Clone, PartialEq)]
struct Point {
x: i32,
y: i32,
}
impl Add for Point {
type Output = Self;
fn add(self, other: Self) -> Self {
Self {
x: self.x + other.x,
y: self.y + other.y,
}
}
}
assert_eq!(Point { x: 1, y: 0 } + Point { x: 2, y: 3 },
Point { x: 3, y: 3 });
Implementing Add
with generics
Here is an example of the same Point
struct implementing the Add
trait
using generics.
use std::ops::Add;
#[derive(Debug, Copy, Clone, PartialEq)]
struct Point<T> {
x: T,
y: T,
}
// Notice that the implementation uses the associated type `Output`.
impl<T: Add<Output = T>> Add for Point<T> {
type Output = Self;
fn add(self, other: Self) -> Self::Output {
Self {
x: self.x + other.x,
y: self.y + other.y,
}
}
}
assert_eq!(Point { x: 1, y: 0 } + Point { x: 2, y: 3 },
Point { x: 3, y: 3 });
Associated Types
Required methods
Implementations on Foreign Types
Panics
This function may panic if the resulting point in time cannot be represented by the
underlying data structure. See SystemTime::checked_add
for a version without panic.
type Output = SystemTime
type Output = <Saturating<i16> as Add<Saturating<i16>>>::Output
type Output = <Saturating<i64> as Add<Saturating<i64>>>::Output
pub fn add(
self,
other: &Saturating<i64>
) -> <Saturating<i64> as Add<Saturating<i64>>>::Output
type Output = Saturating<i128>
type Output = <Saturating<i8> as Add<Saturating<i8>>>::Output
type Output = <Saturating<i128> as Add<Saturating<i128>>>::Output
pub fn add(
self,
other: &Saturating<i128>
) -> <Saturating<i128> as Add<Saturating<i128>>>::Output
type Output = <Saturating<isize> as Add<Saturating<isize>>>::Output
pub fn add(
self,
other: &Saturating<isize>
) -> <Saturating<isize> as Add<Saturating<isize>>>::Output
type Output = <Saturating<u64> as Add<Saturating<u64>>>::Output
type Output = <Saturating<u8> as Add<Saturating<u8>>>::Output
type Output = <Saturating<i8> as Add<Saturating<i8>>>::Output
type Output = <Saturating<i128> as Add<Saturating<i128>>>::Output
pub fn add(
self,
other: &Saturating<i128>
) -> <Saturating<i128> as Add<Saturating<i128>>>::Output
type Output = Saturating<isize>
type Output = Saturating<i32>
type Output = <Saturating<usize> as Add<Saturating<usize>>>::Output
pub fn add(
self,
other: &Saturating<usize>
) -> <Saturating<usize> as Add<Saturating<usize>>>::Output
type Output = <Saturating<u16> as Add<Saturating<u16>>>::Output
type Output = <Saturating<u64> as Add<Saturating<u64>>>::Output
pub fn add(
self,
other: &Saturating<u64>
) -> <Saturating<u64> as Add<Saturating<u64>>>::Output
type Output = <Saturating<u32> as Add<Saturating<u32>>>::Output
pub fn add(
self,
other: &Saturating<u32>
) -> <Saturating<u32> as Add<Saturating<u32>>>::Output
type Output = <Saturating<u32> as Add<Saturating<u32>>>::Output
type Output = Saturating<u32>
type Output = Saturating<usize>
type Output = <Saturating<isize> as Add<Saturating<isize>>>::Output
pub fn add(
self,
other: &Saturating<isize>
) -> <Saturating<isize> as Add<Saturating<isize>>>::Output
type Output = <Saturating<u16> as Add<Saturating<u16>>>::Output
pub fn add(
self,
other: &Saturating<u16>
) -> <Saturating<u16> as Add<Saturating<u16>>>::Output
type Output = <Saturating<usize> as Add<Saturating<usize>>>::Output
pub fn add(
self,
other: &Saturating<usize>
) -> <Saturating<usize> as Add<Saturating<usize>>>::Output
type Output = <Saturating<i128> as Add<Saturating<i128>>>::Output
pub fn add(
self,
other: Saturating<i128>
) -> <Saturating<i128> as Add<Saturating<i128>>>::Output
type Output = <Saturating<i16> as Add<Saturating<i16>>>::Output
pub fn add(
self,
other: &Saturating<i16>
) -> <Saturating<i16> as Add<Saturating<i16>>>::Output
type Output = <Saturating<u8> as Add<Saturating<u8>>>::Output
type Output = <Saturating<i64> as Add<Saturating<i64>>>::Output
pub fn add(
self,
other: &Saturating<i64>
) -> <Saturating<i64> as Add<Saturating<i64>>>::Output
type Output = <Saturating<u128> as Add<Saturating<u128>>>::Output
pub fn add(
self,
other: Saturating<u128>
) -> <Saturating<u128> as Add<Saturating<u128>>>::Output
type Output = <Saturating<isize> as Add<Saturating<isize>>>::Output
pub fn add(
self,
other: Saturating<isize>
) -> <Saturating<isize> as Add<Saturating<isize>>>::Output
type Output = <Saturating<i32> as Add<Saturating<i32>>>::Output
pub fn add(
self,
other: &Saturating<i32>
) -> <Saturating<i32> as Add<Saturating<i32>>>::Output
type Output = Saturating<u64>
type Output = <Saturating<i8> as Add<Saturating<i8>>>::Output
type Output = Saturating<i16>
type Output = <Saturating<u128> as Add<Saturating<u128>>>::Output
pub fn add(
self,
other: &Saturating<u128>
) -> <Saturating<u128> as Add<Saturating<u128>>>::Output
type Output = <Saturating<i32> as Add<Saturating<i32>>>::Output
type Output = Saturating<u16>
type Output = Saturating<i8>
type Output = <Saturating<u64> as Add<Saturating<u64>>>::Output
pub fn add(
self,
other: &Saturating<u64>
) -> <Saturating<u64> as Add<Saturating<u64>>>::Output
type Output = Saturating<u8>
type Output = <Saturating<usize> as Add<Saturating<usize>>>::Output
pub fn add(
self,
other: Saturating<usize>
) -> <Saturating<usize> as Add<Saturating<usize>>>::Output
type Output = <Saturating<u128> as Add<Saturating<u128>>>::Output
pub fn add(
self,
other: &Saturating<u128>
) -> <Saturating<u128> as Add<Saturating<u128>>>::Output
type Output = <Saturating<u16> as Add<Saturating<u16>>>::Output
pub fn add(
self,
other: &Saturating<u16>
) -> <Saturating<u16> as Add<Saturating<u16>>>::Output
type Output = <Saturating<i16> as Add<Saturating<i16>>>::Output
pub fn add(
self,
other: &Saturating<i16>
) -> <Saturating<i16> as Add<Saturating<i16>>>::Output
type Output = Saturating<u128>
type Output = <Saturating<u32> as Add<Saturating<u32>>>::Output
pub fn add(
self,
other: &Saturating<u32>
) -> <Saturating<u32> as Add<Saturating<u32>>>::Output
type Output = <Saturating<i32> as Add<Saturating<i32>>>::Output
pub fn add(
self,
other: &Saturating<i32>
) -> <Saturating<i32> as Add<Saturating<i32>>>::Output
type Output = <Saturating<i64> as Add<Saturating<i64>>>::Output
type Output = Saturating<i64>
type Output = <Saturating<u8> as Add<Saturating<u8>>>::Output
Implements the +
operator for concatenating two strings.
This consumes the String
on the left-hand side and re-uses its buffer (growing it if
necessary). This is done to avoid allocating a new String
and copying the entire contents on
every operation, which would lead to O(n^2) running time when building an n-byte string by
repeated concatenation.
The string on the right-hand side is only borrowed; its contents are copied into the returned
String
.
Examples
Concatenating two String
s takes the first by value and borrows the second:
let a = String::from("hello");
let b = String::from(" world");
let c = a + &b;
// `a` is moved and can no longer be used here.
If you want to keep using the first String
, you can clone it and append to the clone instead:
let a = String::from("hello");
let b = String::from(" world");
let c = a.clone() + &b;
// `a` is still valid here.
Concatenating &str
slices can be done by converting the first to a String
:
let a = "hello";
let b = " world";
let c = a.to_string() + b;
impl Add<Point> for Point
impl Add<Point> for Point
Overloads: We support basic point math Support adding a point to a point
impl Add<Point3> for Point3
impl Add<Point3> for Point3
Overloads: We support basic point math Support adding a point to a point
Support adding an int to a point
Support adding an int to a point
impl Add<Isometry2x4> for Isometry2x4
impl Add<Isometry2x4> for Isometry2x4
impl Add<Isometry3> for Isometry3
impl Add<Isometry3> for Isometry3
impl Add<Similarity2x4> for Similarity2x4
impl Add<Similarity2x4> for Similarity2x4
impl Add<Isometry3x8> for Isometry3x8
impl Add<Isometry3x8> for Isometry3x8
impl Add<Isometry3x4> for Isometry3x4
impl Add<Isometry3x4> for Isometry3x4
impl Add<Similarity3x4> for Similarity3x4
impl Add<Similarity3x4> for Similarity3x4
impl Add<Similarity3> for Similarity3
impl Add<Similarity3> for Similarity3
impl Add<Similarity3x8> for Similarity3x8
impl Add<Similarity3x8> for Similarity3x8
impl Add<Isometry2> for Isometry2
impl Add<Isometry2> for Isometry2
impl Add<Similarity2x8> for Similarity2x8
impl Add<Similarity2x8> for Similarity2x8
impl Add<Isometry2x8> for Isometry2x8
impl Add<Isometry2x8> for Isometry2x8
impl Add<Similarity2> for Similarity2
impl Add<Similarity2> for Similarity2
impl Add<f64x2> for f64x2
impl Add<f64x2> for f64x2
impl<'_> Add<&'_ u32x8> for u32x8
impl<'_> Add<&'_ u32x8> for u32x8
impl Add<i64x2> for i64x2
impl Add<i64x2> for i64x2
impl<'_> Add<&'_ i64x2> for i64x2
impl<'_> Add<&'_ i64x2> for i64x2
impl Add<i8x16> for i8x16
impl Add<i8x16> for i8x16
impl<'_> Add<&'_ i8x16> for i8x16
impl<'_> Add<&'_ i8x16> for i8x16
impl<'_> Add<&'_ i16x8> for i16x8
impl<'_> Add<&'_ i16x8> for i16x8
impl<'_> Add<&'_ i8x32> for i8x32
impl<'_> Add<&'_ i8x32> for i8x32
impl Add<i64x4> for i64x4
impl Add<i64x4> for i64x4
impl Add<u64x2> for u64x2
impl Add<u64x2> for u64x2
impl<'_> Add<&'_ u32x4> for u32x4
impl<'_> Add<&'_ u32x4> for u32x4
impl<'_> Add<&'_ f64x4> for f64x4
impl<'_> Add<&'_ f64x4> for f64x4
impl Add<u64x4> for u64x4
impl Add<u64x4> for u64x4
impl Add<i8x32> for i8x32
impl Add<i8x32> for i8x32
impl<'_> Add<&'_ f32x4> for f32x4
impl<'_> Add<&'_ f32x4> for f32x4
impl<'_> Add<&'_ u16x8> for u16x8
impl<'_> Add<&'_ u16x8> for u16x8
impl Add<i16x8> for i16x8
impl Add<i16x8> for i16x8
impl<'_> Add<&'_ u8x16> for u8x16
impl<'_> Add<&'_ u8x16> for u8x16
impl Add<f32x4> for f32x4
impl Add<f32x4> for f32x4
impl Add<f32x8> for f32x8
impl Add<f32x8> for f32x8
impl Add<u16x8> for u16x8
impl Add<u16x8> for u16x8
impl Add<u32x8> for u32x8
impl Add<u32x8> for u32x8
impl<'_> Add<&'_ f64x2> for f64x2
impl<'_> Add<&'_ f64x2> for f64x2
impl Add<u8x16> for u8x16
impl Add<u8x16> for u8x16
impl Add<f64x4> for f64x4
impl Add<f64x4> for f64x4
impl Add<i32x8> for i32x8
impl Add<i32x8> for i32x8
impl Add<i32x4> for i32x4
impl Add<i32x4> for i32x4
impl<'_> Add<&'_ i32x4> for i32x4
impl<'_> Add<&'_ i32x4> for i32x4
impl Add<u32x4> for u32x4
impl Add<u32x4> for u32x4
impl<'_> Add<&'_ f32x8> for f32x8
impl<'_> Add<&'_ f32x8> for f32x8
impl<'_> Add<&'_ u64x4> for u64x4
impl<'_> Add<&'_ u64x4> for u64x4
impl<'_> Add<&'_ u64x2> for u64x2
impl<'_> Add<&'_ u64x2> for u64x2
impl<'_> Add<&'_ i32x8> for i32x8
impl<'_> Add<&'_ i32x8> for i32x8
type Output = NaiveDateTime
An addition of Duration
to NaiveTime
wraps around and never overflows or underflows.
In particular the addition ignores integral number of days.
As a part of Chrono’s leap second handling,
the addition assumes that there is no leap second ever,
except when the NaiveTime
itself represents a leap second
in which case the assumption becomes that there is exactly a single leap second ever.
Example
use chrono::{Duration, NaiveTime};
let from_hmsm = NaiveTime::from_hms_milli;
assert_eq!(from_hmsm(3, 5, 7, 0) + Duration::zero(), from_hmsm(3, 5, 7, 0));
assert_eq!(from_hmsm(3, 5, 7, 0) + Duration::seconds(1), from_hmsm(3, 5, 8, 0));
assert_eq!(from_hmsm(3, 5, 7, 0) + Duration::seconds(-1), from_hmsm(3, 5, 6, 0));
assert_eq!(from_hmsm(3, 5, 7, 0) + Duration::seconds(60 + 4), from_hmsm(3, 6, 11, 0));
assert_eq!(from_hmsm(3, 5, 7, 0) + Duration::seconds(7*60*60 - 6*60), from_hmsm(9, 59, 7, 0));
assert_eq!(from_hmsm(3, 5, 7, 0) + Duration::milliseconds(80), from_hmsm(3, 5, 7, 80));
assert_eq!(from_hmsm(3, 5, 7, 950) + Duration::milliseconds(280), from_hmsm(3, 5, 8, 230));
assert_eq!(from_hmsm(3, 5, 7, 950) + Duration::milliseconds(-980), from_hmsm(3, 5, 6, 970));
The addition wraps around.
assert_eq!(from_hmsm(3, 5, 7, 0) + Duration::seconds(22*60*60), from_hmsm(1, 5, 7, 0));
assert_eq!(from_hmsm(3, 5, 7, 0) + Duration::seconds(-8*60*60), from_hmsm(19, 5, 7, 0));
assert_eq!(from_hmsm(3, 5, 7, 0) + Duration::days(800), from_hmsm(3, 5, 7, 0));
Leap seconds are handled, but the addition assumes that it is the only leap second happened.
let leap = from_hmsm(3, 5, 59, 1_300);
assert_eq!(leap + Duration::zero(), from_hmsm(3, 5, 59, 1_300));
assert_eq!(leap + Duration::milliseconds(-500), from_hmsm(3, 5, 59, 800));
assert_eq!(leap + Duration::milliseconds(500), from_hmsm(3, 5, 59, 1_800));
assert_eq!(leap + Duration::milliseconds(800), from_hmsm(3, 6, 0, 100));
assert_eq!(leap + Duration::seconds(10), from_hmsm(3, 6, 9, 300));
assert_eq!(leap + Duration::seconds(-10), from_hmsm(3, 5, 50, 300));
assert_eq!(leap + Duration::days(1), from_hmsm(3, 5, 59, 300));
An addition of Duration
to NaiveDateTime
yields another NaiveDateTime
.
As a part of Chrono’s leap second handling,
the addition assumes that there is no leap second ever,
except when the NaiveDateTime
itself represents a leap second
in which case the assumption becomes that there is exactly a single leap second ever.
Panics on underflow or overflow.
Use NaiveDateTime::checked_add_signed
to detect that.
Example
use chrono::{Duration, NaiveDate};
let from_ymd = NaiveDate::from_ymd;
let d = from_ymd(2016, 7, 8);
let hms = |h, m, s| d.and_hms(h, m, s);
assert_eq!(hms(3, 5, 7) + Duration::zero(), hms(3, 5, 7));
assert_eq!(hms(3, 5, 7) + Duration::seconds(1), hms(3, 5, 8));
assert_eq!(hms(3, 5, 7) + Duration::seconds(-1), hms(3, 5, 6));
assert_eq!(hms(3, 5, 7) + Duration::seconds(3600 + 60), hms(4, 6, 7));
assert_eq!(hms(3, 5, 7) + Duration::seconds(86_400),
from_ymd(2016, 7, 9).and_hms(3, 5, 7));
assert_eq!(hms(3, 5, 7) + Duration::days(365),
from_ymd(2017, 7, 8).and_hms(3, 5, 7));
let hmsm = |h, m, s, milli| d.and_hms_milli(h, m, s, milli);
assert_eq!(hmsm(3, 5, 7, 980) + Duration::milliseconds(450), hmsm(3, 5, 8, 430));
Leap seconds are handled, but the addition assumes that it is the only leap second happened.
let leap = hmsm(3, 5, 59, 1_300);
assert_eq!(leap + Duration::zero(), hmsm(3, 5, 59, 1_300));
assert_eq!(leap + Duration::milliseconds(-500), hmsm(3, 5, 59, 800));
assert_eq!(leap + Duration::milliseconds(500), hmsm(3, 5, 59, 1_800));
assert_eq!(leap + Duration::milliseconds(800), hmsm(3, 6, 0, 100));
assert_eq!(leap + Duration::seconds(10), hmsm(3, 6, 9, 300));
assert_eq!(leap + Duration::seconds(-10), hmsm(3, 5, 50, 300));
assert_eq!(leap + Duration::days(1),
from_ymd(2016, 7, 9).and_hms_milli(3, 5, 59, 300));
type Output = NaiveDateTime
An addition of Duration
to NaiveDate
discards the fractional days,
rounding to the closest integral number of days towards Duration::zero()
.
Panics on underflow or overflow.
Use NaiveDate::checked_add_signed
to detect that.
Example
use chrono::{Duration, NaiveDate};
let from_ymd = NaiveDate::from_ymd;
assert_eq!(from_ymd(2014, 1, 1) + Duration::zero(), from_ymd(2014, 1, 1));
assert_eq!(from_ymd(2014, 1, 1) + Duration::seconds(86399), from_ymd(2014, 1, 1));
assert_eq!(from_ymd(2014, 1, 1) + Duration::seconds(-86399), from_ymd(2014, 1, 1));
assert_eq!(from_ymd(2014, 1, 1) + Duration::days(1), from_ymd(2014, 1, 2));
assert_eq!(from_ymd(2014, 1, 1) + Duration::days(-1), from_ymd(2013, 12, 31));
assert_eq!(from_ymd(2014, 1, 1) + Duration::days(364), from_ymd(2014, 12, 31));
assert_eq!(from_ymd(2014, 1, 1) + Duration::days(365*4 + 1), from_ymd(2018, 1, 1));
assert_eq!(from_ymd(2014, 1, 1) + Duration::days(365*400 + 97), from_ymd(2414, 1, 1));