2012-12-03 18:48:01 -06:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2014-11-25 20:17:11 -06:00
|
|
|
//! Overloadable operators
|
|
|
|
//!
|
|
|
|
//! Implementing these traits allows you to get an effect similar to
|
|
|
|
//! overloading operators.
|
|
|
|
//!
|
2015-01-16 01:31:01 -06:00
|
|
|
//! Some of these traits are imported by the prelude, so they are available in
|
2014-11-25 20:17:11 -06:00
|
|
|
//! every Rust program.
|
|
|
|
//!
|
2015-01-16 01:31:01 -06:00
|
|
|
//! Many of the operators take their operands by value. In non-generic
|
|
|
|
//! contexts involving built-in types, this is usually not a problem.
|
|
|
|
//! However, using these operators in generic code, requires some
|
|
|
|
//! attention if values have to be reused as opposed to letting the operators
|
|
|
|
//! consume them. One option is to occasionally use `clone()`.
|
|
|
|
//! Another option is to rely on the types involved providing additional
|
|
|
|
//! operator implementations for references. For example, for a user-defined
|
|
|
|
//! type `T` which is supposed to support addition, it is probably a good
|
|
|
|
//! idea to have both `T` and `&T` implement the traits `Add<T>` and `Add<&T>`
|
|
|
|
//! so that generic code can be written without unnecessary cloning.
|
|
|
|
//!
|
2014-11-25 20:17:11 -06:00
|
|
|
//! # Example
|
|
|
|
//!
|
|
|
|
//! This example creates a `Point` struct that implements `Add` and `Sub`, and then
|
|
|
|
//! demonstrates adding and subtracting two `Point`s.
|
|
|
|
//!
|
|
|
|
//! ```rust
|
2014-12-22 11:04:23 -06:00
|
|
|
//! use std::ops::{Add, Sub};
|
|
|
|
//!
|
2015-01-28 07:34:18 -06:00
|
|
|
//! #[derive(Debug)]
|
2014-11-25 20:17:11 -06:00
|
|
|
//! struct Point {
|
2015-02-15 09:22:43 -06:00
|
|
|
//! x: i32,
|
|
|
|
//! y: i32
|
2014-11-25 20:17:11 -06:00
|
|
|
//! }
|
|
|
|
//!
|
2014-12-31 14:45:13 -06:00
|
|
|
//! impl Add for Point {
|
|
|
|
//! type Output = Point;
|
|
|
|
//!
|
2014-12-01 18:10:12 -06:00
|
|
|
//! fn add(self, other: Point) -> Point {
|
2014-11-25 20:17:11 -06:00
|
|
|
//! Point {x: self.x + other.x, y: self.y + other.y}
|
|
|
|
//! }
|
|
|
|
//! }
|
|
|
|
//!
|
2014-12-31 14:45:13 -06:00
|
|
|
//! impl Sub for Point {
|
|
|
|
//! type Output = Point;
|
|
|
|
//!
|
2014-12-01 18:10:12 -06:00
|
|
|
//! fn sub(self, other: Point) -> Point {
|
2014-11-25 20:17:11 -06:00
|
|
|
//! Point {x: self.x - other.x, y: self.y - other.y}
|
|
|
|
//! }
|
|
|
|
//! }
|
|
|
|
//! fn main() {
|
2015-01-06 18:16:35 -06:00
|
|
|
//! println!("{:?}", Point {x: 1, y: 0} + Point {x: 2, y: 3});
|
|
|
|
//! println!("{:?}", Point {x: 1, y: 0} - Point {x: 2, y: 3});
|
2014-11-25 20:17:11 -06:00
|
|
|
//! }
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! See the documentation for each trait for a minimum implementation that prints
|
|
|
|
//! something to the screen.
|
2013-09-26 07:54:48 -05:00
|
|
|
|
2015-01-23 23:48:20 -06:00
|
|
|
#![stable(feature = "rust1", since = "1.0.0")]
|
2015-01-04 17:42:51 -06:00
|
|
|
|
2015-01-06 16:33:42 -06:00
|
|
|
use marker::Sized;
|
2015-01-06 22:21:09 -06:00
|
|
|
use fmt;
|
2014-09-05 00:10:32 -05:00
|
|
|
|
2014-11-24 19:06:06 -06:00
|
|
|
/// The `Drop` trait is used to run some code when a value goes out of scope. This
|
|
|
|
/// is sometimes called a 'destructor'.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// A trivial implementation of `Drop`. The `drop` method is called when `_x` goes
|
|
|
|
/// out of scope, and therefore `main` prints `Dropping!`.
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// struct HasDrop;
|
|
|
|
///
|
|
|
|
/// impl Drop for HasDrop {
|
2014-12-22 11:04:23 -06:00
|
|
|
/// fn drop(&mut self) {
|
|
|
|
/// println!("Dropping!");
|
|
|
|
/// }
|
2014-11-24 19:06:06 -06:00
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn main() {
|
2014-12-22 11:04:23 -06:00
|
|
|
/// let _x = HasDrop;
|
2014-11-24 19:06:06 -06:00
|
|
|
/// }
|
|
|
|
/// ```
|
2012-11-05 19:50:01 -06:00
|
|
|
#[lang="drop"]
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2012-11-05 19:50:01 -06:00
|
|
|
pub trait Drop {
|
2014-03-16 17:35:35 -05:00
|
|
|
/// The `drop` method, called when the value goes out of scope.
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2013-09-16 20:18:07 -05:00
|
|
|
fn drop(&mut self);
|
2012-11-05 19:50:01 -06:00
|
|
|
}
|
|
|
|
|
2015-01-15 22:44:19 -06:00
|
|
|
// implements the unary operator "op &T"
|
|
|
|
// based on "op T" where T is expected to be `Copy`able
|
|
|
|
macro_rules! forward_ref_unop {
|
|
|
|
(impl $imp:ident, $method:ident for $t:ty) => {
|
2015-01-22 20:22:03 -06:00
|
|
|
#[unstable(feature = "core",
|
2015-01-12 20:40:19 -06:00
|
|
|
reason = "recently added, waiting for dust to settle")]
|
2015-01-15 22:44:19 -06:00
|
|
|
impl<'a> $imp for &'a $t {
|
|
|
|
type Output = <$t as $imp>::Output;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn $method(self) -> <$t as $imp>::Output {
|
|
|
|
$imp::$method(*self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-16 04:22:46 -06:00
|
|
|
// implements binary operators "&T op U", "T op &U", "&T op &U"
|
|
|
|
// based on "T op U" where T and U are expected to be `Copy`able
|
|
|
|
macro_rules! forward_ref_binop {
|
2015-01-15 22:44:19 -06:00
|
|
|
(impl $imp:ident, $method:ident for $t:ty, $u:ty) => {
|
2015-01-22 20:22:03 -06:00
|
|
|
#[unstable(feature = "core",
|
2015-01-12 20:40:19 -06:00
|
|
|
reason = "recently added, waiting for dust to settle")]
|
2015-01-15 22:44:19 -06:00
|
|
|
impl<'a> $imp<$u> for &'a $t {
|
|
|
|
type Output = <$t as $imp<$u>>::Output;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn $method(self, other: $u) -> <$t as $imp<$u>>::Output {
|
|
|
|
$imp::$method(*self, other)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-22 20:22:03 -06:00
|
|
|
#[unstable(feature = "core",
|
2015-01-12 20:40:19 -06:00
|
|
|
reason = "recently added, waiting for dust to settle")]
|
2015-01-15 22:44:19 -06:00
|
|
|
impl<'a> $imp<&'a $u> for $t {
|
|
|
|
type Output = <$t as $imp<$u>>::Output;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn $method(self, other: &'a $u) -> <$t as $imp<$u>>::Output {
|
|
|
|
$imp::$method(self, *other)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-22 20:22:03 -06:00
|
|
|
#[unstable(feature = "core",
|
2015-01-12 20:40:19 -06:00
|
|
|
reason = "recently added, waiting for dust to settle")]
|
2015-01-15 22:44:19 -06:00
|
|
|
impl<'a, 'b> $imp<&'a $u> for &'b $t {
|
|
|
|
type Output = <$t as $imp<$u>>::Output;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn $method(self, other: &'a $u) -> <$t as $imp<$u>>::Output {
|
|
|
|
$imp::$method(*self, *other)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-24 19:06:06 -06:00
|
|
|
/// The `Add` trait is used to specify the functionality of `+`.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// A trivial implementation of `Add`. When `Foo + Foo` happens, it ends up
|
|
|
|
/// calling `add`, and therefore, `main` prints `Adding!`.
|
|
|
|
///
|
|
|
|
/// ```rust
|
2014-12-22 11:04:23 -06:00
|
|
|
/// use std::ops::Add;
|
|
|
|
///
|
2015-01-03 21:54:18 -06:00
|
|
|
/// #[derive(Copy)]
|
2014-11-24 19:06:06 -06:00
|
|
|
/// struct Foo;
|
|
|
|
///
|
2014-12-31 14:45:13 -06:00
|
|
|
/// impl Add for Foo {
|
|
|
|
/// type Output = Foo;
|
|
|
|
///
|
2014-12-01 18:10:12 -06:00
|
|
|
/// fn add(self, _rhs: Foo) -> Foo {
|
2014-12-01 12:00:23 -06:00
|
|
|
/// println!("Adding!");
|
2014-12-01 18:10:12 -06:00
|
|
|
/// self
|
2014-12-01 12:00:23 -06:00
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn main() {
|
|
|
|
/// Foo + Foo;
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
#[lang="add"]
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-31 14:45:13 -06:00
|
|
|
pub trait Add<RHS=Self> {
|
2015-02-23 13:05:55 -06:00
|
|
|
/// The resulting type after applying the `+` operator
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-31 14:45:13 -06:00
|
|
|
type Output;
|
|
|
|
|
2014-12-01 12:00:23 -06:00
|
|
|
/// The method for the `+` operator
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-31 14:45:13 -06:00
|
|
|
fn add(self, rhs: RHS) -> Self::Output;
|
2014-12-01 12:00:23 -06:00
|
|
|
}
|
|
|
|
|
2014-11-14 11:18:10 -06:00
|
|
|
macro_rules! add_impl {
|
2014-12-01 12:00:23 -06:00
|
|
|
($($t:ty)*) => ($(
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-31 14:45:13 -06:00
|
|
|
impl Add for $t {
|
|
|
|
type Output = $t;
|
|
|
|
|
2014-12-01 12:00:23 -06:00
|
|
|
#[inline]
|
|
|
|
fn add(self, other: $t) -> $t { self + other }
|
|
|
|
}
|
2015-01-15 22:44:19 -06:00
|
|
|
|
|
|
|
forward_ref_binop! { impl Add, add for $t, $t }
|
2014-12-01 12:00:23 -06:00
|
|
|
)*)
|
2014-11-14 11:18:10 -06:00
|
|
|
}
|
2014-12-01 12:00:23 -06:00
|
|
|
|
2015-02-15 09:22:43 -06:00
|
|
|
add_impl! { usize u8 u16 u32 u64 isize i8 i16 i32 i64 f32 f64 }
|
2014-05-26 12:33:04 -05:00
|
|
|
|
2014-11-24 19:06:06 -06:00
|
|
|
/// The `Sub` trait is used to specify the functionality of `-`.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// A trivial implementation of `Sub`. When `Foo - Foo` happens, it ends up
|
|
|
|
/// calling `sub`, and therefore, `main` prints `Subtracting!`.
|
|
|
|
///
|
|
|
|
/// ```rust
|
2014-12-22 11:04:23 -06:00
|
|
|
/// use std::ops::Sub;
|
|
|
|
///
|
2015-01-03 21:54:18 -06:00
|
|
|
/// #[derive(Copy)]
|
2014-11-24 19:06:06 -06:00
|
|
|
/// struct Foo;
|
|
|
|
///
|
2014-12-31 14:45:13 -06:00
|
|
|
/// impl Sub for Foo {
|
|
|
|
/// type Output = Foo;
|
|
|
|
///
|
2014-12-01 18:10:12 -06:00
|
|
|
/// fn sub(self, _rhs: Foo) -> Foo {
|
2014-12-01 12:00:23 -06:00
|
|
|
/// println!("Subtracting!");
|
2014-12-01 18:10:12 -06:00
|
|
|
/// self
|
2014-12-01 12:00:23 -06:00
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn main() {
|
|
|
|
/// Foo - Foo;
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
#[lang="sub"]
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-31 14:45:13 -06:00
|
|
|
pub trait Sub<RHS=Self> {
|
2015-02-23 13:05:55 -06:00
|
|
|
/// The resulting type after applying the `-` operator
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-31 14:45:13 -06:00
|
|
|
type Output;
|
|
|
|
|
2014-12-01 12:00:23 -06:00
|
|
|
/// The method for the `-` operator
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-31 14:45:13 -06:00
|
|
|
fn sub(self, rhs: RHS) -> Self::Output;
|
2014-12-01 12:00:23 -06:00
|
|
|
}
|
|
|
|
|
2014-11-14 11:18:10 -06:00
|
|
|
macro_rules! sub_impl {
|
2014-12-01 12:00:23 -06:00
|
|
|
($($t:ty)*) => ($(
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-31 14:45:13 -06:00
|
|
|
impl Sub for $t {
|
|
|
|
type Output = $t;
|
|
|
|
|
2014-12-01 12:00:23 -06:00
|
|
|
#[inline]
|
|
|
|
fn sub(self, other: $t) -> $t { self - other }
|
|
|
|
}
|
2015-01-15 22:44:19 -06:00
|
|
|
|
|
|
|
forward_ref_binop! { impl Sub, sub for $t, $t }
|
2014-12-01 12:00:23 -06:00
|
|
|
)*)
|
2014-11-14 11:18:10 -06:00
|
|
|
}
|
2014-12-01 12:00:23 -06:00
|
|
|
|
2015-02-15 09:22:43 -06:00
|
|
|
sub_impl! { usize u8 u16 u32 u64 isize i8 i16 i32 i64 f32 f64 }
|
2014-05-26 12:33:04 -05:00
|
|
|
|
2014-11-24 19:06:06 -06:00
|
|
|
/// The `Mul` trait is used to specify the functionality of `*`.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// A trivial implementation of `Mul`. When `Foo * Foo` happens, it ends up
|
|
|
|
/// calling `mul`, and therefore, `main` prints `Multiplying!`.
|
|
|
|
///
|
|
|
|
/// ```rust
|
2014-12-22 11:04:23 -06:00
|
|
|
/// use std::ops::Mul;
|
|
|
|
///
|
2015-01-03 21:54:18 -06:00
|
|
|
/// #[derive(Copy)]
|
2014-11-24 19:06:06 -06:00
|
|
|
/// struct Foo;
|
|
|
|
///
|
2014-12-31 14:45:13 -06:00
|
|
|
/// impl Mul for Foo {
|
|
|
|
/// type Output = Foo;
|
|
|
|
///
|
2014-12-01 18:10:12 -06:00
|
|
|
/// fn mul(self, _rhs: Foo) -> Foo {
|
2014-12-01 12:00:23 -06:00
|
|
|
/// println!("Multiplying!");
|
2014-12-01 18:10:12 -06:00
|
|
|
/// self
|
2014-12-01 12:00:23 -06:00
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn main() {
|
|
|
|
/// Foo * Foo;
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
#[lang="mul"]
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-31 14:45:13 -06:00
|
|
|
pub trait Mul<RHS=Self> {
|
2015-02-23 13:05:55 -06:00
|
|
|
/// The resulting type after applying the `*` operator
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-31 14:45:13 -06:00
|
|
|
type Output;
|
|
|
|
|
2014-12-01 12:00:23 -06:00
|
|
|
/// The method for the `*` operator
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-31 14:45:13 -06:00
|
|
|
fn mul(self, rhs: RHS) -> Self::Output;
|
2014-12-01 12:00:23 -06:00
|
|
|
}
|
|
|
|
|
2014-11-14 11:18:10 -06:00
|
|
|
macro_rules! mul_impl {
|
2014-12-01 12:00:23 -06:00
|
|
|
($($t:ty)*) => ($(
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-31 14:45:13 -06:00
|
|
|
impl Mul for $t {
|
|
|
|
type Output = $t;
|
|
|
|
|
2014-12-01 12:00:23 -06:00
|
|
|
#[inline]
|
|
|
|
fn mul(self, other: $t) -> $t { self * other }
|
|
|
|
}
|
2015-01-15 22:44:19 -06:00
|
|
|
|
|
|
|
forward_ref_binop! { impl Mul, mul for $t, $t }
|
2014-12-01 12:00:23 -06:00
|
|
|
)*)
|
2014-11-14 11:18:10 -06:00
|
|
|
}
|
2014-12-01 12:00:23 -06:00
|
|
|
|
2015-02-15 09:22:43 -06:00
|
|
|
mul_impl! { usize u8 u16 u32 u64 isize i8 i16 i32 i64 f32 f64 }
|
2014-05-26 12:33:04 -05:00
|
|
|
|
2014-11-24 19:06:06 -06:00
|
|
|
/// The `Div` trait is used to specify the functionality of `/`.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// A trivial implementation of `Div`. When `Foo / Foo` happens, it ends up
|
|
|
|
/// calling `div`, and therefore, `main` prints `Dividing!`.
|
|
|
|
///
|
|
|
|
/// ```
|
2014-12-22 11:04:23 -06:00
|
|
|
/// use std::ops::Div;
|
|
|
|
///
|
2015-01-03 21:54:18 -06:00
|
|
|
/// #[derive(Copy)]
|
2014-11-24 19:06:06 -06:00
|
|
|
/// struct Foo;
|
|
|
|
///
|
2014-12-31 14:45:13 -06:00
|
|
|
/// impl Div for Foo {
|
|
|
|
/// type Output = Foo;
|
|
|
|
///
|
2014-12-01 18:10:12 -06:00
|
|
|
/// fn div(self, _rhs: Foo) -> Foo {
|
2014-12-01 12:00:23 -06:00
|
|
|
/// println!("Dividing!");
|
2014-12-01 18:10:12 -06:00
|
|
|
/// self
|
2014-12-01 12:00:23 -06:00
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn main() {
|
|
|
|
/// Foo / Foo;
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
#[lang="div"]
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-31 14:45:13 -06:00
|
|
|
pub trait Div<RHS=Self> {
|
2015-02-23 13:05:55 -06:00
|
|
|
/// The resulting type after applying the `/` operator
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-31 14:45:13 -06:00
|
|
|
type Output;
|
|
|
|
|
2014-12-01 12:00:23 -06:00
|
|
|
/// The method for the `/` operator
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-31 14:45:13 -06:00
|
|
|
fn div(self, rhs: RHS) -> Self::Output;
|
2014-12-01 12:00:23 -06:00
|
|
|
}
|
|
|
|
|
2014-11-14 11:18:10 -06:00
|
|
|
macro_rules! div_impl {
|
2014-12-01 12:00:23 -06:00
|
|
|
($($t:ty)*) => ($(
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-31 14:45:13 -06:00
|
|
|
impl Div for $t {
|
|
|
|
type Output = $t;
|
|
|
|
|
2014-12-01 12:00:23 -06:00
|
|
|
#[inline]
|
|
|
|
fn div(self, other: $t) -> $t { self / other }
|
|
|
|
}
|
2015-01-15 22:44:19 -06:00
|
|
|
|
|
|
|
forward_ref_binop! { impl Div, div for $t, $t }
|
2014-12-01 12:00:23 -06:00
|
|
|
)*)
|
2014-11-14 11:18:10 -06:00
|
|
|
}
|
2014-12-01 12:00:23 -06:00
|
|
|
|
2015-02-15 09:22:43 -06:00
|
|
|
div_impl! { usize u8 u16 u32 u64 isize i8 i16 i32 i64 f32 f64 }
|
2014-05-26 12:33:04 -05:00
|
|
|
|
2014-11-24 19:06:06 -06:00
|
|
|
/// The `Rem` trait is used to specify the functionality of `%`.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// A trivial implementation of `Rem`. When `Foo % Foo` happens, it ends up
|
|
|
|
/// calling `rem`, and therefore, `main` prints `Remainder-ing!`.
|
|
|
|
///
|
|
|
|
/// ```
|
2014-12-22 11:04:23 -06:00
|
|
|
/// use std::ops::Rem;
|
|
|
|
///
|
2015-01-03 21:54:18 -06:00
|
|
|
/// #[derive(Copy)]
|
2014-11-24 19:06:06 -06:00
|
|
|
/// struct Foo;
|
|
|
|
///
|
2014-12-31 14:45:13 -06:00
|
|
|
/// impl Rem for Foo {
|
|
|
|
/// type Output = Foo;
|
|
|
|
///
|
2014-12-01 18:10:12 -06:00
|
|
|
/// fn rem(self, _rhs: Foo) -> Foo {
|
2014-12-01 12:00:23 -06:00
|
|
|
/// println!("Remainder-ing!");
|
2014-12-01 18:10:12 -06:00
|
|
|
/// self
|
2014-12-01 12:00:23 -06:00
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn main() {
|
|
|
|
/// Foo % Foo;
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
#[lang="rem"]
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-31 14:45:13 -06:00
|
|
|
pub trait Rem<RHS=Self> {
|
2015-02-23 13:05:55 -06:00
|
|
|
/// The resulting type after applying the `%` operator
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-31 14:45:13 -06:00
|
|
|
type Output = Self;
|
|
|
|
|
2014-12-01 12:00:23 -06:00
|
|
|
/// The method for the `%` operator
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-31 14:45:13 -06:00
|
|
|
fn rem(self, rhs: RHS) -> Self::Output;
|
2014-12-01 12:00:23 -06:00
|
|
|
}
|
|
|
|
|
2014-11-14 11:18:10 -06:00
|
|
|
macro_rules! rem_impl {
|
2014-12-01 12:00:23 -06:00
|
|
|
($($t:ty)*) => ($(
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-31 14:45:13 -06:00
|
|
|
impl Rem for $t {
|
|
|
|
type Output = $t;
|
|
|
|
|
2014-12-01 12:00:23 -06:00
|
|
|
#[inline]
|
|
|
|
fn rem(self, other: $t) -> $t { self % other }
|
|
|
|
}
|
2015-01-15 22:44:19 -06:00
|
|
|
|
|
|
|
forward_ref_binop! { impl Rem, rem for $t, $t }
|
2014-12-01 12:00:23 -06:00
|
|
|
)*)
|
2014-11-14 11:18:10 -06:00
|
|
|
}
|
2014-12-01 12:00:23 -06:00
|
|
|
|
2014-11-14 11:18:10 -06:00
|
|
|
macro_rules! rem_float_impl {
|
2014-12-01 12:00:23 -06:00
|
|
|
($t:ty, $fmod:ident) => {
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-31 14:45:13 -06:00
|
|
|
impl Rem for $t {
|
|
|
|
type Output = $t;
|
|
|
|
|
2014-12-01 12:00:23 -06:00
|
|
|
#[inline]
|
|
|
|
fn rem(self, other: $t) -> $t {
|
|
|
|
extern { fn $fmod(a: $t, b: $t) -> $t; }
|
|
|
|
unsafe { $fmod(self, other) }
|
|
|
|
}
|
|
|
|
}
|
2015-01-15 22:44:19 -06:00
|
|
|
|
|
|
|
forward_ref_binop! { impl Rem, rem for $t, $t }
|
2014-12-01 12:00:23 -06:00
|
|
|
}
|
2014-11-14 11:18:10 -06:00
|
|
|
}
|
2014-12-01 12:00:23 -06:00
|
|
|
|
2015-02-15 09:22:43 -06:00
|
|
|
rem_impl! { usize u8 u16 u32 u64 isize i8 i16 i32 i64 }
|
2014-11-14 11:18:10 -06:00
|
|
|
rem_float_impl! { f32, fmodf }
|
|
|
|
rem_float_impl! { f64, fmod }
|
2014-05-26 12:33:04 -05:00
|
|
|
|
2014-12-15 15:24:24 -06:00
|
|
|
/// The `Neg` trait is used to specify the functionality of unary `-`.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// A trivial implementation of `Neg`. When `-Foo` happens, it ends up calling
|
|
|
|
/// `neg`, and therefore, `main` prints `Negating!`.
|
|
|
|
///
|
|
|
|
/// ```
|
2014-12-22 11:04:23 -06:00
|
|
|
/// use std::ops::Neg;
|
|
|
|
///
|
2015-01-24 15:36:30 -06:00
|
|
|
/// #[derive(Copy)]
|
2014-12-15 15:24:24 -06:00
|
|
|
/// struct Foo;
|
|
|
|
///
|
2015-01-02 21:56:24 -06:00
|
|
|
/// impl Neg for Foo {
|
|
|
|
/// type Output = Foo;
|
|
|
|
///
|
2014-12-15 15:24:24 -06:00
|
|
|
/// fn neg(self) -> Foo {
|
|
|
|
/// println!("Negating!");
|
|
|
|
/// self
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn main() {
|
|
|
|
/// -Foo;
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
#[lang="neg"]
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-02 21:56:24 -06:00
|
|
|
pub trait Neg {
|
2015-02-23 13:05:55 -06:00
|
|
|
/// The resulting type after applying the `-` operator
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-02 21:56:24 -06:00
|
|
|
type Output;
|
|
|
|
|
2014-12-15 15:24:24 -06:00
|
|
|
/// The method for the unary `-` operator
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-02 21:56:24 -06:00
|
|
|
fn neg(self) -> Self::Output;
|
2014-12-15 15:24:24 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! neg_impl {
|
|
|
|
($($t:ty)*) => ($(
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-02 21:56:24 -06:00
|
|
|
impl Neg for $t {
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-02 21:56:24 -06:00
|
|
|
type Output = $t;
|
|
|
|
|
2014-12-15 15:24:24 -06:00
|
|
|
#[inline]
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-15 15:24:24 -06:00
|
|
|
fn neg(self) -> $t { -self }
|
|
|
|
}
|
2015-01-15 22:44:19 -06:00
|
|
|
|
|
|
|
forward_ref_unop! { impl Neg, neg for $t }
|
2014-12-15 15:24:24 -06:00
|
|
|
)*)
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! neg_uint_impl {
|
|
|
|
($t:ty, $t_signed:ty) => {
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-02 21:56:24 -06:00
|
|
|
impl Neg for $t {
|
|
|
|
type Output = $t;
|
|
|
|
|
2014-12-15 15:24:24 -06:00
|
|
|
#[inline]
|
|
|
|
fn neg(self) -> $t { -(self as $t_signed) as $t }
|
|
|
|
}
|
2015-01-15 22:44:19 -06:00
|
|
|
|
|
|
|
forward_ref_unop! { impl Neg, neg for $t }
|
2014-12-15 15:24:24 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-15 09:22:43 -06:00
|
|
|
neg_impl! { isize i8 i16 i32 i64 f32 f64 }
|
2014-05-26 12:33:04 -05:00
|
|
|
|
2015-02-15 09:22:43 -06:00
|
|
|
neg_uint_impl! { usize, isize }
|
2014-11-14 11:18:10 -06:00
|
|
|
neg_uint_impl! { u8, i8 }
|
|
|
|
neg_uint_impl! { u16, i16 }
|
|
|
|
neg_uint_impl! { u32, i32 }
|
|
|
|
neg_uint_impl! { u64, i64 }
|
2014-05-26 12:33:04 -05:00
|
|
|
|
|
|
|
|
2014-12-15 15:24:24 -06:00
|
|
|
/// The `Not` trait is used to specify the functionality of unary `!`.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// A trivial implementation of `Not`. When `!Foo` happens, it ends up calling
|
|
|
|
/// `not`, and therefore, `main` prints `Not-ing!`.
|
|
|
|
///
|
|
|
|
/// ```
|
2014-12-22 11:04:23 -06:00
|
|
|
/// use std::ops::Not;
|
|
|
|
///
|
2015-01-24 15:36:30 -06:00
|
|
|
/// #[derive(Copy)]
|
2014-12-15 15:24:24 -06:00
|
|
|
/// struct Foo;
|
|
|
|
///
|
2015-01-02 21:56:24 -06:00
|
|
|
/// impl Not for Foo {
|
|
|
|
/// type Output = Foo;
|
|
|
|
///
|
2014-12-15 15:24:24 -06:00
|
|
|
/// fn not(self) -> Foo {
|
|
|
|
/// println!("Not-ing!");
|
|
|
|
/// self
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn main() {
|
|
|
|
/// !Foo;
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
#[lang="not"]
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-02 21:56:24 -06:00
|
|
|
pub trait Not {
|
2015-02-23 13:05:55 -06:00
|
|
|
/// The resulting type after applying the `!` operator
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-02 21:56:24 -06:00
|
|
|
type Output;
|
|
|
|
|
2014-12-15 15:24:24 -06:00
|
|
|
/// The method for the unary `!` operator
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-02 21:56:24 -06:00
|
|
|
fn not(self) -> Self::Output;
|
2014-12-15 15:24:24 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! not_impl {
|
|
|
|
($($t:ty)*) => ($(
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-02 21:56:24 -06:00
|
|
|
impl Not for $t {
|
|
|
|
type Output = $t;
|
|
|
|
|
2014-12-15 15:24:24 -06:00
|
|
|
#[inline]
|
|
|
|
fn not(self) -> $t { !self }
|
|
|
|
}
|
2015-01-15 22:44:19 -06:00
|
|
|
|
|
|
|
forward_ref_unop! { impl Not, not for $t }
|
2014-12-15 15:24:24 -06:00
|
|
|
)*)
|
|
|
|
}
|
|
|
|
|
2015-02-15 09:22:43 -06:00
|
|
|
not_impl! { bool usize u8 u16 u32 u64 isize i8 i16 i32 i64 }
|
2014-05-26 12:33:04 -05:00
|
|
|
|
2014-11-24 19:06:06 -06:00
|
|
|
/// The `BitAnd` trait is used to specify the functionality of `&`.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// A trivial implementation of `BitAnd`. When `Foo & Foo` happens, it ends up
|
|
|
|
/// calling `bitand`, and therefore, `main` prints `Bitwise And-ing!`.
|
|
|
|
///
|
|
|
|
/// ```
|
2014-12-22 11:04:23 -06:00
|
|
|
/// use std::ops::BitAnd;
|
|
|
|
///
|
2015-01-03 21:54:18 -06:00
|
|
|
/// #[derive(Copy)]
|
2014-11-24 19:06:06 -06:00
|
|
|
/// struct Foo;
|
|
|
|
///
|
2014-12-31 14:45:13 -06:00
|
|
|
/// impl BitAnd for Foo {
|
|
|
|
/// type Output = Foo;
|
|
|
|
///
|
2014-12-01 18:10:12 -06:00
|
|
|
/// fn bitand(self, _rhs: Foo) -> Foo {
|
2014-12-01 12:00:23 -06:00
|
|
|
/// println!("Bitwise And-ing!");
|
2014-12-01 18:10:12 -06:00
|
|
|
/// self
|
2014-12-01 12:00:23 -06:00
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn main() {
|
|
|
|
/// Foo & Foo;
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
#[lang="bitand"]
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-31 14:45:13 -06:00
|
|
|
pub trait BitAnd<RHS=Self> {
|
2015-02-23 13:05:55 -06:00
|
|
|
/// The resulting type after applying the `&` operator
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-31 14:45:13 -06:00
|
|
|
type Output;
|
|
|
|
|
2014-12-01 12:00:23 -06:00
|
|
|
/// The method for the `&` operator
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-31 14:45:13 -06:00
|
|
|
fn bitand(self, rhs: RHS) -> Self::Output;
|
2014-12-01 12:00:23 -06:00
|
|
|
}
|
|
|
|
|
2014-11-14 11:18:10 -06:00
|
|
|
macro_rules! bitand_impl {
|
2014-12-01 12:00:23 -06:00
|
|
|
($($t:ty)*) => ($(
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-31 14:45:13 -06:00
|
|
|
impl BitAnd for $t {
|
|
|
|
type Output = $t;
|
|
|
|
|
2014-12-01 12:00:23 -06:00
|
|
|
#[inline]
|
|
|
|
fn bitand(self, rhs: $t) -> $t { self & rhs }
|
|
|
|
}
|
2015-01-15 22:44:19 -06:00
|
|
|
|
|
|
|
forward_ref_binop! { impl BitAnd, bitand for $t, $t }
|
2014-12-01 12:00:23 -06:00
|
|
|
)*)
|
2014-11-14 11:18:10 -06:00
|
|
|
}
|
2014-12-01 12:00:23 -06:00
|
|
|
|
2015-02-15 09:22:43 -06:00
|
|
|
bitand_impl! { bool usize u8 u16 u32 u64 isize i8 i16 i32 i64 }
|
2014-05-26 12:33:04 -05:00
|
|
|
|
2014-11-24 19:06:06 -06:00
|
|
|
/// The `BitOr` trait is used to specify the functionality of `|`.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// A trivial implementation of `BitOr`. When `Foo | Foo` happens, it ends up
|
|
|
|
/// calling `bitor`, and therefore, `main` prints `Bitwise Or-ing!`.
|
|
|
|
///
|
|
|
|
/// ```
|
2014-12-22 11:04:23 -06:00
|
|
|
/// use std::ops::BitOr;
|
|
|
|
///
|
2015-01-03 21:54:18 -06:00
|
|
|
/// #[derive(Copy)]
|
2014-11-24 19:06:06 -06:00
|
|
|
/// struct Foo;
|
|
|
|
///
|
2014-12-31 14:45:13 -06:00
|
|
|
/// impl BitOr for Foo {
|
|
|
|
/// type Output = Foo;
|
|
|
|
///
|
2014-12-01 18:10:12 -06:00
|
|
|
/// fn bitor(self, _rhs: Foo) -> Foo {
|
2014-12-01 12:00:23 -06:00
|
|
|
/// println!("Bitwise Or-ing!");
|
2014-12-01 18:10:12 -06:00
|
|
|
/// self
|
2014-12-01 12:00:23 -06:00
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn main() {
|
|
|
|
/// Foo | Foo;
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
#[lang="bitor"]
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-31 14:45:13 -06:00
|
|
|
pub trait BitOr<RHS=Self> {
|
2015-02-23 13:05:55 -06:00
|
|
|
/// The resulting type after applying the `|` operator
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-31 14:45:13 -06:00
|
|
|
type Output;
|
|
|
|
|
2014-12-01 12:00:23 -06:00
|
|
|
/// The method for the `|` operator
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-31 14:45:13 -06:00
|
|
|
fn bitor(self, rhs: RHS) -> Self::Output;
|
2014-12-01 12:00:23 -06:00
|
|
|
}
|
|
|
|
|
2014-11-14 11:18:10 -06:00
|
|
|
macro_rules! bitor_impl {
|
2014-12-01 12:00:23 -06:00
|
|
|
($($t:ty)*) => ($(
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-31 14:45:13 -06:00
|
|
|
impl BitOr for $t {
|
|
|
|
type Output = $t;
|
|
|
|
|
2014-12-01 12:00:23 -06:00
|
|
|
#[inline]
|
|
|
|
fn bitor(self, rhs: $t) -> $t { self | rhs }
|
|
|
|
}
|
2015-01-15 22:44:19 -06:00
|
|
|
|
|
|
|
forward_ref_binop! { impl BitOr, bitor for $t, $t }
|
2014-12-01 12:00:23 -06:00
|
|
|
)*)
|
2014-11-14 11:18:10 -06:00
|
|
|
}
|
2014-12-01 12:00:23 -06:00
|
|
|
|
2015-02-15 09:22:43 -06:00
|
|
|
bitor_impl! { bool usize u8 u16 u32 u64 isize i8 i16 i32 i64 }
|
2014-05-26 12:33:04 -05:00
|
|
|
|
2014-11-24 19:06:06 -06:00
|
|
|
/// The `BitXor` trait is used to specify the functionality of `^`.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// A trivial implementation of `BitXor`. When `Foo ^ Foo` happens, it ends up
|
|
|
|
/// calling `bitxor`, and therefore, `main` prints `Bitwise Xor-ing!`.
|
|
|
|
///
|
|
|
|
/// ```
|
2014-12-22 11:04:23 -06:00
|
|
|
/// use std::ops::BitXor;
|
|
|
|
///
|
2015-01-03 21:54:18 -06:00
|
|
|
/// #[derive(Copy)]
|
2014-11-24 19:06:06 -06:00
|
|
|
/// struct Foo;
|
|
|
|
///
|
2014-12-31 14:45:13 -06:00
|
|
|
/// impl BitXor for Foo {
|
|
|
|
/// type Output = Foo;
|
|
|
|
///
|
2014-12-01 18:10:12 -06:00
|
|
|
/// fn bitxor(self, _rhs: Foo) -> Foo {
|
2014-12-01 12:00:23 -06:00
|
|
|
/// println!("Bitwise Xor-ing!");
|
2014-12-01 18:10:12 -06:00
|
|
|
/// self
|
2014-12-01 12:00:23 -06:00
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn main() {
|
|
|
|
/// Foo ^ Foo;
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
#[lang="bitxor"]
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-31 14:45:13 -06:00
|
|
|
pub trait BitXor<RHS=Self> {
|
2015-02-23 13:05:55 -06:00
|
|
|
/// The resulting type after applying the `^` operator
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-31 14:45:13 -06:00
|
|
|
type Output;
|
|
|
|
|
2014-12-01 12:00:23 -06:00
|
|
|
/// The method for the `^` operator
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-31 14:45:13 -06:00
|
|
|
fn bitxor(self, rhs: RHS) -> Self::Output;
|
2014-12-01 12:00:23 -06:00
|
|
|
}
|
|
|
|
|
2014-11-14 11:18:10 -06:00
|
|
|
macro_rules! bitxor_impl {
|
2014-12-01 12:00:23 -06:00
|
|
|
($($t:ty)*) => ($(
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-31 14:45:13 -06:00
|
|
|
impl BitXor for $t {
|
|
|
|
type Output = $t;
|
|
|
|
|
2014-12-01 12:00:23 -06:00
|
|
|
#[inline]
|
|
|
|
fn bitxor(self, other: $t) -> $t { self ^ other }
|
|
|
|
}
|
2015-01-15 22:44:19 -06:00
|
|
|
|
|
|
|
forward_ref_binop! { impl BitXor, bitxor for $t, $t }
|
2014-12-01 12:00:23 -06:00
|
|
|
)*)
|
2014-11-14 11:18:10 -06:00
|
|
|
}
|
2014-12-01 12:00:23 -06:00
|
|
|
|
2015-02-15 09:22:43 -06:00
|
|
|
bitxor_impl! { bool usize u8 u16 u32 u64 isize i8 i16 i32 i64 }
|
2014-05-26 12:33:04 -05:00
|
|
|
|
2014-11-24 19:06:06 -06:00
|
|
|
/// The `Shl` trait is used to specify the functionality of `<<`.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// A trivial implementation of `Shl`. When `Foo << Foo` happens, it ends up
|
|
|
|
/// calling `shl`, and therefore, `main` prints `Shifting left!`.
|
|
|
|
///
|
|
|
|
/// ```
|
2014-12-22 11:04:23 -06:00
|
|
|
/// use std::ops::Shl;
|
|
|
|
///
|
2015-01-03 21:54:18 -06:00
|
|
|
/// #[derive(Copy)]
|
2014-11-24 19:06:06 -06:00
|
|
|
/// struct Foo;
|
|
|
|
///
|
2014-12-31 14:45:13 -06:00
|
|
|
/// impl Shl<Foo> for Foo {
|
|
|
|
/// type Output = Foo;
|
|
|
|
///
|
2014-12-01 18:10:12 -06:00
|
|
|
/// fn shl(self, _rhs: Foo) -> Foo {
|
2014-12-01 12:00:23 -06:00
|
|
|
/// println!("Shifting left!");
|
2014-12-01 18:10:12 -06:00
|
|
|
/// self
|
2014-12-01 12:00:23 -06:00
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn main() {
|
|
|
|
/// Foo << Foo;
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
#[lang="shl"]
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-31 14:45:13 -06:00
|
|
|
pub trait Shl<RHS> {
|
2015-02-23 13:05:55 -06:00
|
|
|
/// The resulting type after applying the `<<` operator
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-31 14:45:13 -06:00
|
|
|
type Output;
|
|
|
|
|
2014-12-01 12:00:23 -06:00
|
|
|
/// The method for the `<<` operator
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-31 14:45:13 -06:00
|
|
|
fn shl(self, rhs: RHS) -> Self::Output;
|
2014-12-01 12:00:23 -06:00
|
|
|
}
|
|
|
|
|
2014-11-14 11:18:10 -06:00
|
|
|
macro_rules! shl_impl {
|
2015-01-10 16:38:39 -06:00
|
|
|
($t:ty, $f:ty) => (
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-10 16:38:39 -06:00
|
|
|
impl Shl<$f> for $t {
|
2014-12-31 14:45:13 -06:00
|
|
|
type Output = $t;
|
|
|
|
|
2014-12-01 12:00:23 -06:00
|
|
|
#[inline]
|
2015-01-10 16:38:39 -06:00
|
|
|
fn shl(self, other: $f) -> $t {
|
2014-12-01 12:00:23 -06:00
|
|
|
self << other
|
|
|
|
}
|
|
|
|
}
|
2015-01-15 22:44:19 -06:00
|
|
|
|
|
|
|
forward_ref_binop! { impl Shl, shl for $t, $f }
|
2015-01-10 16:38:39 -06:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! shl_impl_all {
|
|
|
|
($($t:ty)*) => ($(
|
|
|
|
shl_impl! { $t, u8 }
|
|
|
|
shl_impl! { $t, u16 }
|
|
|
|
shl_impl! { $t, u32 }
|
|
|
|
shl_impl! { $t, u64 }
|
|
|
|
shl_impl! { $t, usize }
|
|
|
|
|
|
|
|
shl_impl! { $t, i8 }
|
|
|
|
shl_impl! { $t, i16 }
|
|
|
|
shl_impl! { $t, i32 }
|
|
|
|
shl_impl! { $t, i64 }
|
|
|
|
shl_impl! { $t, isize }
|
2014-12-01 12:00:23 -06:00
|
|
|
)*)
|
2014-11-14 11:18:10 -06:00
|
|
|
}
|
2014-12-01 12:00:23 -06:00
|
|
|
|
2015-01-10 16:38:39 -06:00
|
|
|
shl_impl_all! { u8 u16 u32 u64 usize i8 i16 i32 i64 isize }
|
2014-05-26 12:33:04 -05:00
|
|
|
|
2014-11-24 19:06:06 -06:00
|
|
|
/// The `Shr` trait is used to specify the functionality of `>>`.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// A trivial implementation of `Shr`. When `Foo >> Foo` happens, it ends up
|
|
|
|
/// calling `shr`, and therefore, `main` prints `Shifting right!`.
|
|
|
|
///
|
|
|
|
/// ```
|
2014-12-22 11:04:23 -06:00
|
|
|
/// use std::ops::Shr;
|
|
|
|
///
|
2015-01-03 21:54:18 -06:00
|
|
|
/// #[derive(Copy)]
|
2014-11-24 19:06:06 -06:00
|
|
|
/// struct Foo;
|
|
|
|
///
|
2014-12-31 14:45:13 -06:00
|
|
|
/// impl Shr<Foo> for Foo {
|
|
|
|
/// type Output = Foo;
|
|
|
|
///
|
2014-12-01 18:10:12 -06:00
|
|
|
/// fn shr(self, _rhs: Foo) -> Foo {
|
2014-12-01 12:00:23 -06:00
|
|
|
/// println!("Shifting right!");
|
2014-12-01 18:10:12 -06:00
|
|
|
/// self
|
2014-12-01 12:00:23 -06:00
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn main() {
|
|
|
|
/// Foo >> Foo;
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
#[lang="shr"]
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-31 14:45:13 -06:00
|
|
|
pub trait Shr<RHS> {
|
2015-02-23 13:05:55 -06:00
|
|
|
/// The resulting type after applying the `>>` operator
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-31 14:45:13 -06:00
|
|
|
type Output;
|
|
|
|
|
2014-12-01 12:00:23 -06:00
|
|
|
/// The method for the `>>` operator
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-31 14:45:13 -06:00
|
|
|
fn shr(self, rhs: RHS) -> Self::Output;
|
2014-12-01 12:00:23 -06:00
|
|
|
}
|
|
|
|
|
2014-11-14 11:18:10 -06:00
|
|
|
macro_rules! shr_impl {
|
2015-01-10 16:38:39 -06:00
|
|
|
($t:ty, $f:ty) => (
|
|
|
|
impl Shr<$f> for $t {
|
2014-12-31 14:45:13 -06:00
|
|
|
type Output = $t;
|
|
|
|
|
2014-12-01 12:00:23 -06:00
|
|
|
#[inline]
|
2015-01-10 16:38:39 -06:00
|
|
|
fn shr(self, other: $f) -> $t {
|
|
|
|
self >> other
|
|
|
|
}
|
2014-12-01 12:00:23 -06:00
|
|
|
}
|
2015-01-15 22:44:19 -06:00
|
|
|
|
|
|
|
forward_ref_binop! { impl Shr, shr for $t, $f }
|
2015-01-10 16:38:39 -06:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! shr_impl_all {
|
|
|
|
($($t:ty)*) => ($(
|
|
|
|
shr_impl! { $t, u8 }
|
|
|
|
shr_impl! { $t, u16 }
|
|
|
|
shr_impl! { $t, u32 }
|
|
|
|
shr_impl! { $t, u64 }
|
|
|
|
shr_impl! { $t, usize }
|
|
|
|
|
|
|
|
shr_impl! { $t, i8 }
|
|
|
|
shr_impl! { $t, i16 }
|
|
|
|
shr_impl! { $t, i32 }
|
|
|
|
shr_impl! { $t, i64 }
|
|
|
|
shr_impl! { $t, isize }
|
|
|
|
)*)
|
|
|
|
}
|
|
|
|
|
|
|
|
shr_impl_all! { u8 u16 u32 u64 usize i8 i16 i32 i64 isize }
|
2014-05-26 12:33:04 -05:00
|
|
|
|
2014-11-24 19:06:06 -06:00
|
|
|
/// The `Index` trait is used to specify the functionality of indexing operations
|
|
|
|
/// like `arr[idx]` when used in an immutable context.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
2015-01-16 14:29:43 -06:00
|
|
|
/// A trivial implementation of `Index`. When `Foo[Bar]` happens, it ends up
|
2014-11-24 19:06:06 -06:00
|
|
|
/// calling `index`, and therefore, `main` prints `Indexing!`.
|
|
|
|
///
|
|
|
|
/// ```
|
2014-12-22 11:04:23 -06:00
|
|
|
/// use std::ops::Index;
|
|
|
|
///
|
2015-01-03 21:54:18 -06:00
|
|
|
/// #[derive(Copy)]
|
2014-11-24 19:06:06 -06:00
|
|
|
/// struct Foo;
|
2015-01-16 14:29:43 -06:00
|
|
|
/// struct Bar;
|
2014-11-24 19:06:06 -06:00
|
|
|
///
|
2015-01-16 14:29:43 -06:00
|
|
|
/// impl Index<Bar> for Foo {
|
2015-01-03 08:46:29 -06:00
|
|
|
/// type Output = Foo;
|
|
|
|
///
|
2015-01-16 14:29:43 -06:00
|
|
|
/// fn index<'a>(&'a self, _index: &Bar) -> &'a Foo {
|
2014-11-24 19:06:06 -06:00
|
|
|
/// println!("Indexing!");
|
|
|
|
/// self
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn main() {
|
2015-01-16 14:29:43 -06:00
|
|
|
/// Foo[Bar];
|
2014-11-24 19:06:06 -06:00
|
|
|
/// }
|
|
|
|
/// ```
|
2012-11-28 15:51:50 -06:00
|
|
|
#[lang="index"]
|
2015-02-04 17:00:12 -06:00
|
|
|
#[rustc_on_unimplemented = "the type `{Self}` cannot be indexed by `{Idx}`"]
|
2015-01-24 11:15:42 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-04 17:00:12 -06:00
|
|
|
pub trait Index<Idx: ?Sized> {
|
2015-02-23 13:05:55 -06:00
|
|
|
/// The returned type after indexing
|
2015-03-03 13:28:57 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-05 15:16:49 -06:00
|
|
|
type Output: ?Sized;
|
2015-01-03 08:46:29 -06:00
|
|
|
|
2014-03-16 17:35:35 -05:00
|
|
|
/// The method for the indexing (`Foo[Bar]`) operation
|
2015-01-24 11:15:42 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-04 17:00:12 -06:00
|
|
|
fn index<'a>(&'a self, index: &Idx) -> &'a Self::Output;
|
2015-01-03 08:46:29 -06:00
|
|
|
}
|
|
|
|
|
2014-11-24 19:06:06 -06:00
|
|
|
/// The `IndexMut` trait is used to specify the functionality of indexing
|
|
|
|
/// operations like `arr[idx]`, when used in a mutable context.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
2015-01-16 14:29:43 -06:00
|
|
|
/// A trivial implementation of `IndexMut`. When `Foo[Bar]` happens, it ends up
|
2014-11-24 19:06:06 -06:00
|
|
|
/// calling `index_mut`, and therefore, `main` prints `Indexing!`.
|
|
|
|
///
|
|
|
|
/// ```
|
2015-02-04 17:00:12 -06:00
|
|
|
/// use std::ops::{Index, IndexMut};
|
2014-12-22 11:04:23 -06:00
|
|
|
///
|
2015-01-03 21:54:18 -06:00
|
|
|
/// #[derive(Copy)]
|
2014-11-24 19:06:06 -06:00
|
|
|
/// struct Foo;
|
2015-01-16 14:29:43 -06:00
|
|
|
/// struct Bar;
|
2014-11-24 19:06:06 -06:00
|
|
|
///
|
2015-02-04 17:00:12 -06:00
|
|
|
/// impl Index<Bar> for Foo {
|
2015-01-03 08:46:29 -06:00
|
|
|
/// type Output = Foo;
|
|
|
|
///
|
2015-02-04 17:00:12 -06:00
|
|
|
/// fn index<'a>(&'a self, _index: &Bar) -> &'a Foo {
|
|
|
|
/// self
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// impl IndexMut<Bar> for Foo {
|
2015-01-16 14:29:43 -06:00
|
|
|
/// fn index_mut<'a>(&'a mut self, _index: &Bar) -> &'a mut Foo {
|
2014-11-24 19:06:06 -06:00
|
|
|
/// println!("Indexing!");
|
|
|
|
/// self
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn main() {
|
2015-01-16 14:29:43 -06:00
|
|
|
/// &mut Foo[Bar];
|
2014-11-24 19:06:06 -06:00
|
|
|
/// }
|
|
|
|
/// ```
|
2014-07-03 16:32:41 -05:00
|
|
|
#[lang="index_mut"]
|
2015-02-04 17:00:12 -06:00
|
|
|
#[rustc_on_unimplemented = "the type `{Self}` cannot be mutably indexed by `{Idx}`"]
|
2015-01-24 11:15:42 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-04 17:00:12 -06:00
|
|
|
pub trait IndexMut<Idx: ?Sized>: Index<Idx> {
|
2014-07-03 16:32:41 -05:00
|
|
|
/// The method for the indexing (`Foo[Bar]`) operation
|
2015-01-24 11:15:42 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-02-04 17:00:12 -06:00
|
|
|
fn index_mut<'a>(&'a mut self, index: &Idx) -> &'a mut Self::Output;
|
2012-11-28 15:51:50 -06:00
|
|
|
}
|
2013-07-22 18:50:17 -05:00
|
|
|
|
2014-12-12 21:58:48 -06:00
|
|
|
/// An unbounded range.
|
2015-01-27 22:06:46 -06:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq)]
|
|
|
|
#[lang="range_full"]
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
pub struct RangeFull;
|
|
|
|
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
impl fmt::Debug for RangeFull {
|
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
fmt::Debug::fmt("..", fmt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-15 21:25:33 -06:00
|
|
|
/// A (half-open) range which is bounded at both ends.
|
2015-01-31 08:09:22 -06:00
|
|
|
#[derive(Clone, PartialEq, Eq)]
|
2014-12-12 22:19:54 -06:00
|
|
|
#[lang="range"]
|
2015-01-24 11:15:42 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-12 21:58:48 -06:00
|
|
|
pub struct Range<Idx> {
|
|
|
|
/// The lower bound of the range (inclusive).
|
|
|
|
pub start: Idx,
|
|
|
|
/// The upper bound of the range (exclusive).
|
|
|
|
pub end: Idx,
|
|
|
|
}
|
|
|
|
|
2015-01-24 11:15:42 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-20 17:45:07 -06:00
|
|
|
impl<Idx: fmt::Debug> fmt::Debug for Range<Idx> {
|
2015-01-06 22:21:09 -06:00
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(fmt, "{:?}..{:?}", self.start, self.end)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-12 21:58:48 -06:00
|
|
|
/// A range which is only bounded below.
|
2015-01-31 08:09:22 -06:00
|
|
|
#[derive(Clone, PartialEq, Eq)]
|
2014-12-12 22:19:54 -06:00
|
|
|
#[lang="range_from"]
|
2015-01-24 11:15:42 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-12 21:58:48 -06:00
|
|
|
pub struct RangeFrom<Idx> {
|
|
|
|
/// The lower bound of the range (inclusive).
|
|
|
|
pub start: Idx,
|
|
|
|
}
|
|
|
|
|
2014-12-29 15:18:41 -06:00
|
|
|
|
2014-12-12 21:58:48 -06:00
|
|
|
|
2015-01-24 11:15:42 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-20 17:45:07 -06:00
|
|
|
impl<Idx: fmt::Debug> fmt::Debug for RangeFrom<Idx> {
|
2015-01-06 22:21:09 -06:00
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(fmt, "{:?}..", self.start)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-17 22:55:04 -06:00
|
|
|
/// A range which is only bounded above.
|
2015-01-18 23:43:15 -06:00
|
|
|
#[derive(Copy, Clone, PartialEq, Eq)]
|
2014-12-17 22:55:04 -06:00
|
|
|
#[lang="range_to"]
|
2015-01-24 11:15:42 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-17 22:55:04 -06:00
|
|
|
pub struct RangeTo<Idx> {
|
|
|
|
/// The upper bound of the range (exclusive).
|
|
|
|
pub end: Idx,
|
|
|
|
}
|
|
|
|
|
2015-01-24 11:15:42 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-20 17:45:07 -06:00
|
|
|
impl<Idx: fmt::Debug> fmt::Debug for RangeTo<Idx> {
|
2015-01-06 22:21:09 -06:00
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
write!(fmt, "..{:?}", self.end)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-12 21:58:48 -06:00
|
|
|
|
2014-11-24 19:06:06 -06:00
|
|
|
/// The `Deref` trait is used to specify the functionality of dereferencing
|
|
|
|
/// operations like `*v`.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// A struct with a single field which is accessible via dereferencing the
|
|
|
|
/// struct.
|
|
|
|
///
|
|
|
|
/// ```
|
2014-12-22 11:04:23 -06:00
|
|
|
/// use std::ops::Deref;
|
|
|
|
///
|
2014-11-24 19:06:06 -06:00
|
|
|
/// struct DerefExample<T> {
|
|
|
|
/// value: T
|
|
|
|
/// }
|
|
|
|
///
|
2015-01-01 13:53:20 -06:00
|
|
|
/// impl<T> Deref for DerefExample<T> {
|
|
|
|
/// type Target = T;
|
|
|
|
///
|
2014-11-24 19:06:06 -06:00
|
|
|
/// fn deref<'a>(&'a self) -> &'a T {
|
|
|
|
/// &self.value
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn main() {
|
|
|
|
/// let x = DerefExample { value: 'a' };
|
|
|
|
/// assert_eq!('a', *x);
|
|
|
|
/// }
|
|
|
|
/// ```
|
2014-02-26 15:02:35 -06:00
|
|
|
#[lang="deref"]
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-04 20:39:02 -06:00
|
|
|
pub trait Deref {
|
2015-02-23 13:05:55 -06:00
|
|
|
/// The resulting type after dereferencing
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-05 15:16:49 -06:00
|
|
|
type Target: ?Sized;
|
2015-01-01 13:53:20 -06:00
|
|
|
|
2014-03-16 17:35:35 -05:00
|
|
|
/// The method called to dereference a value
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-01 13:53:20 -06:00
|
|
|
fn deref<'a>(&'a self) -> &'a Self::Target;
|
2014-02-26 15:02:35 -06:00
|
|
|
}
|
|
|
|
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-05 15:16:49 -06:00
|
|
|
impl<'a, T: ?Sized> Deref for &'a T {
|
2015-01-01 13:53:20 -06:00
|
|
|
type Target = T;
|
|
|
|
|
2014-11-06 16:34:33 -06:00
|
|
|
fn deref(&self) -> &T { *self }
|
|
|
|
}
|
|
|
|
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-05 15:16:49 -06:00
|
|
|
impl<'a, T: ?Sized> Deref for &'a mut T {
|
2015-01-01 13:53:20 -06:00
|
|
|
type Target = T;
|
|
|
|
|
2014-11-06 16:34:33 -06:00
|
|
|
fn deref(&self) -> &T { *self }
|
|
|
|
}
|
|
|
|
|
2014-11-24 19:06:06 -06:00
|
|
|
/// The `DerefMut` trait is used to specify the functionality of dereferencing
|
|
|
|
/// mutably like `*v = 1;`
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// A struct with a single field which is modifiable via dereferencing the
|
|
|
|
/// struct.
|
|
|
|
///
|
|
|
|
/// ```
|
2014-12-22 11:04:23 -06:00
|
|
|
/// use std::ops::{Deref, DerefMut};
|
|
|
|
///
|
2014-11-24 19:06:06 -06:00
|
|
|
/// struct DerefMutExample<T> {
|
|
|
|
/// value: T
|
|
|
|
/// }
|
|
|
|
///
|
2015-01-01 13:53:20 -06:00
|
|
|
/// impl<T> Deref for DerefMutExample<T> {
|
|
|
|
/// type Target = T;
|
|
|
|
///
|
2014-11-24 19:06:06 -06:00
|
|
|
/// fn deref<'a>(&'a self) -> &'a T {
|
|
|
|
/// &self.value
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
///
|
2015-01-01 13:53:20 -06:00
|
|
|
/// impl<T> DerefMut for DerefMutExample<T> {
|
2014-11-24 19:06:06 -06:00
|
|
|
/// fn deref_mut<'a>(&'a mut self) -> &'a mut T {
|
|
|
|
/// &mut self.value
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn main() {
|
|
|
|
/// let mut x = DerefMutExample { value: 'a' };
|
|
|
|
/// *x = 'b';
|
|
|
|
/// assert_eq!('b', *x);
|
|
|
|
/// }
|
|
|
|
/// ```
|
2014-02-26 15:02:35 -06:00
|
|
|
#[lang="deref_mut"]
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-05 20:47:45 -06:00
|
|
|
pub trait DerefMut: Deref {
|
2014-03-16 17:35:35 -05:00
|
|
|
/// The method called to mutably dereference a value
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-05 19:47:21 -06:00
|
|
|
fn deref_mut<'a>(&'a mut self) -> &'a mut Self::Target;
|
2014-02-26 15:02:35 -06:00
|
|
|
}
|
|
|
|
|
2015-01-23 23:48:20 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-05 15:16:49 -06:00
|
|
|
impl<'a, T: ?Sized> DerefMut for &'a mut T {
|
2014-11-06 16:34:33 -06:00
|
|
|
fn deref_mut(&mut self) -> &mut T { *self }
|
|
|
|
}
|
|
|
|
|
2014-06-01 18:35:01 -05:00
|
|
|
/// A version of the call operator that takes an immutable receiver.
|
|
|
|
#[lang="fn"]
|
2015-02-09 18:33:19 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-26 13:39:58 -06:00
|
|
|
#[rustc_paren_sugar]
|
2015-01-10 10:54:15 -06:00
|
|
|
pub trait Fn<Args> {
|
2015-02-23 13:05:55 -06:00
|
|
|
/// The returned type after the call operator is used.
|
2015-01-10 10:54:15 -06:00
|
|
|
type Output;
|
|
|
|
|
|
|
|
/// This is called when the call operator is used.
|
|
|
|
extern "rust-call" fn call(&self, args: Args) -> Self::Output;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A version of the call operator that takes a mutable receiver.
|
|
|
|
#[lang="fn_mut"]
|
2015-02-09 18:33:19 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-26 13:39:58 -06:00
|
|
|
#[rustc_paren_sugar]
|
2015-01-10 10:54:15 -06:00
|
|
|
pub trait FnMut<Args> {
|
2015-02-23 13:05:55 -06:00
|
|
|
/// The returned type after the call operator is used.
|
2015-01-10 10:54:15 -06:00
|
|
|
type Output;
|
|
|
|
|
|
|
|
/// This is called when the call operator is used.
|
|
|
|
extern "rust-call" fn call_mut(&mut self, args: Args) -> Self::Output;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A version of the call operator that takes a by-value receiver.
|
|
|
|
#[lang="fn_once"]
|
2015-02-09 18:33:19 -06:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2015-01-26 13:39:58 -06:00
|
|
|
#[rustc_paren_sugar]
|
2015-01-10 10:54:15 -06:00
|
|
|
pub trait FnOnce<Args> {
|
2015-02-23 13:05:55 -06:00
|
|
|
/// The returned type after the call operator is used.
|
2015-01-10 10:54:15 -06:00
|
|
|
type Output;
|
|
|
|
|
|
|
|
/// This is called when the call operator is used.
|
|
|
|
extern "rust-call" fn call_once(self, args: Args) -> Self::Output;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<F: ?Sized, A> FnMut<A> for F
|
|
|
|
where F : Fn<A>
|
|
|
|
{
|
|
|
|
type Output = <F as Fn<A>>::Output;
|
|
|
|
|
|
|
|
extern "rust-call" fn call_mut(&mut self, args: A) -> <F as Fn<A>>::Output {
|
|
|
|
self.call(args)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<F,A> FnOnce<A> for F
|
|
|
|
where F : FnMut<A>
|
|
|
|
{
|
|
|
|
type Output = <F as FnMut<A>>::Output;
|
|
|
|
|
|
|
|
extern "rust-call" fn call_once(mut self, args: A) -> <F as FnMut<A>>::Output {
|
|
|
|
self.call_mut(args)
|
|
|
|
}
|
|
|
|
}
|