2012-12-10 15:44:02 -08: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 21:17:11 -05:00
|
|
|
//! Primitive traits representing basic 'kinds' of types
|
|
|
|
//!
|
|
|
|
//! Rust types can be classified in various useful ways according to
|
|
|
|
//! intrinsic properties of the type. These classifications, often called
|
|
|
|
//! 'kinds', are represented as traits.
|
|
|
|
//!
|
|
|
|
//! They cannot be implemented by user code, but are instead implemented
|
|
|
|
//! by the compiler automatically for the types to which they apply.
|
2012-11-30 00:47:45 -08:00
|
|
|
|
2013-08-13 20:46:50 -04:00
|
|
|
/// Types able to be transferred across task boundaries.
|
2013-06-05 11:33:14 -07:00
|
|
|
#[lang="send"]
|
2014-12-22 00:49:42 +01:00
|
|
|
pub unsafe trait Send for Sized? : 'static {
|
2013-06-05 11:33:14 -07:00
|
|
|
// empty.
|
2012-11-30 00:47:45 -08:00
|
|
|
}
|
|
|
|
|
2013-08-13 20:46:50 -04:00
|
|
|
/// Types with a constant size known at compile-time.
|
2013-05-30 20:03:01 -04:00
|
|
|
#[lang="sized"]
|
2014-09-18 11:08:04 -04:00
|
|
|
pub trait Sized for Sized? {
|
2013-05-30 20:03:01 -04:00
|
|
|
// Empty.
|
|
|
|
}
|
2013-12-11 12:40:51 -08:00
|
|
|
|
|
|
|
/// Types that can be copied by simply copying bits (i.e. `memcpy`).
|
2014-03-27 00:01:11 +01:00
|
|
|
#[lang="copy"]
|
2014-09-18 11:08:04 -04:00
|
|
|
pub trait Copy for Sized? {
|
2013-12-11 12:40:51 -08:00
|
|
|
// Empty.
|
|
|
|
}
|
|
|
|
|
2014-03-22 18:12:22 +11:00
|
|
|
/// Types that can be safely shared between tasks when aliased.
|
|
|
|
///
|
2014-08-05 16:40:04 -07:00
|
|
|
/// The precise definition is: a type `T` is `Sync` if `&T` is
|
2014-03-22 18:12:22 +11:00
|
|
|
/// thread-safe. In other words, there is no possibility of data races
|
|
|
|
/// when passing `&T` references between tasks.
|
|
|
|
///
|
|
|
|
/// As one would expect, primitive types like `u8` and `f64` are all
|
2014-08-05 16:40:04 -07:00
|
|
|
/// `Sync`, and so are simple aggregate types containing them (like
|
|
|
|
/// tuples, structs and enums). More instances of basic `Sync` types
|
2014-03-22 18:12:22 +11:00
|
|
|
/// include "immutable" types like `&T` and those with simple
|
2014-05-05 18:56:44 -07:00
|
|
|
/// inherited mutability, such as `Box<T>`, `Vec<T>` and most other
|
2014-08-05 16:40:04 -07:00
|
|
|
/// collection types. (Generic parameters need to be `Sync` for their
|
|
|
|
/// container to be `Sync`.)
|
2014-03-22 18:12:22 +11:00
|
|
|
///
|
|
|
|
/// A somewhat surprising consequence of the definition is `&mut T` is
|
2014-08-05 16:40:04 -07:00
|
|
|
/// `Sync` (if `T` is `Sync`) even though it seems that it might
|
2014-03-22 18:12:22 +11:00
|
|
|
/// provide unsynchronised mutation. The trick is a mutable reference
|
|
|
|
/// stored in an aliasable reference (that is, `& &mut T`) becomes
|
|
|
|
/// read-only, as if it were a `& &T`, hence there is no risk of a data
|
|
|
|
/// race.
|
|
|
|
///
|
2014-08-05 16:40:04 -07:00
|
|
|
/// Types that are not `Sync` are those that have "interior
|
2014-03-22 18:12:22 +11:00
|
|
|
/// mutability" in a non-thread-safe way, such as `Cell` and `RefCell`
|
|
|
|
/// in `std::cell`. These types allow for mutation of their contents
|
|
|
|
/// even when in an immutable, aliasable slot, e.g. the contents of
|
|
|
|
/// `&Cell<T>` can be `.set`, and do not ensure data races are
|
2014-08-05 16:40:04 -07:00
|
|
|
/// impossible, hence they cannot be `Sync`. A higher level example
|
|
|
|
/// of a non-`Sync` type is the reference counted pointer
|
2014-03-22 18:12:22 +11:00
|
|
|
/// `std::rc::Rc`, because any reference `&Rc<T>` can clone a new
|
|
|
|
/// reference, which modifies the reference counts in a non-atomic
|
|
|
|
/// way.
|
|
|
|
///
|
|
|
|
/// For cases when one does need thread-safe interior mutability,
|
|
|
|
/// types like the atomics in `std::sync` and `Mutex` & `RWLock` in
|
|
|
|
/// the `sync` crate do ensure that any mutation cannot cause data
|
2014-08-05 16:40:04 -07:00
|
|
|
/// races. Hence these types are `Sync`.
|
2014-03-22 18:12:22 +11:00
|
|
|
///
|
|
|
|
/// Users writing their own types with interior mutability (or anything
|
2014-08-05 16:40:04 -07:00
|
|
|
/// else that is not thread-safe) should use the `NoSync` marker type
|
2014-03-22 18:12:22 +11:00
|
|
|
/// (from `std::kinds::marker`) to ensure that the compiler doesn't
|
2014-08-05 16:40:04 -07:00
|
|
|
/// consider the user-defined type to be `Sync`. Any types with
|
std: Stabilize unit, bool, ty, tuple, arc, any
This commit applies stability attributes to the contents of these modules,
summarized here:
* The `unit` and `bool` modules have become #[unstable] as they are purely meant
for documentation purposes and are candidates for removal.
* The `ty` module has been deprecated, and the inner `Unsafe` type has been
renamed to `UnsafeCell` and moved to the `cell` module. The `marker1` field
has been removed as the compiler now always infers `UnsafeCell` to be
invariant. The `new` method i stable, but the `value` field, `get` and
`unwrap` methods are all unstable.
* The `tuple` module has its name as stable, the naming of the `TupleN` traits
as stable while the methods are all #[unstable]. The other impls in the module
have appropriate stability for the corresponding trait.
* The `arc` module has received the exact same treatment as the `rc` module
previously did.
* The `any` module has its name as stable. The `Any` trait is also stable, with
a new private supertrait which now contains the `get_type_id` method. This is
to make the method a private implementation detail rather than a public-facing
detail.
The two extension traits in the module are marked #[unstable] as they will not
be necessary with DST. The `is` method is #[stable], the as_{mut,ref} methods
have been renamed to downcast_{mut,ref} and are #[unstable].
The extension trait `BoxAny` has been clarified as to why it is unstable as it
will not be necessary with DST.
This is a breaking change because the `marker1` field was removed from the
`UnsafeCell` type. To deal with this change, you can simply delete the field and
only specify the value of the `data` field in static initializers.
[breaking-change]
2014-07-23 19:10:12 -07:00
|
|
|
/// interior mutability must also use the `std::cell::UnsafeCell` wrapper
|
2014-03-22 18:12:22 +11:00
|
|
|
/// around the value(s) which can be mutated when behind a `&`
|
|
|
|
/// reference; not doing this is undefined behaviour (for example,
|
|
|
|
/// `transmute`-ing from `&T` to `&mut T` is illegal).
|
2014-08-05 16:40:04 -07:00
|
|
|
#[lang="sync"]
|
2014-12-22 00:49:42 +01:00
|
|
|
pub unsafe trait Sync for Sized? {
|
2014-03-03 23:27:46 +01:00
|
|
|
// Empty
|
|
|
|
}
|
|
|
|
|
2014-01-22 14:03:02 -05:00
|
|
|
/// Marker types are special types that are used with unsafe code to
|
|
|
|
/// inform the compiler of special constraints. Marker types should
|
|
|
|
/// only be needed when you are creating an abstraction that is
|
|
|
|
/// implemented using unsafe code. In that case, you may want to embed
|
|
|
|
/// some of the marker types below into your type.
|
|
|
|
pub mod marker {
|
2014-12-18 18:31:29 -08:00
|
|
|
use super::{Copy,Sized};
|
|
|
|
use clone::Clone;
|
librustc: Make `Copy` opt-in.
This change makes the compiler no longer infer whether types (structures
and enumerations) implement the `Copy` trait (and thus are implicitly
copyable). Rather, you must implement `Copy` yourself via `impl Copy for
MyType {}`.
A new warning has been added, `missing_copy_implementations`, to warn
you if a non-generic public type has been added that could have
implemented `Copy` but didn't.
For convenience, you may *temporarily* opt out of this behavior by using
`#![feature(opt_out_copy)]`. Note though that this feature gate will never be
accepted and will be removed by the time that 1.0 is released, so you should
transition your code away from using it.
This breaks code like:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
Change this code to:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
impl Copy for Point2D {}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
This is the backwards-incompatible part of #13231.
Part of RFC #3.
[breaking-change]
2014-12-05 17:01:33 -08:00
|
|
|
|
2014-01-22 14:03:02 -05:00
|
|
|
/// A marker type whose type parameter `T` is considered to be
|
|
|
|
/// covariant with respect to the type itself. This is (typically)
|
|
|
|
/// used to indicate that an instance of the type `T` is being stored
|
|
|
|
/// into memory and read from, even though that may not be apparent.
|
|
|
|
///
|
|
|
|
/// For more information about variance, refer to this Wikipedia
|
|
|
|
/// article <http://en.wikipedia.org/wiki/Variance_%28computer_science%29>.
|
|
|
|
///
|
|
|
|
/// *Note:* It is very unusual to have to add a covariant constraint.
|
|
|
|
/// If you are not sure, you probably want to use `InvariantType`.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// Given a struct `S` that includes a type parameter `T`
|
|
|
|
/// but does not actually *reference* that type parameter:
|
|
|
|
///
|
2014-02-14 23:44:22 -08:00
|
|
|
/// ```ignore
|
core: Remove the cast module
This commit revisits the `cast` module in libcore and libstd, and scrutinizes
all functions inside of it. The result was to remove the `cast` module entirely,
folding all functionality into the `mem` module. Specifically, this is the fate
of each function in the `cast` module.
* transmute - This function was moved to `mem`, but it is now marked as
#[unstable]. This is due to planned changes to the `transmute`
function and how it can be invoked (see the #[unstable] comment).
For more information, see RFC 5 and #12898
* transmute_copy - This function was moved to `mem`, with clarification that is
is not an error to invoke it with T/U that are different
sizes, but rather that it is strongly discouraged. This
function is now #[stable]
* forget - This function was moved to `mem` and marked #[stable]
* bump_box_refcount - This function was removed due to the deprecation of
managed boxes as well as its questionable utility.
* transmute_mut - This function was previously deprecated, and removed as part
of this commit.
* transmute_mut_unsafe - This function doesn't serve much of a purpose when it
can be achieved with an `as` in safe code, so it was
removed.
* transmute_lifetime - This function was removed because it is likely a strong
indication that code is incorrect in the first place.
* transmute_mut_lifetime - This function was removed for the same reasons as
`transmute_lifetime`
* copy_lifetime - This function was moved to `mem`, but it is marked
`#[unstable]` now due to the likelihood of being removed in
the future if it is found to not be very useful.
* copy_mut_lifetime - This function was also moved to `mem`, but had the same
treatment as `copy_lifetime`.
* copy_lifetime_vec - This function was removed because it is not used today,
and its existence is not necessary with DST
(copy_lifetime will suffice).
In summary, the cast module was stripped down to these functions, and then the
functions were moved to the `mem` module.
transmute - #[unstable]
transmute_copy - #[stable]
forget - #[stable]
copy_lifetime - #[unstable]
copy_mut_lifetime - #[unstable]
[breaking-change]
2014-05-09 10:34:51 -07:00
|
|
|
/// use std::mem;
|
2014-02-14 23:44:22 -08:00
|
|
|
///
|
2014-01-22 14:03:02 -05:00
|
|
|
/// struct S<T> { x: *() }
|
|
|
|
/// fn get<T>(s: &S<T>) -> T {
|
|
|
|
/// unsafe {
|
core: Remove the cast module
This commit revisits the `cast` module in libcore and libstd, and scrutinizes
all functions inside of it. The result was to remove the `cast` module entirely,
folding all functionality into the `mem` module. Specifically, this is the fate
of each function in the `cast` module.
* transmute - This function was moved to `mem`, but it is now marked as
#[unstable]. This is due to planned changes to the `transmute`
function and how it can be invoked (see the #[unstable] comment).
For more information, see RFC 5 and #12898
* transmute_copy - This function was moved to `mem`, with clarification that is
is not an error to invoke it with T/U that are different
sizes, but rather that it is strongly discouraged. This
function is now #[stable]
* forget - This function was moved to `mem` and marked #[stable]
* bump_box_refcount - This function was removed due to the deprecation of
managed boxes as well as its questionable utility.
* transmute_mut - This function was previously deprecated, and removed as part
of this commit.
* transmute_mut_unsafe - This function doesn't serve much of a purpose when it
can be achieved with an `as` in safe code, so it was
removed.
* transmute_lifetime - This function was removed because it is likely a strong
indication that code is incorrect in the first place.
* transmute_mut_lifetime - This function was removed for the same reasons as
`transmute_lifetime`
* copy_lifetime - This function was moved to `mem`, but it is marked
`#[unstable]` now due to the likelihood of being removed in
the future if it is found to not be very useful.
* copy_mut_lifetime - This function was also moved to `mem`, but had the same
treatment as `copy_lifetime`.
* copy_lifetime_vec - This function was removed because it is not used today,
and its existence is not necessary with DST
(copy_lifetime will suffice).
In summary, the cast module was stripped down to these functions, and then the
functions were moved to the `mem` module.
transmute - #[unstable]
transmute_copy - #[stable]
forget - #[stable]
copy_lifetime - #[unstable]
copy_mut_lifetime - #[unstable]
[breaking-change]
2014-05-09 10:34:51 -07:00
|
|
|
/// let x: *T = mem::transmute(s.x);
|
2014-01-22 14:03:02 -05:00
|
|
|
/// *x
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// The type system would currently infer that the value of
|
|
|
|
/// the type parameter `T` is irrelevant, and hence a `S<int>` is
|
2014-08-05 15:23:06 -04:00
|
|
|
/// a subtype of `S<Box<int>>` (or, for that matter, `S<U>` for
|
2014-03-06 16:35:12 +09:00
|
|
|
/// any `U`). But this is incorrect because `get()` converts the
|
2014-01-22 14:03:02 -05:00
|
|
|
/// `*()` into a `*T` and reads from it. Therefore, we should include the
|
|
|
|
/// a marker field `CovariantType<T>` to inform the type checker that
|
2014-03-06 16:35:12 +09:00
|
|
|
/// `S<T>` is a subtype of `S<U>` if `T` is a subtype of `U`
|
2014-01-22 14:03:02 -05:00
|
|
|
/// (for example, `S<&'static int>` is a subtype of `S<&'a int>`
|
|
|
|
/// for some lifetime `'a`, but not the other way around).
|
|
|
|
#[lang="covariant_type"]
|
2015-01-03 22:54:18 -05:00
|
|
|
#[derive(PartialEq, Eq, PartialOrd, Ord)]
|
2014-12-18 18:31:29 -08:00
|
|
|
pub struct CovariantType<Sized? T>;
|
2014-01-22 14:03:02 -05:00
|
|
|
|
2014-12-18 18:31:29 -08:00
|
|
|
impl<Sized? T> Copy for CovariantType<T> {}
|
|
|
|
impl<Sized? T> Clone for CovariantType<T> {
|
|
|
|
fn clone(&self) -> CovariantType<T> { *self }
|
|
|
|
}
|
librustc: Make `Copy` opt-in.
This change makes the compiler no longer infer whether types (structures
and enumerations) implement the `Copy` trait (and thus are implicitly
copyable). Rather, you must implement `Copy` yourself via `impl Copy for
MyType {}`.
A new warning has been added, `missing_copy_implementations`, to warn
you if a non-generic public type has been added that could have
implemented `Copy` but didn't.
For convenience, you may *temporarily* opt out of this behavior by using
`#![feature(opt_out_copy)]`. Note though that this feature gate will never be
accepted and will be removed by the time that 1.0 is released, so you should
transition your code away from using it.
This breaks code like:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
Change this code to:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
impl Copy for Point2D {}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
This is the backwards-incompatible part of #13231.
Part of RFC #3.
[breaking-change]
2014-12-05 17:01:33 -08:00
|
|
|
|
2014-01-22 14:03:02 -05:00
|
|
|
/// A marker type whose type parameter `T` is considered to be
|
|
|
|
/// contravariant with respect to the type itself. This is (typically)
|
|
|
|
/// used to indicate that an instance of the type `T` will be consumed
|
|
|
|
/// (but not read from), even though that may not be apparent.
|
|
|
|
///
|
|
|
|
/// For more information about variance, refer to this Wikipedia
|
|
|
|
/// article <http://en.wikipedia.org/wiki/Variance_%28computer_science%29>.
|
|
|
|
///
|
|
|
|
/// *Note:* It is very unusual to have to add a contravariant constraint.
|
|
|
|
/// If you are not sure, you probably want to use `InvariantType`.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// Given a struct `S` that includes a type parameter `T`
|
|
|
|
/// but does not actually *reference* that type parameter:
|
|
|
|
///
|
|
|
|
/// ```
|
core: Remove the cast module
This commit revisits the `cast` module in libcore and libstd, and scrutinizes
all functions inside of it. The result was to remove the `cast` module entirely,
folding all functionality into the `mem` module. Specifically, this is the fate
of each function in the `cast` module.
* transmute - This function was moved to `mem`, but it is now marked as
#[unstable]. This is due to planned changes to the `transmute`
function and how it can be invoked (see the #[unstable] comment).
For more information, see RFC 5 and #12898
* transmute_copy - This function was moved to `mem`, with clarification that is
is not an error to invoke it with T/U that are different
sizes, but rather that it is strongly discouraged. This
function is now #[stable]
* forget - This function was moved to `mem` and marked #[stable]
* bump_box_refcount - This function was removed due to the deprecation of
managed boxes as well as its questionable utility.
* transmute_mut - This function was previously deprecated, and removed as part
of this commit.
* transmute_mut_unsafe - This function doesn't serve much of a purpose when it
can be achieved with an `as` in safe code, so it was
removed.
* transmute_lifetime - This function was removed because it is likely a strong
indication that code is incorrect in the first place.
* transmute_mut_lifetime - This function was removed for the same reasons as
`transmute_lifetime`
* copy_lifetime - This function was moved to `mem`, but it is marked
`#[unstable]` now due to the likelihood of being removed in
the future if it is found to not be very useful.
* copy_mut_lifetime - This function was also moved to `mem`, but had the same
treatment as `copy_lifetime`.
* copy_lifetime_vec - This function was removed because it is not used today,
and its existence is not necessary with DST
(copy_lifetime will suffice).
In summary, the cast module was stripped down to these functions, and then the
functions were moved to the `mem` module.
transmute - #[unstable]
transmute_copy - #[stable]
forget - #[stable]
copy_lifetime - #[unstable]
copy_mut_lifetime - #[unstable]
[breaking-change]
2014-05-09 10:34:51 -07:00
|
|
|
/// use std::mem;
|
2014-02-14 23:44:22 -08:00
|
|
|
///
|
2014-06-25 12:47:34 -07:00
|
|
|
/// struct S<T> { x: *const () }
|
2014-01-22 14:03:02 -05:00
|
|
|
/// fn get<T>(s: &S<T>, v: T) {
|
|
|
|
/// unsafe {
|
core: Remove the cast module
This commit revisits the `cast` module in libcore and libstd, and scrutinizes
all functions inside of it. The result was to remove the `cast` module entirely,
folding all functionality into the `mem` module. Specifically, this is the fate
of each function in the `cast` module.
* transmute - This function was moved to `mem`, but it is now marked as
#[unstable]. This is due to planned changes to the `transmute`
function and how it can be invoked (see the #[unstable] comment).
For more information, see RFC 5 and #12898
* transmute_copy - This function was moved to `mem`, with clarification that is
is not an error to invoke it with T/U that are different
sizes, but rather that it is strongly discouraged. This
function is now #[stable]
* forget - This function was moved to `mem` and marked #[stable]
* bump_box_refcount - This function was removed due to the deprecation of
managed boxes as well as its questionable utility.
* transmute_mut - This function was previously deprecated, and removed as part
of this commit.
* transmute_mut_unsafe - This function doesn't serve much of a purpose when it
can be achieved with an `as` in safe code, so it was
removed.
* transmute_lifetime - This function was removed because it is likely a strong
indication that code is incorrect in the first place.
* transmute_mut_lifetime - This function was removed for the same reasons as
`transmute_lifetime`
* copy_lifetime - This function was moved to `mem`, but it is marked
`#[unstable]` now due to the likelihood of being removed in
the future if it is found to not be very useful.
* copy_mut_lifetime - This function was also moved to `mem`, but had the same
treatment as `copy_lifetime`.
* copy_lifetime_vec - This function was removed because it is not used today,
and its existence is not necessary with DST
(copy_lifetime will suffice).
In summary, the cast module was stripped down to these functions, and then the
functions were moved to the `mem` module.
transmute - #[unstable]
transmute_copy - #[stable]
forget - #[stable]
copy_lifetime - #[unstable]
copy_mut_lifetime - #[unstable]
[breaking-change]
2014-05-09 10:34:51 -07:00
|
|
|
/// let x: fn(T) = mem::transmute(s.x);
|
2014-01-22 14:03:02 -05:00
|
|
|
/// x(v)
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// The type system would currently infer that the value of
|
|
|
|
/// the type parameter `T` is irrelevant, and hence a `S<int>` is
|
2014-08-05 15:23:06 -04:00
|
|
|
/// a subtype of `S<Box<int>>` (or, for that matter, `S<U>` for
|
2014-03-06 16:35:12 +09:00
|
|
|
/// any `U`). But this is incorrect because `get()` converts the
|
2014-01-22 14:03:02 -05:00
|
|
|
/// `*()` into a `fn(T)` and then passes a value of type `T` to it.
|
|
|
|
///
|
|
|
|
/// Supplying a `ContravariantType` marker would correct the
|
|
|
|
/// problem, because it would mark `S` so that `S<T>` is only a
|
|
|
|
/// subtype of `S<U>` if `U` is a subtype of `T`; given that the
|
|
|
|
/// function requires arguments of type `T`, it must also accept
|
|
|
|
/// arguments of type `U`, hence such a conversion is safe.
|
|
|
|
#[lang="contravariant_type"]
|
2015-01-03 22:54:18 -05:00
|
|
|
#[derive(PartialEq, Eq, PartialOrd, Ord)]
|
2014-12-18 18:31:29 -08:00
|
|
|
pub struct ContravariantType<Sized? T>;
|
2014-01-22 14:03:02 -05:00
|
|
|
|
2014-12-18 18:31:29 -08:00
|
|
|
impl<Sized? T> Copy for ContravariantType<T> {}
|
|
|
|
impl<Sized? T> Clone for ContravariantType<T> {
|
|
|
|
fn clone(&self) -> ContravariantType<T> { *self }
|
|
|
|
}
|
librustc: Make `Copy` opt-in.
This change makes the compiler no longer infer whether types (structures
and enumerations) implement the `Copy` trait (and thus are implicitly
copyable). Rather, you must implement `Copy` yourself via `impl Copy for
MyType {}`.
A new warning has been added, `missing_copy_implementations`, to warn
you if a non-generic public type has been added that could have
implemented `Copy` but didn't.
For convenience, you may *temporarily* opt out of this behavior by using
`#![feature(opt_out_copy)]`. Note though that this feature gate will never be
accepted and will be removed by the time that 1.0 is released, so you should
transition your code away from using it.
This breaks code like:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
Change this code to:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
impl Copy for Point2D {}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
This is the backwards-incompatible part of #13231.
Part of RFC #3.
[breaking-change]
2014-12-05 17:01:33 -08:00
|
|
|
|
2014-01-22 14:03:02 -05:00
|
|
|
/// A marker type whose type parameter `T` is considered to be
|
|
|
|
/// invariant with respect to the type itself. This is (typically)
|
|
|
|
/// used to indicate that instances of the type `T` may be read or
|
|
|
|
/// written, even though that may not be apparent.
|
|
|
|
///
|
|
|
|
/// For more information about variance, refer to this Wikipedia
|
|
|
|
/// article <http://en.wikipedia.org/wiki/Variance_%28computer_science%29>.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// The Cell type is an example which uses unsafe code to achieve
|
|
|
|
/// "interior" mutability:
|
|
|
|
///
|
|
|
|
/// ```
|
2014-03-27 15:09:47 -07:00
|
|
|
/// pub struct Cell<T> { value: T }
|
2014-02-14 23:44:22 -08:00
|
|
|
/// # fn main() {}
|
2014-01-22 14:03:02 -05:00
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// The type system would infer that `value` is only read here and
|
|
|
|
/// never written, but in fact `Cell` uses unsafe code to achieve
|
|
|
|
/// interior mutability.
|
|
|
|
#[lang="invariant_type"]
|
2015-01-03 22:54:18 -05:00
|
|
|
#[derive(PartialEq, Eq, PartialOrd, Ord)]
|
2014-12-18 18:31:29 -08:00
|
|
|
pub struct InvariantType<Sized? T>;
|
2014-01-22 14:03:02 -05:00
|
|
|
|
2014-12-18 18:31:29 -08:00
|
|
|
impl<Sized? T> Copy for InvariantType<T> {}
|
|
|
|
impl<Sized? T> Clone for InvariantType<T> {
|
|
|
|
fn clone(&self) -> InvariantType<T> { *self }
|
|
|
|
}
|
librustc: Make `Copy` opt-in.
This change makes the compiler no longer infer whether types (structures
and enumerations) implement the `Copy` trait (and thus are implicitly
copyable). Rather, you must implement `Copy` yourself via `impl Copy for
MyType {}`.
A new warning has been added, `missing_copy_implementations`, to warn
you if a non-generic public type has been added that could have
implemented `Copy` but didn't.
For convenience, you may *temporarily* opt out of this behavior by using
`#![feature(opt_out_copy)]`. Note though that this feature gate will never be
accepted and will be removed by the time that 1.0 is released, so you should
transition your code away from using it.
This breaks code like:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
Change this code to:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
impl Copy for Point2D {}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
This is the backwards-incompatible part of #13231.
Part of RFC #3.
[breaking-change]
2014-12-05 17:01:33 -08:00
|
|
|
|
2014-01-22 14:03:02 -05:00
|
|
|
/// As `CovariantType`, but for lifetime parameters. Using
|
|
|
|
/// `CovariantLifetime<'a>` indicates that it is ok to substitute
|
|
|
|
/// a *longer* lifetime for `'a` than the one you originally
|
|
|
|
/// started with (e.g., you could convert any lifetime `'foo` to
|
|
|
|
/// `'static`). You almost certainly want `ContravariantLifetime`
|
|
|
|
/// instead, or possibly `InvariantLifetime`. The only case where
|
|
|
|
/// it would be appropriate is that you have a (type-casted, and
|
|
|
|
/// hence hidden from the type system) function pointer with a
|
|
|
|
/// signature like `fn(&'a T)` (and no other uses of `'a`). In
|
|
|
|
/// this case, it is ok to substitute a larger lifetime for `'a`
|
|
|
|
/// (e.g., `fn(&'static T)`), because the function is only
|
|
|
|
/// becoming more selective in terms of what it accepts as
|
|
|
|
/// argument.
|
|
|
|
///
|
|
|
|
/// For more information about variance, refer to this Wikipedia
|
|
|
|
/// article <http://en.wikipedia.org/wiki/Variance_%28computer_science%29>.
|
|
|
|
#[lang="covariant_lifetime"]
|
2015-01-03 22:54:18 -05:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
|
2014-01-22 14:03:02 -05:00
|
|
|
pub struct CovariantLifetime<'a>;
|
|
|
|
|
|
|
|
/// As `ContravariantType`, but for lifetime parameters. Using
|
|
|
|
/// `ContravariantLifetime<'a>` indicates that it is ok to
|
|
|
|
/// substitute a *shorter* lifetime for `'a` than the one you
|
|
|
|
/// originally started with (e.g., you could convert `'static` to
|
|
|
|
/// any lifetime `'foo`). This is appropriate for cases where you
|
|
|
|
/// have an unsafe pointer that is actually a pointer into some
|
|
|
|
/// memory with lifetime `'a`, and thus you want to limit the
|
|
|
|
/// lifetime of your data structure to `'a`. An example of where
|
|
|
|
/// this is used is the iterator for vectors.
|
|
|
|
///
|
|
|
|
/// For more information about variance, refer to this Wikipedia
|
|
|
|
/// article <http://en.wikipedia.org/wiki/Variance_%28computer_science%29>.
|
|
|
|
#[lang="contravariant_lifetime"]
|
2015-01-03 22:54:18 -05:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
|
2014-01-22 14:03:02 -05:00
|
|
|
pub struct ContravariantLifetime<'a>;
|
|
|
|
|
|
|
|
/// As `InvariantType`, but for lifetime parameters. Using
|
|
|
|
/// `InvariantLifetime<'a>` indicates that it is not ok to
|
|
|
|
/// substitute any other lifetime for `'a` besides its original
|
|
|
|
/// value. This is appropriate for cases where you have an unsafe
|
|
|
|
/// pointer that is actually a pointer into memory with lifetime `'a`,
|
|
|
|
/// and this pointer is itself stored in an inherently mutable
|
|
|
|
/// location (such as a `Cell`).
|
|
|
|
#[lang="invariant_lifetime"]
|
2015-01-03 22:54:18 -05:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
|
2014-01-22 14:03:02 -05:00
|
|
|
pub struct InvariantLifetime<'a>;
|
|
|
|
|
|
|
|
/// A type which is considered "not sendable", meaning that it cannot
|
|
|
|
/// be safely sent between tasks, even if it is owned. This is
|
|
|
|
/// typically embedded in other types, such as `Gc`, to ensure that
|
|
|
|
/// their instances remain thread-local.
|
|
|
|
#[lang="no_send_bound"]
|
2015-01-03 22:54:18 -05:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
|
2014-01-22 14:03:02 -05:00
|
|
|
pub struct NoSend;
|
|
|
|
|
|
|
|
/// A type which is considered "not POD", meaning that it is not
|
|
|
|
/// implicitly copyable. This is typically embedded in other types to
|
|
|
|
/// ensure that they are never copied, even if they lack a destructor.
|
2014-03-27 00:01:11 +01:00
|
|
|
#[lang="no_copy_bound"]
|
2015-01-03 22:54:18 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
|
librustc: Make `Copy` opt-in.
This change makes the compiler no longer infer whether types (structures
and enumerations) implement the `Copy` trait (and thus are implicitly
copyable). Rather, you must implement `Copy` yourself via `impl Copy for
MyType {}`.
A new warning has been added, `missing_copy_implementations`, to warn
you if a non-generic public type has been added that could have
implemented `Copy` but didn't.
For convenience, you may *temporarily* opt out of this behavior by using
`#![feature(opt_out_copy)]`. Note though that this feature gate will never be
accepted and will be removed by the time that 1.0 is released, so you should
transition your code away from using it.
This breaks code like:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
Change this code to:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
impl Copy for Point2D {}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
This is the backwards-incompatible part of #13231.
Part of RFC #3.
[breaking-change]
2014-12-05 17:01:33 -08:00
|
|
|
#[allow(missing_copy_implementations)]
|
2014-03-27 00:01:11 +01:00
|
|
|
pub struct NoCopy;
|
|
|
|
|
2014-08-05 16:40:04 -07:00
|
|
|
/// A type which is considered "not sync", meaning that
|
2014-03-03 23:27:46 +01:00
|
|
|
/// its contents are not threadsafe, hence they cannot be
|
|
|
|
/// shared between tasks.
|
2014-09-05 02:01:56 +02:00
|
|
|
#[lang="no_sync_bound"]
|
2015-01-03 22:54:18 -05:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
|
2014-08-05 16:40:04 -07:00
|
|
|
pub struct NoSync;
|
2014-03-03 23:27:46 +01:00
|
|
|
|
2014-01-22 14:03:02 -05:00
|
|
|
/// A type which is considered managed by the GC. This is typically
|
|
|
|
/// embedded in other types.
|
|
|
|
#[lang="managed_bound"]
|
2015-01-03 22:54:18 -05:00
|
|
|
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
|
librustc: Make `Copy` opt-in.
This change makes the compiler no longer infer whether types (structures
and enumerations) implement the `Copy` trait (and thus are implicitly
copyable). Rather, you must implement `Copy` yourself via `impl Copy for
MyType {}`.
A new warning has been added, `missing_copy_implementations`, to warn
you if a non-generic public type has been added that could have
implemented `Copy` but didn't.
For convenience, you may *temporarily* opt out of this behavior by using
`#![feature(opt_out_copy)]`. Note though that this feature gate will never be
accepted and will be removed by the time that 1.0 is released, so you should
transition your code away from using it.
This breaks code like:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
Change this code to:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
impl Copy for Point2D {}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
This is the backwards-incompatible part of #13231.
Part of RFC #3.
[breaking-change]
2014-12-05 17:01:33 -08:00
|
|
|
#[allow(missing_copy_implementations)]
|
2014-01-22 14:03:02 -05:00
|
|
|
pub struct Managed;
|
|
|
|
}
|
librustc: Make `Copy` opt-in.
This change makes the compiler no longer infer whether types (structures
and enumerations) implement the `Copy` trait (and thus are implicitly
copyable). Rather, you must implement `Copy` yourself via `impl Copy for
MyType {}`.
A new warning has been added, `missing_copy_implementations`, to warn
you if a non-generic public type has been added that could have
implemented `Copy` but didn't.
For convenience, you may *temporarily* opt out of this behavior by using
`#![feature(opt_out_copy)]`. Note though that this feature gate will never be
accepted and will be removed by the time that 1.0 is released, so you should
transition your code away from using it.
This breaks code like:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
Change this code to:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
impl Copy for Point2D {}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
This is the backwards-incompatible part of #13231.
Part of RFC #3.
[breaking-change]
2014-12-05 17:01:33 -08:00
|
|
|
|