1 // Copyright 2012 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.
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.
11 //! Overloadable operators
13 //! Implementing these traits allows you to get an effect similar to
14 //! overloading operators.
16 //! Some of these traits are imported by the prelude, so they are available in
17 //! every Rust program.
19 //! Many of the operators take their operands by value. In non-generic
20 //! contexts involving built-in types, this is usually not a problem.
21 //! However, using these operators in generic code, requires some
22 //! attention if values have to be reused as opposed to letting the operators
23 //! consume them. One option is to occasionally use `clone()`.
24 //! Another option is to rely on the types involved providing additional
25 //! operator implementations for references. For example, for a user-defined
26 //! type `T` which is supposed to support addition, it is probably a good
27 //! idea to have both `T` and `&T` implement the traits `Add<T>` and `Add<&T>`
28 //! so that generic code can be written without unnecessary cloning.
32 //! This example creates a `Point` struct that implements `Add` and `Sub`, and
33 //! then demonstrates adding and subtracting two `Point`s.
36 //! use std::ops::{Add, Sub};
44 //! impl Add for Point {
45 //! type Output = Point;
47 //! fn add(self, other: Point) -> Point {
48 //! Point {x: self.x + other.x, y: self.y + other.y}
52 //! impl Sub for Point {
53 //! type Output = Point;
55 //! fn sub(self, other: Point) -> Point {
56 //! Point {x: self.x - other.x, y: self.y - other.y}
60 //! println!("{:?}", Point {x: 1, y: 0} + Point {x: 2, y: 3});
61 //! println!("{:?}", Point {x: 1, y: 0} - Point {x: 2, y: 3});
65 //! See the documentation for each trait for a minimum implementation that
66 //! prints something to the screen.
68 #![stable(feature = "rust1", since = "1.0.0")]
70 use marker::{Sized, Unsize};
73 /// The `Drop` trait is used to run some code when a value goes out of scope.
74 /// This is sometimes called a 'destructor'.
78 /// A trivial implementation of `Drop`. The `drop` method is called when `_x`
79 /// goes out of scope, and therefore `main` prints `Dropping!`.
84 /// impl Drop for HasDrop {
85 /// fn drop(&mut self) {
86 /// println!("Dropping!");
95 #[stable(feature = "rust1", since = "1.0.0")]
97 /// A method called when the value goes out of scope.
98 #[stable(feature = "rust1", since = "1.0.0")]
102 // implements the unary operator "op &T"
103 // based on "op T" where T is expected to be `Copy`able
104 macro_rules! forward_ref_unop {
105 (impl $imp:ident, $method:ident for $t:ty) => {
106 #[stable(feature = "rust1", since = "1.0.0")]
107 impl<'a> $imp for &'a $t {
108 type Output = <$t as $imp>::Output;
111 fn $method(self) -> <$t as $imp>::Output {
118 // implements binary operators "&T op U", "T op &U", "&T op &U"
119 // based on "T op U" where T and U are expected to be `Copy`able
120 macro_rules! forward_ref_binop {
121 (impl $imp:ident, $method:ident for $t:ty, $u:ty) => {
122 #[stable(feature = "rust1", since = "1.0.0")]
123 impl<'a> $imp<$u> for &'a $t {
124 type Output = <$t as $imp<$u>>::Output;
127 fn $method(self, other: $u) -> <$t as $imp<$u>>::Output {
128 $imp::$method(*self, other)
132 #[stable(feature = "rust1", since = "1.0.0")]
133 impl<'a> $imp<&'a $u> for $t {
134 type Output = <$t as $imp<$u>>::Output;
137 fn $method(self, other: &'a $u) -> <$t as $imp<$u>>::Output {
138 $imp::$method(self, *other)
142 #[stable(feature = "rust1", since = "1.0.0")]
143 impl<'a, 'b> $imp<&'a $u> for &'b $t {
144 type Output = <$t as $imp<$u>>::Output;
147 fn $method(self, other: &'a $u) -> <$t as $imp<$u>>::Output {
148 $imp::$method(*self, *other)
154 /// The `Add` trait is used to specify the functionality of `+`.
158 /// A trivial implementation of `Add`. When `Foo + Foo` happens, it ends up
159 /// calling `add`, and therefore, `main` prints `Adding!`.
162 /// use std::ops::Add;
164 /// #[derive(Copy, Clone)]
167 /// impl Add for Foo {
168 /// type Output = Foo;
170 /// fn add(self, _rhs: Foo) -> Foo {
171 /// println!("Adding!");
181 #[stable(feature = "rust1", since = "1.0.0")]
182 pub trait Add<RHS=Self> {
183 /// The resulting type after applying the `+` operator
184 #[stable(feature = "rust1", since = "1.0.0")]
187 /// The method for the `+` operator
188 #[stable(feature = "rust1", since = "1.0.0")]
189 fn add(self, rhs: RHS) -> Self::Output;
192 macro_rules! add_impl {
194 #[stable(feature = "rust1", since = "1.0.0")]
199 fn add(self, other: $t) -> $t { self + other }
202 forward_ref_binop! { impl Add, add for $t, $t }
206 add_impl! { usize u8 u16 u32 u64 isize i8 i16 i32 i64 f32 f64 }
208 /// The `Sub` trait is used to specify the functionality of `-`.
212 /// A trivial implementation of `Sub`. When `Foo - Foo` happens, it ends up
213 /// calling `sub`, and therefore, `main` prints `Subtracting!`.
216 /// use std::ops::Sub;
218 /// #[derive(Copy, Clone)]
221 /// impl Sub for Foo {
222 /// type Output = Foo;
224 /// fn sub(self, _rhs: Foo) -> Foo {
225 /// println!("Subtracting!");
235 #[stable(feature = "rust1", since = "1.0.0")]
236 pub trait Sub<RHS=Self> {
237 /// The resulting type after applying the `-` operator
238 #[stable(feature = "rust1", since = "1.0.0")]
241 /// The method for the `-` operator
242 #[stable(feature = "rust1", since = "1.0.0")]
243 fn sub(self, rhs: RHS) -> Self::Output;
246 macro_rules! sub_impl {
248 #[stable(feature = "rust1", since = "1.0.0")]
253 fn sub(self, other: $t) -> $t { self - other }
256 forward_ref_binop! { impl Sub, sub for $t, $t }
260 sub_impl! { usize u8 u16 u32 u64 isize i8 i16 i32 i64 f32 f64 }
262 /// The `Mul` trait is used to specify the functionality of `*`.
266 /// A trivial implementation of `Mul`. When `Foo * Foo` happens, it ends up
267 /// calling `mul`, and therefore, `main` prints `Multiplying!`.
270 /// use std::ops::Mul;
272 /// #[derive(Copy, Clone)]
275 /// impl Mul for Foo {
276 /// type Output = Foo;
278 /// fn mul(self, _rhs: Foo) -> Foo {
279 /// println!("Multiplying!");
289 #[stable(feature = "rust1", since = "1.0.0")]
290 pub trait Mul<RHS=Self> {
291 /// The resulting type after applying the `*` operator
292 #[stable(feature = "rust1", since = "1.0.0")]
295 /// The method for the `*` operator
296 #[stable(feature = "rust1", since = "1.0.0")]
297 fn mul(self, rhs: RHS) -> Self::Output;
300 macro_rules! mul_impl {
302 #[stable(feature = "rust1", since = "1.0.0")]
307 fn mul(self, other: $t) -> $t { self * other }
310 forward_ref_binop! { impl Mul, mul for $t, $t }
314 mul_impl! { usize u8 u16 u32 u64 isize i8 i16 i32 i64 f32 f64 }
316 /// The `Div` trait is used to specify the functionality of `/`.
320 /// A trivial implementation of `Div`. When `Foo / Foo` happens, it ends up
321 /// calling `div`, and therefore, `main` prints `Dividing!`.
324 /// use std::ops::Div;
326 /// #[derive(Copy, Clone)]
329 /// impl Div for Foo {
330 /// type Output = Foo;
332 /// fn div(self, _rhs: Foo) -> Foo {
333 /// println!("Dividing!");
343 #[stable(feature = "rust1", since = "1.0.0")]
344 pub trait Div<RHS=Self> {
345 /// The resulting type after applying the `/` operator
346 #[stable(feature = "rust1", since = "1.0.0")]
349 /// The method for the `/` operator
350 #[stable(feature = "rust1", since = "1.0.0")]
351 fn div(self, rhs: RHS) -> Self::Output;
354 macro_rules! div_impl_integer {
356 /// This operation rounds towards zero, truncating any
357 /// fractional part of the exact result.
358 #[stable(feature = "rust1", since = "1.0.0")]
363 fn div(self, other: $t) -> $t { self / other }
366 forward_ref_binop! { impl Div, div for $t, $t }
370 div_impl_integer! { usize u8 u16 u32 u64 isize i8 i16 i32 i64 }
372 macro_rules! div_impl_float {
374 #[stable(feature = "rust1", since = "1.0.0")]
379 fn div(self, other: $t) -> $t { self / other }
382 forward_ref_binop! { impl Div, div for $t, $t }
386 div_impl_float! { f32 f64 }
388 /// The `Rem` trait is used to specify the functionality of `%`.
392 /// A trivial implementation of `Rem`. When `Foo % Foo` happens, it ends up
393 /// calling `rem`, and therefore, `main` prints `Remainder-ing!`.
396 /// use std::ops::Rem;
398 /// #[derive(Copy, Clone)]
401 /// impl Rem for Foo {
402 /// type Output = Foo;
404 /// fn rem(self, _rhs: Foo) -> Foo {
405 /// println!("Remainder-ing!");
415 #[stable(feature = "rust1", since = "1.0.0")]
416 pub trait Rem<RHS=Self> {
417 /// The resulting type after applying the `%` operator
418 #[stable(feature = "rust1", since = "1.0.0")]
421 /// The method for the `%` operator
422 #[stable(feature = "rust1", since = "1.0.0")]
423 fn rem(self, rhs: RHS) -> Self::Output;
426 macro_rules! rem_impl_integer {
428 /// This operation satisfies `n % d == n - (n / d) * d`. The
429 /// result has the same sign as the left operand.
430 #[stable(feature = "rust1", since = "1.0.0")]
435 fn rem(self, other: $t) -> $t { self % other }
438 forward_ref_binop! { impl Rem, rem for $t, $t }
442 rem_impl_integer! { usize u8 u16 u32 u64 isize i8 i16 i32 i64 }
445 macro_rules! rem_impl_float {
447 #[stable(feature = "rust1", since = "1.0.0")]
452 fn rem(self, other: $t) -> $t { self % other }
455 forward_ref_binop! { impl Rem, rem for $t, $t }
460 rem_impl_float! { f32 f64 }
462 #[stable(feature = "rust1", since = "1.0.0")]
467 // The builtin f32 rem operator is broken when targeting
468 // MSVC; see comment in std::f32::floor.
469 // FIXME: See also #27859.
471 #[cfg(target_env = "msvc")]
472 fn rem(self, other: f32) -> f32 {
473 (self as f64).rem(other as f64) as f32
477 #[cfg(not(target_env = "msvc"))]
478 fn rem(self, other: f32) -> f32 {
479 extern { fn fmodf(a: f32, b: f32) -> f32; }
480 unsafe { fmodf(self, other) }
484 #[stable(feature = "rust1", since = "1.0.0")]
490 fn rem(self, other: f64) -> f64 {
491 extern { fn fmod(a: f64, b: f64) -> f64; }
492 unsafe { fmod(self, other) }
497 forward_ref_binop! { impl Rem, rem for f64, f64 }
499 forward_ref_binop! { impl Rem, rem for f32, f32 }
501 /// The `Neg` trait is used to specify the functionality of unary `-`.
505 /// A trivial implementation of `Neg`. When `-Foo` happens, it ends up calling
506 /// `neg`, and therefore, `main` prints `Negating!`.
509 /// use std::ops::Neg;
511 /// #[derive(Copy, Clone)]
514 /// impl Neg for Foo {
515 /// type Output = Foo;
517 /// fn neg(self) -> Foo {
518 /// println!("Negating!");
528 #[stable(feature = "rust1", since = "1.0.0")]
530 /// The resulting type after applying the `-` operator
531 #[stable(feature = "rust1", since = "1.0.0")]
534 /// The method for the unary `-` operator
535 #[stable(feature = "rust1", since = "1.0.0")]
536 fn neg(self) -> Self::Output;
541 macro_rules! neg_impl_core {
542 ($id:ident => $body:expr, $($t:ty)*) => ($(
543 #[stable(feature = "rust1", since = "1.0.0")]
545 #[stable(feature = "rust1", since = "1.0.0")]
549 #[stable(feature = "rust1", since = "1.0.0")]
550 fn neg(self) -> $t { let $id = self; $body }
553 forward_ref_unop! { impl Neg, neg for $t }
557 macro_rules! neg_impl_numeric {
558 ($($t:ty)*) => { neg_impl_core!{ x => -x, $($t)*} }
561 macro_rules! neg_impl_unsigned {
563 neg_impl_core!{ x => {
568 // neg_impl_unsigned! { usize u8 u16 u32 u64 }
569 neg_impl_numeric! { isize i8 i16 i32 i64 f32 f64 }
571 /// The `Not` trait is used to specify the functionality of unary `!`.
575 /// A trivial implementation of `Not`. When `!Foo` happens, it ends up calling
576 /// `not`, and therefore, `main` prints `Not-ing!`.
579 /// use std::ops::Not;
581 /// #[derive(Copy, Clone)]
584 /// impl Not for Foo {
585 /// type Output = Foo;
587 /// fn not(self) -> Foo {
588 /// println!("Not-ing!");
598 #[stable(feature = "rust1", since = "1.0.0")]
600 /// The resulting type after applying the `!` operator
601 #[stable(feature = "rust1", since = "1.0.0")]
604 /// The method for the unary `!` operator
605 #[stable(feature = "rust1", since = "1.0.0")]
606 fn not(self) -> Self::Output;
609 macro_rules! not_impl {
611 #[stable(feature = "rust1", since = "1.0.0")]
616 fn not(self) -> $t { !self }
619 forward_ref_unop! { impl Not, not for $t }
623 not_impl! { bool usize u8 u16 u32 u64 isize i8 i16 i32 i64 }
625 /// The `BitAnd` trait is used to specify the functionality of `&`.
629 /// A trivial implementation of `BitAnd`. When `Foo & Foo` happens, it ends up
630 /// calling `bitand`, and therefore, `main` prints `Bitwise And-ing!`.
633 /// use std::ops::BitAnd;
635 /// #[derive(Copy, Clone)]
638 /// impl BitAnd for Foo {
639 /// type Output = Foo;
641 /// fn bitand(self, _rhs: Foo) -> Foo {
642 /// println!("Bitwise And-ing!");
652 #[stable(feature = "rust1", since = "1.0.0")]
653 pub trait BitAnd<RHS=Self> {
654 /// The resulting type after applying the `&` operator
655 #[stable(feature = "rust1", since = "1.0.0")]
658 /// The method for the `&` operator
659 #[stable(feature = "rust1", since = "1.0.0")]
660 fn bitand(self, rhs: RHS) -> Self::Output;
663 macro_rules! bitand_impl {
665 #[stable(feature = "rust1", since = "1.0.0")]
670 fn bitand(self, rhs: $t) -> $t { self & rhs }
673 forward_ref_binop! { impl BitAnd, bitand for $t, $t }
677 bitand_impl! { bool usize u8 u16 u32 u64 isize i8 i16 i32 i64 }
679 /// The `BitOr` trait is used to specify the functionality of `|`.
683 /// A trivial implementation of `BitOr`. When `Foo | Foo` happens, it ends up
684 /// calling `bitor`, and therefore, `main` prints `Bitwise Or-ing!`.
687 /// use std::ops::BitOr;
689 /// #[derive(Copy, Clone)]
692 /// impl BitOr for Foo {
693 /// type Output = Foo;
695 /// fn bitor(self, _rhs: Foo) -> Foo {
696 /// println!("Bitwise Or-ing!");
706 #[stable(feature = "rust1", since = "1.0.0")]
707 pub trait BitOr<RHS=Self> {
708 /// The resulting type after applying the `|` operator
709 #[stable(feature = "rust1", since = "1.0.0")]
712 /// The method for the `|` operator
713 #[stable(feature = "rust1", since = "1.0.0")]
714 fn bitor(self, rhs: RHS) -> Self::Output;
717 macro_rules! bitor_impl {
719 #[stable(feature = "rust1", since = "1.0.0")]
724 fn bitor(self, rhs: $t) -> $t { self | rhs }
727 forward_ref_binop! { impl BitOr, bitor for $t, $t }
731 bitor_impl! { bool usize u8 u16 u32 u64 isize i8 i16 i32 i64 }
733 /// The `BitXor` trait is used to specify the functionality of `^`.
737 /// A trivial implementation of `BitXor`. When `Foo ^ Foo` happens, it ends up
738 /// calling `bitxor`, and therefore, `main` prints `Bitwise Xor-ing!`.
741 /// use std::ops::BitXor;
743 /// #[derive(Copy, Clone)]
746 /// impl BitXor for Foo {
747 /// type Output = Foo;
749 /// fn bitxor(self, _rhs: Foo) -> Foo {
750 /// println!("Bitwise Xor-ing!");
760 #[stable(feature = "rust1", since = "1.0.0")]
761 pub trait BitXor<RHS=Self> {
762 /// The resulting type after applying the `^` operator
763 #[stable(feature = "rust1", since = "1.0.0")]
766 /// The method for the `^` operator
767 #[stable(feature = "rust1", since = "1.0.0")]
768 fn bitxor(self, rhs: RHS) -> Self::Output;
771 macro_rules! bitxor_impl {
773 #[stable(feature = "rust1", since = "1.0.0")]
778 fn bitxor(self, other: $t) -> $t { self ^ other }
781 forward_ref_binop! { impl BitXor, bitxor for $t, $t }
785 bitxor_impl! { bool usize u8 u16 u32 u64 isize i8 i16 i32 i64 }
787 /// The `Shl` trait is used to specify the functionality of `<<`.
791 /// A trivial implementation of `Shl`. When `Foo << Foo` happens, it ends up
792 /// calling `shl`, and therefore, `main` prints `Shifting left!`.
795 /// use std::ops::Shl;
797 /// #[derive(Copy, Clone)]
800 /// impl Shl<Foo> for Foo {
801 /// type Output = Foo;
803 /// fn shl(self, _rhs: Foo) -> Foo {
804 /// println!("Shifting left!");
814 #[stable(feature = "rust1", since = "1.0.0")]
816 /// The resulting type after applying the `<<` operator
817 #[stable(feature = "rust1", since = "1.0.0")]
820 /// The method for the `<<` operator
821 #[stable(feature = "rust1", since = "1.0.0")]
822 fn shl(self, rhs: RHS) -> Self::Output;
825 macro_rules! shl_impl {
827 #[stable(feature = "rust1", since = "1.0.0")]
828 impl Shl<$f> for $t {
832 fn shl(self, other: $f) -> $t {
837 forward_ref_binop! { impl Shl, shl for $t, $f }
841 macro_rules! shl_impl_all {
844 shl_impl! { $t, u16 }
845 shl_impl! { $t, u32 }
846 shl_impl! { $t, u64 }
847 shl_impl! { $t, usize }
850 shl_impl! { $t, i16 }
851 shl_impl! { $t, i32 }
852 shl_impl! { $t, i64 }
853 shl_impl! { $t, isize }
857 shl_impl_all! { u8 u16 u32 u64 usize i8 i16 i32 i64 isize }
859 /// The `Shr` trait is used to specify the functionality of `>>`.
863 /// A trivial implementation of `Shr`. When `Foo >> Foo` happens, it ends up
864 /// calling `shr`, and therefore, `main` prints `Shifting right!`.
867 /// use std::ops::Shr;
869 /// #[derive(Copy, Clone)]
872 /// impl Shr<Foo> for Foo {
873 /// type Output = Foo;
875 /// fn shr(self, _rhs: Foo) -> Foo {
876 /// println!("Shifting right!");
886 #[stable(feature = "rust1", since = "1.0.0")]
888 /// The resulting type after applying the `>>` operator
889 #[stable(feature = "rust1", since = "1.0.0")]
892 /// The method for the `>>` operator
893 #[stable(feature = "rust1", since = "1.0.0")]
894 fn shr(self, rhs: RHS) -> Self::Output;
897 macro_rules! shr_impl {
899 impl Shr<$f> for $t {
903 fn shr(self, other: $f) -> $t {
908 forward_ref_binop! { impl Shr, shr for $t, $f }
912 macro_rules! shr_impl_all {
915 shr_impl! { $t, u16 }
916 shr_impl! { $t, u32 }
917 shr_impl! { $t, u64 }
918 shr_impl! { $t, usize }
921 shr_impl! { $t, i16 }
922 shr_impl! { $t, i32 }
923 shr_impl! { $t, i64 }
924 shr_impl! { $t, isize }
928 shr_impl_all! { u8 u16 u32 u64 usize i8 i16 i32 i64 isize }
930 /// The `AddAssign` trait is used to specify the functionality of `+=`.
934 /// A trivial implementation of `AddAssign`. When `Foo += Foo` happens, it ends up
935 /// calling `add_assign`, and therefore, `main` prints `Adding!`.
938 /// #![feature(augmented_assignments)]
939 /// #![feature(op_assign_traits)]
941 /// use std::ops::AddAssign;
943 /// #[derive(Copy, Clone)]
946 /// impl AddAssign for Foo {
947 /// fn add_assign(&mut self, _rhs: Foo) {
948 /// println!("Adding!");
953 /// let mut foo = Foo;
958 #[lang = "add_assign"]
959 #[unstable(feature = "op_assign_traits", reason = "recently added", issue = "28235")]
960 pub trait AddAssign<Rhs=Self> {
961 /// The method for the `+=` operator
962 fn add_assign(&mut self, Rhs);
966 macro_rules! add_assign_impl {
968 #[unstable(feature = "op_assign_traits", reason = "recently added", issue = "28235")]
969 impl AddAssign for $t {
971 fn add_assign(&mut self, other: $t) { *self += other }
977 add_assign_impl! { usize u8 u16 u32 u64 isize i8 i16 i32 i64 f32 f64 }
979 /// The `SubAssign` trait is used to specify the functionality of `-=`.
983 /// A trivial implementation of `SubAssign`. When `Foo -= Foo` happens, it ends up
984 /// calling `sub_assign`, and therefore, `main` prints `Subtracting!`.
987 /// #![feature(augmented_assignments)]
988 /// #![feature(op_assign_traits)]
990 /// use std::ops::SubAssign;
992 /// #[derive(Copy, Clone)]
995 /// impl SubAssign for Foo {
996 /// fn sub_assign(&mut self, _rhs: Foo) {
997 /// println!("Subtracting!");
1002 /// let mut foo = Foo;
1007 #[lang = "sub_assign"]
1008 #[unstable(feature = "op_assign_traits", reason = "recently added", issue = "28235")]
1009 pub trait SubAssign<Rhs=Self> {
1010 /// The method for the `-=` operator
1011 fn sub_assign(&mut self, Rhs);
1015 macro_rules! sub_assign_impl {
1017 #[unstable(feature = "op_assign_traits", reason = "recently added", issue = "28235")]
1018 impl SubAssign for $t {
1020 fn sub_assign(&mut self, other: $t) { *self -= other }
1026 sub_assign_impl! { usize u8 u16 u32 u64 isize i8 i16 i32 i64 f32 f64 }
1028 /// The `MulAssign` trait is used to specify the functionality of `*=`.
1032 /// A trivial implementation of `MulAssign`. When `Foo *= Foo` happens, it ends up
1033 /// calling `mul_assign`, and therefore, `main` prints `Multiplying!`.
1036 /// #![feature(augmented_assignments)]
1037 /// #![feature(op_assign_traits)]
1039 /// use std::ops::MulAssign;
1041 /// #[derive(Copy, Clone)]
1044 /// impl MulAssign for Foo {
1045 /// fn mul_assign(&mut self, _rhs: Foo) {
1046 /// println!("Multiplying!");
1051 /// let mut foo = Foo;
1056 #[lang = "mul_assign"]
1057 #[unstable(feature = "op_assign_traits", reason = "recently added", issue = "28235")]
1058 pub trait MulAssign<Rhs=Self> {
1059 /// The method for the `*=` operator
1060 fn mul_assign(&mut self, Rhs);
1064 macro_rules! mul_assign_impl {
1066 #[unstable(feature = "op_assign_traits", reason = "recently added", issue = "28235")]
1067 impl MulAssign for $t {
1069 fn mul_assign(&mut self, other: $t) { *self *= other }
1075 mul_assign_impl! { usize u8 u16 u32 u64 isize i8 i16 i32 i64 f32 f64 }
1077 /// The `DivAssign` trait is used to specify the functionality of `/=`.
1081 /// A trivial implementation of `DivAssign`. When `Foo /= Foo` happens, it ends up
1082 /// calling `div_assign`, and therefore, `main` prints `Dividing!`.
1085 /// #![feature(augmented_assignments)]
1086 /// #![feature(op_assign_traits)]
1088 /// use std::ops::DivAssign;
1090 /// #[derive(Copy, Clone)]
1093 /// impl DivAssign for Foo {
1094 /// fn div_assign(&mut self, _rhs: Foo) {
1095 /// println!("Dividing!");
1100 /// let mut foo = Foo;
1105 #[lang = "div_assign"]
1106 #[unstable(feature = "op_assign_traits", reason = "recently added", issue = "28235")]
1107 pub trait DivAssign<Rhs=Self> {
1108 /// The method for the `/=` operator
1109 fn div_assign(&mut self, Rhs);
1113 macro_rules! div_assign_impl {
1115 #[unstable(feature = "op_assign_traits", reason = "recently added", issue = "28235")]
1116 impl DivAssign for $t {
1118 fn div_assign(&mut self, other: $t) { *self /= other }
1124 div_assign_impl! { usize u8 u16 u32 u64 isize i8 i16 i32 i64 f32 f64 }
1126 /// The `RemAssign` trait is used to specify the functionality of `%=`.
1130 /// A trivial implementation of `RemAssign`. When `Foo %= Foo` happens, it ends up
1131 /// calling `rem_assign`, and therefore, `main` prints `Remainder-ing!`.
1134 /// #![feature(augmented_assignments)]
1135 /// #![feature(op_assign_traits)]
1137 /// use std::ops::RemAssign;
1139 /// #[derive(Copy, Clone)]
1142 /// impl RemAssign for Foo {
1143 /// fn rem_assign(&mut self, _rhs: Foo) {
1144 /// println!("Remainder-ing!");
1149 /// let mut foo = Foo;
1154 #[lang = "rem_assign"]
1155 #[unstable(feature = "op_assign_traits", reason = "recently added", issue = "28235")]
1156 pub trait RemAssign<Rhs=Self> {
1157 /// The method for the `%=` operator
1158 fn rem_assign(&mut self, Rhs);
1162 macro_rules! rem_assign_impl {
1164 #[unstable(feature = "op_assign_traits", reason = "recently added", issue = "28235")]
1165 impl RemAssign for $t {
1167 fn rem_assign(&mut self, other: $t) { *self %= other }
1173 rem_assign_impl! { usize u8 u16 u32 u64 isize i8 i16 i32 i64 f32 f64 }
1175 /// The `BitAndAssign` trait is used to specify the functionality of `&=`.
1179 /// A trivial implementation of `BitAndAssign`. When `Foo &= Foo` happens, it ends up
1180 /// calling `bitand_assign`, and therefore, `main` prints `Bitwise And-ing!`.
1183 /// #![feature(augmented_assignments)]
1184 /// #![feature(op_assign_traits)]
1186 /// use std::ops::BitAndAssign;
1188 /// #[derive(Copy, Clone)]
1191 /// impl BitAndAssign for Foo {
1192 /// fn bitand_assign(&mut self, _rhs: Foo) {
1193 /// println!("Bitwise And-ing!");
1198 /// let mut foo = Foo;
1203 #[lang = "bitand_assign"]
1204 #[unstable(feature = "op_assign_traits", reason = "recently added", issue = "28235")]
1205 pub trait BitAndAssign<Rhs=Self> {
1206 /// The method for the `&` operator
1207 fn bitand_assign(&mut self, Rhs);
1211 macro_rules! bitand_assign_impl {
1213 #[unstable(feature = "op_assign_traits", reason = "recently added", issue = "28235")]
1214 impl BitAndAssign for $t {
1216 fn bitand_assign(&mut self, other: $t) { *self &= other }
1222 bitand_assign_impl! { bool usize u8 u16 u32 u64 isize i8 i16 i32 i64 }
1224 /// The `BitOrAssign` trait is used to specify the functionality of `|=`.
1228 /// A trivial implementation of `BitOrAssign`. When `Foo |= Foo` happens, it ends up
1229 /// calling `bitor_assign`, and therefore, `main` prints `Bitwise Or-ing!`.
1232 /// #![feature(augmented_assignments)]
1233 /// #![feature(op_assign_traits)]
1235 /// use std::ops::BitOrAssign;
1237 /// #[derive(Copy, Clone)]
1240 /// impl BitOrAssign for Foo {
1241 /// fn bitor_assign(&mut self, _rhs: Foo) {
1242 /// println!("Bitwise Or-ing!");
1247 /// let mut foo = Foo;
1252 #[lang = "bitor_assign"]
1253 #[unstable(feature = "op_assign_traits", reason = "recently added", issue = "28235")]
1254 pub trait BitOrAssign<Rhs=Self> {
1255 /// The method for the `|=` operator
1256 fn bitor_assign(&mut self, Rhs);
1260 macro_rules! bitor_assign_impl {
1262 #[unstable(feature = "op_assign_traits", reason = "recently added", issue = "28235")]
1263 impl BitOrAssign for $t {
1265 fn bitor_assign(&mut self, other: $t) { *self |= other }
1271 bitor_assign_impl! { bool usize u8 u16 u32 u64 isize i8 i16 i32 i64 }
1273 /// The `BitXorAssign` trait is used to specify the functionality of `^=`.
1277 /// A trivial implementation of `BitXorAssign`. When `Foo ^= Foo` happens, it ends up
1278 /// calling `bitxor_assign`, and therefore, `main` prints `Bitwise Xor-ing!`.
1281 /// #![feature(augmented_assignments)]
1282 /// #![feature(op_assign_traits)]
1284 /// use std::ops::BitXorAssign;
1286 /// #[derive(Copy, Clone)]
1289 /// impl BitXorAssign for Foo {
1290 /// fn bitxor_assign(&mut self, _rhs: Foo) {
1291 /// println!("Bitwise Xor-ing!");
1296 /// let mut foo = Foo;
1301 #[lang = "bitxor_assign"]
1302 #[unstable(feature = "op_assign_traits", reason = "recently added", issue = "28235")]
1303 pub trait BitXorAssign<Rhs=Self> {
1304 /// The method for the `^=` operator
1305 fn bitxor_assign(&mut self, Rhs);
1309 macro_rules! bitxor_assign_impl {
1311 #[unstable(feature = "op_assign_traits", reason = "recently added", issue = "28235")]
1312 impl BitXorAssign for $t {
1314 fn bitxor_assign(&mut self, other: $t) { *self ^= other }
1320 bitxor_assign_impl! { bool usize u8 u16 u32 u64 isize i8 i16 i32 i64 }
1322 /// The `ShlAssign` trait is used to specify the functionality of `<<=`.
1326 /// A trivial implementation of `ShlAssign`. When `Foo <<= Foo` happens, it ends up
1327 /// calling `shl_assign`, and therefore, `main` prints `Shifting left!`.
1330 /// #![feature(augmented_assignments)]
1331 /// #![feature(op_assign_traits)]
1333 /// use std::ops::ShlAssign;
1335 /// #[derive(Copy, Clone)]
1338 /// impl ShlAssign<Foo> for Foo {
1339 /// fn shl_assign(&mut self, _rhs: Foo) {
1340 /// println!("Shifting left!");
1345 /// let mut foo = Foo;
1350 #[lang = "shl_assign"]
1351 #[unstable(feature = "op_assign_traits", reason = "recently added", issue = "28235")]
1352 pub trait ShlAssign<Rhs> {
1353 /// The method for the `<<=` operator
1354 fn shl_assign(&mut self, Rhs);
1358 macro_rules! shl_assign_impl {
1360 #[unstable(feature = "op_assign_traits", reason = "recently added", issue = "28235")]
1361 impl ShlAssign<$f> for $t {
1363 fn shl_assign(&mut self, other: $f) {
1371 macro_rules! shl_assign_impl_all {
1373 shl_assign_impl! { $t, u8 }
1374 shl_assign_impl! { $t, u16 }
1375 shl_assign_impl! { $t, u32 }
1376 shl_assign_impl! { $t, u64 }
1377 shl_assign_impl! { $t, usize }
1379 shl_assign_impl! { $t, i8 }
1380 shl_assign_impl! { $t, i16 }
1381 shl_assign_impl! { $t, i32 }
1382 shl_assign_impl! { $t, i64 }
1383 shl_assign_impl! { $t, isize }
1388 shl_assign_impl_all! { u8 u16 u32 u64 usize i8 i16 i32 i64 isize }
1390 /// The `ShrAssign` trait is used to specify the functionality of `>>=`.
1394 /// A trivial implementation of `ShrAssign`. When `Foo >>= Foo` happens, it ends up
1395 /// calling `shr_assign`, and therefore, `main` prints `Shifting right!`.
1398 /// #![feature(augmented_assignments)]
1399 /// #![feature(op_assign_traits)]
1401 /// use std::ops::ShrAssign;
1403 /// #[derive(Copy, Clone)]
1406 /// impl ShrAssign<Foo> for Foo {
1407 /// fn shr_assign(&mut self, _rhs: Foo) {
1408 /// println!("Shifting right!");
1413 /// let mut foo = Foo;
1418 #[lang = "shr_assign"]
1419 #[unstable(feature = "op_assign_traits", reason = "recently added", issue = "28235")]
1420 pub trait ShrAssign<Rhs=Self> {
1421 /// The method for the `>>=` operator
1422 fn shr_assign(&mut self, Rhs);
1426 macro_rules! shr_assign_impl {
1428 #[unstable(feature = "op_assign_traits", reason = "recently added", issue = "28235")]
1429 impl ShrAssign<$f> for $t {
1431 fn shr_assign(&mut self, other: $f) {
1439 macro_rules! shr_assign_impl_all {
1441 shr_assign_impl! { $t, u8 }
1442 shr_assign_impl! { $t, u16 }
1443 shr_assign_impl! { $t, u32 }
1444 shr_assign_impl! { $t, u64 }
1445 shr_assign_impl! { $t, usize }
1447 shr_assign_impl! { $t, i8 }
1448 shr_assign_impl! { $t, i16 }
1449 shr_assign_impl! { $t, i32 }
1450 shr_assign_impl! { $t, i64 }
1451 shr_assign_impl! { $t, isize }
1456 shr_assign_impl_all! { u8 u16 u32 u64 usize i8 i16 i32 i64 isize }
1458 /// The `Index` trait is used to specify the functionality of indexing operations
1459 /// like `arr[idx]` when used in an immutable context.
1463 /// A trivial implementation of `Index`. When `Foo[Bar]` happens, it ends up
1464 /// calling `index`, and therefore, `main` prints `Indexing!`.
1467 /// use std::ops::Index;
1469 /// #[derive(Copy, Clone)]
1473 /// impl Index<Bar> for Foo {
1474 /// type Output = Foo;
1476 /// fn index<'a>(&'a self, _index: Bar) -> &'a Foo {
1477 /// println!("Indexing!");
1487 #[rustc_on_unimplemented = "the type `{Self}` cannot be indexed by `{Idx}`"]
1488 #[stable(feature = "rust1", since = "1.0.0")]
1489 pub trait Index<Idx: ?Sized> {
1490 /// The returned type after indexing
1491 #[stable(feature = "rust1", since = "1.0.0")]
1492 type Output: ?Sized;
1494 /// The method for the indexing (`Foo[Bar]`) operation
1495 #[stable(feature = "rust1", since = "1.0.0")]
1496 fn index(&self, index: Idx) -> &Self::Output;
1499 /// The `IndexMut` trait is used to specify the functionality of indexing
1500 /// operations like `arr[idx]`, when used in a mutable context.
1504 /// A trivial implementation of `IndexMut`. When `Foo[Bar]` happens, it ends up
1505 /// calling `index_mut`, and therefore, `main` prints `Indexing!`.
1508 /// use std::ops::{Index, IndexMut};
1510 /// #[derive(Copy, Clone)]
1514 /// impl Index<Bar> for Foo {
1515 /// type Output = Foo;
1517 /// fn index<'a>(&'a self, _index: Bar) -> &'a Foo {
1522 /// impl IndexMut<Bar> for Foo {
1523 /// fn index_mut<'a>(&'a mut self, _index: Bar) -> &'a mut Foo {
1524 /// println!("Indexing!");
1533 #[lang = "index_mut"]
1534 #[rustc_on_unimplemented = "the type `{Self}` cannot be mutably indexed by `{Idx}`"]
1535 #[stable(feature = "rust1", since = "1.0.0")]
1536 pub trait IndexMut<Idx: ?Sized>: Index<Idx> {
1537 /// The method for the indexing (`Foo[Bar]`) operation
1538 #[stable(feature = "rust1", since = "1.0.0")]
1539 fn index_mut(&mut self, index: Idx) -> &mut Self::Output;
1542 /// An unbounded range.
1543 #[derive(Copy, Clone, PartialEq, Eq)]
1544 #[lang = "range_full"]
1545 #[stable(feature = "rust1", since = "1.0.0")]
1546 pub struct RangeFull;
1548 #[stable(feature = "rust1", since = "1.0.0")]
1549 impl fmt::Debug for RangeFull {
1550 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
1555 /// A (half-open) range which is bounded at both ends.
1556 #[derive(Clone, PartialEq, Eq)]
1558 #[stable(feature = "rust1", since = "1.0.0")]
1559 pub struct Range<Idx> {
1560 /// The lower bound of the range (inclusive).
1561 #[stable(feature = "rust1", since = "1.0.0")]
1563 /// The upper bound of the range (exclusive).
1564 #[stable(feature = "rust1", since = "1.0.0")]
1568 #[stable(feature = "rust1", since = "1.0.0")]
1569 impl<Idx: fmt::Debug> fmt::Debug for Range<Idx> {
1570 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
1571 write!(fmt, "{:?}..{:?}", self.start, self.end)
1575 /// A range which is only bounded below.
1576 #[derive(Clone, PartialEq, Eq)]
1577 #[lang = "range_from"]
1578 #[stable(feature = "rust1", since = "1.0.0")]
1579 pub struct RangeFrom<Idx> {
1580 /// The lower bound of the range (inclusive).
1581 #[stable(feature = "rust1", since = "1.0.0")]
1585 #[stable(feature = "rust1", since = "1.0.0")]
1586 impl<Idx: fmt::Debug> fmt::Debug for RangeFrom<Idx> {
1587 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
1588 write!(fmt, "{:?}..", self.start)
1592 /// A range which is only bounded above.
1593 #[derive(Copy, Clone, PartialEq, Eq)]
1594 #[lang = "range_to"]
1595 #[stable(feature = "rust1", since = "1.0.0")]
1596 pub struct RangeTo<Idx> {
1597 /// The upper bound of the range (exclusive).
1598 #[stable(feature = "rust1", since = "1.0.0")]
1602 #[stable(feature = "rust1", since = "1.0.0")]
1603 impl<Idx: fmt::Debug> fmt::Debug for RangeTo<Idx> {
1604 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
1605 write!(fmt, "..{:?}", self.end)
1609 /// The `Deref` trait is used to specify the functionality of dereferencing
1610 /// operations like `*v`.
1612 /// `Deref` also enables ['`Deref` coercions'][coercions].
1614 /// [coercions]: ../../book/deref-coercions.html
1618 /// A struct with a single field which is accessible via dereferencing the
1622 /// use std::ops::Deref;
1624 /// struct DerefExample<T> {
1628 /// impl<T> Deref for DerefExample<T> {
1629 /// type Target = T;
1631 /// fn deref(&self) -> &T {
1637 /// let x = DerefExample { value: 'a' };
1638 /// assert_eq!('a', *x);
1642 #[stable(feature = "rust1", since = "1.0.0")]
1644 /// The resulting type after dereferencing
1645 #[stable(feature = "rust1", since = "1.0.0")]
1646 type Target: ?Sized;
1648 /// The method called to dereference a value
1649 #[stable(feature = "rust1", since = "1.0.0")]
1650 fn deref(&self) -> &Self::Target;
1653 #[stable(feature = "rust1", since = "1.0.0")]
1654 impl<'a, T: ?Sized> Deref for &'a T {
1657 fn deref(&self) -> &T { *self }
1660 #[stable(feature = "rust1", since = "1.0.0")]
1661 impl<'a, T: ?Sized> Deref for &'a mut T {
1664 fn deref(&self) -> &T { *self }
1667 /// The `DerefMut` trait is used to specify the functionality of dereferencing
1668 /// mutably like `*v = 1;`
1670 /// `DerefMut` also enables ['`Deref` coercions'][coercions].
1672 /// [coercions]: ../../book/deref-coercions.html
1676 /// A struct with a single field which is modifiable via dereferencing the
1680 /// use std::ops::{Deref, DerefMut};
1682 /// struct DerefMutExample<T> {
1686 /// impl<T> Deref for DerefMutExample<T> {
1687 /// type Target = T;
1689 /// fn deref<'a>(&'a self) -> &'a T {
1694 /// impl<T> DerefMut for DerefMutExample<T> {
1695 /// fn deref_mut<'a>(&'a mut self) -> &'a mut T {
1701 /// let mut x = DerefMutExample { value: 'a' };
1703 /// assert_eq!('b', *x);
1706 #[lang = "deref_mut"]
1707 #[stable(feature = "rust1", since = "1.0.0")]
1708 pub trait DerefMut: Deref {
1709 /// The method called to mutably dereference a value
1710 #[stable(feature = "rust1", since = "1.0.0")]
1711 fn deref_mut(&mut self) -> &mut Self::Target;
1714 #[stable(feature = "rust1", since = "1.0.0")]
1715 impl<'a, T: ?Sized> DerefMut for &'a mut T {
1716 fn deref_mut(&mut self) -> &mut T { *self }
1719 /// A version of the call operator that takes an immutable receiver.
1721 #[stable(feature = "rust1", since = "1.0.0")]
1722 #[rustc_paren_sugar]
1723 #[fundamental] // so that regex can rely that `&str: !FnMut`
1724 pub trait Fn<Args> : FnMut<Args> {
1725 /// This is called when the call operator is used.
1726 extern "rust-call" fn call(&self, args: Args) -> Self::Output;
1729 /// A version of the call operator that takes a mutable receiver.
1731 #[stable(feature = "rust1", since = "1.0.0")]
1732 #[rustc_paren_sugar]
1733 #[fundamental] // so that regex can rely that `&str: !FnMut`
1734 pub trait FnMut<Args> : FnOnce<Args> {
1735 /// This is called when the call operator is used.
1736 extern "rust-call" fn call_mut(&mut self, args: Args) -> Self::Output;
1739 /// A version of the call operator that takes a by-value receiver.
1741 #[stable(feature = "rust1", since = "1.0.0")]
1742 #[rustc_paren_sugar]
1743 #[fundamental] // so that regex can rely that `&str: !FnMut`
1744 pub trait FnOnce<Args> {
1745 /// The returned type after the call operator is used.
1748 /// This is called when the call operator is used.
1749 extern "rust-call" fn call_once(self, args: Args) -> Self::Output;
1754 use super::{Fn, FnMut, FnOnce};
1756 impl<'a,A,F:?Sized> Fn<A> for &'a F
1759 extern "rust-call" fn call(&self, args: A) -> F::Output {
1764 impl<'a,A,F:?Sized> FnMut<A> for &'a F
1767 extern "rust-call" fn call_mut(&mut self, args: A) -> F::Output {
1772 impl<'a,A,F:?Sized> FnOnce<A> for &'a F
1775 type Output = F::Output;
1777 extern "rust-call" fn call_once(self, args: A) -> F::Output {
1782 impl<'a,A,F:?Sized> FnMut<A> for &'a mut F
1785 extern "rust-call" fn call_mut(&mut self, args: A) -> F::Output {
1786 (*self).call_mut(args)
1790 impl<'a,A,F:?Sized> FnOnce<A> for &'a mut F
1793 type Output = F::Output;
1794 extern "rust-call" fn call_once(mut self, args: A) -> F::Output {
1795 (*self).call_mut(args)
1800 /// Trait that indicates that this is a pointer or a wrapper for one,
1801 /// where unsizing can be performed on the pointee.
1802 #[unstable(feature = "coerce_unsized", issue = "27732")]
1803 #[lang="coerce_unsized"]
1804 pub trait CoerceUnsized<T> {
1809 impl<'a, T: ?Sized+Unsize<U>, U: ?Sized> CoerceUnsized<&'a mut U> for &'a mut T {}
1811 impl<'a, 'b: 'a, T: ?Sized+Unsize<U>, U: ?Sized> CoerceUnsized<&'a U> for &'b mut T {}
1813 impl<'a, T: ?Sized+Unsize<U>, U: ?Sized> CoerceUnsized<*mut U> for &'a mut T {}
1814 // &mut T -> *const U
1815 impl<'a, T: ?Sized+Unsize<U>, U: ?Sized> CoerceUnsized<*const U> for &'a mut T {}
1818 impl<'a, 'b: 'a, T: ?Sized+Unsize<U>, U: ?Sized> CoerceUnsized<&'a U> for &'b T {}
1820 impl<'a, T: ?Sized+Unsize<U>, U: ?Sized> CoerceUnsized<*const U> for &'a T {}
1823 impl<T: ?Sized+Unsize<U>, U: ?Sized> CoerceUnsized<*mut U> for *mut T {}
1824 // *mut T -> *const U
1825 impl<T: ?Sized+Unsize<U>, U: ?Sized> CoerceUnsized<*const U> for *mut T {}
1827 // *const T -> *const U
1828 impl<T: ?Sized+Unsize<U>, U: ?Sized> CoerceUnsized<*const U> for *const T {}
1830 /// Both `in (PLACE) EXPR` and `box EXPR` desugar into expressions
1831 /// that allocate an intermediate "place" that holds uninitialized
1832 /// state. The desugaring evaluates EXPR, and writes the result at
1833 /// the address returned by the `pointer` method of this trait.
1835 /// A `Place` can be thought of as a special representation for a
1836 /// hypothetical `&uninit` reference (which Rust cannot currently
1837 /// express directly). That is, it represents a pointer to
1838 /// uninitialized storage.
1840 /// The client is responsible for two steps: First, initializing the
1841 /// payload (it can access its address via `pointer`). Second,
1842 /// converting the agent to an instance of the owning pointer, via the
1843 /// appropriate `finalize` method (see the `InPlace`.
1845 /// If evaluating EXPR fails, then the destructor for the
1846 /// implementation of Place to clean up any intermediate state
1847 /// (e.g. deallocate box storage, pop a stack, etc).
1848 #[unstable(feature = "placement_new_protocol", issue = "27779")]
1849 pub trait Place<Data: ?Sized> {
1850 /// Returns the address where the input value will be written.
1851 /// Note that the data at this address is generally uninitialized,
1852 /// and thus one should use `ptr::write` for initializing it.
1853 fn pointer(&mut self) -> *mut Data;
1856 /// Interface to implementations of `in (PLACE) EXPR`.
1858 /// `in (PLACE) EXPR` effectively desugars into:
1862 /// let mut place = Placer::make_place(p);
1863 /// let raw_place = Place::pointer(&mut place);
1864 /// let value = EXPR;
1866 /// std::ptr::write(raw_place, value);
1867 /// InPlace::finalize(place)
1871 /// The type of `in (PLACE) EXPR` is derived from the type of `PLACE`;
1872 /// if the type of `PLACE` is `P`, then the final type of the whole
1873 /// expression is `P::Place::Owner` (see the `InPlace` and `Boxed`
1876 /// Values for types implementing this trait usually are transient
1877 /// intermediate values (e.g. the return value of `Vec::emplace_back`)
1878 /// or `Copy`, since the `make_place` method takes `self` by value.
1879 #[unstable(feature = "placement_new_protocol", issue = "27779")]
1880 pub trait Placer<Data: ?Sized> {
1881 /// `Place` is the intermedate agent guarding the
1882 /// uninitialized state for `Data`.
1883 type Place: InPlace<Data>;
1885 /// Creates a fresh place from `self`.
1886 fn make_place(self) -> Self::Place;
1889 /// Specialization of `Place` trait supporting `in (PLACE) EXPR`.
1890 #[unstable(feature = "placement_new_protocol", issue = "27779")]
1891 pub trait InPlace<Data: ?Sized>: Place<Data> {
1892 /// `Owner` is the type of the end value of `in (PLACE) EXPR`
1894 /// Note that when `in (PLACE) EXPR` is solely used for
1895 /// side-effecting an existing data-structure,
1896 /// e.g. `Vec::emplace_back`, then `Owner` need not carry any
1897 /// information at all (e.g. it can be the unit type `()` in that
1901 /// Converts self into the final value, shifting
1902 /// deallocation/cleanup responsibilities (if any remain), over to
1903 /// the returned instance of `Owner` and forgetting self.
1904 unsafe fn finalize(self) -> Self::Owner;
1907 /// Core trait for the `box EXPR` form.
1909 /// `box EXPR` effectively desugars into:
1912 /// let mut place = BoxPlace::make_place();
1913 /// let raw_place = Place::pointer(&mut place);
1914 /// let value = EXPR;
1916 /// ::std::ptr::write(raw_place, value);
1917 /// Boxed::finalize(place)
1921 /// The type of `box EXPR` is supplied from its surrounding
1922 /// context; in the above expansion, the result type `T` is used
1923 /// to determine which implementation of `Boxed` to use, and that
1924 /// `<T as Boxed>` in turn dictates determines which
1925 /// implementation of `BoxPlace` to use, namely:
1926 /// `<<T as Boxed>::Place as BoxPlace>`.
1927 #[unstable(feature = "placement_new_protocol", issue = "27779")]
1929 /// The kind of data that is stored in this kind of box.
1930 type Data; /* (`Data` unused b/c cannot yet express below bound.) */
1931 /// The place that will negotiate the storage of the data.
1932 type Place: BoxPlace<Self::Data>;
1934 /// Converts filled place into final owning value, shifting
1935 /// deallocation/cleanup responsibilities (if any remain), over to
1936 /// returned instance of `Self` and forgetting `filled`.
1937 unsafe fn finalize(filled: Self::Place) -> Self;
1940 /// Specialization of `Place` trait supporting `box EXPR`.
1941 #[unstable(feature = "placement_new_protocol", issue = "27779")]
1942 pub trait BoxPlace<Data: ?Sized> : Place<Data> {
1943 /// Creates a globally fresh place.
1944 fn make_place() -> Self;