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 //! Primitive traits representing basic 'kinds' of types
13 //! Rust types can be classified in various useful ways according to
14 //! intrinsic properties of the type. These classifications, often called
15 //! 'kinds', are represented as traits.
17 //! They cannot be implemented by user code, but are instead implemented
18 //! by the compiler automatically for the types to which they apply.
20 /// Types able to be transferred across task boundaries.
22 pub trait Send for Sized? : 'static {
26 /// Types with a constant size known at compile-time.
28 pub trait Sized for Sized? {
32 /// Types that can be copied by simply copying bits (i.e. `memcpy`).
34 pub trait Copy for Sized? {
38 /// Types that can be safely shared between tasks when aliased.
40 /// The precise definition is: a type `T` is `Sync` if `&T` is
41 /// thread-safe. In other words, there is no possibility of data races
42 /// when passing `&T` references between tasks.
44 /// As one would expect, primitive types like `u8` and `f64` are all
45 /// `Sync`, and so are simple aggregate types containing them (like
46 /// tuples, structs and enums). More instances of basic `Sync` types
47 /// include "immutable" types like `&T` and those with simple
48 /// inherited mutability, such as `Box<T>`, `Vec<T>` and most other
49 /// collection types. (Generic parameters need to be `Sync` for their
50 /// container to be `Sync`.)
52 /// A somewhat surprising consequence of the definition is `&mut T` is
53 /// `Sync` (if `T` is `Sync`) even though it seems that it might
54 /// provide unsynchronised mutation. The trick is a mutable reference
55 /// stored in an aliasable reference (that is, `& &mut T`) becomes
56 /// read-only, as if it were a `& &T`, hence there is no risk of a data
59 /// Types that are not `Sync` are those that have "interior
60 /// mutability" in a non-thread-safe way, such as `Cell` and `RefCell`
61 /// in `std::cell`. These types allow for mutation of their contents
62 /// even when in an immutable, aliasable slot, e.g. the contents of
63 /// `&Cell<T>` can be `.set`, and do not ensure data races are
64 /// impossible, hence they cannot be `Sync`. A higher level example
65 /// of a non-`Sync` type is the reference counted pointer
66 /// `std::rc::Rc`, because any reference `&Rc<T>` can clone a new
67 /// reference, which modifies the reference counts in a non-atomic
70 /// For cases when one does need thread-safe interior mutability,
71 /// types like the atomics in `std::sync` and `Mutex` & `RWLock` in
72 /// the `sync` crate do ensure that any mutation cannot cause data
73 /// races. Hence these types are `Sync`.
75 /// Users writing their own types with interior mutability (or anything
76 /// else that is not thread-safe) should use the `NoSync` marker type
77 /// (from `std::kinds::marker`) to ensure that the compiler doesn't
78 /// consider the user-defined type to be `Sync`. Any types with
79 /// interior mutability must also use the `std::cell::UnsafeCell` wrapper
80 /// around the value(s) which can be mutated when behind a `&`
81 /// reference; not doing this is undefined behaviour (for example,
82 /// `transmute`-ing from `&T` to `&mut T` is illegal).
84 pub trait Sync for Sized? {
88 /// Marker types are special types that are used with unsafe code to
89 /// inform the compiler of special constraints. Marker types should
90 /// only be needed when you are creating an abstraction that is
91 /// implemented using unsafe code. In that case, you may want to embed
92 /// some of the marker types below into your type.
96 /// A marker type whose type parameter `T` is considered to be
97 /// covariant with respect to the type itself. This is (typically)
98 /// used to indicate that an instance of the type `T` is being stored
99 /// into memory and read from, even though that may not be apparent.
101 /// For more information about variance, refer to this Wikipedia
102 /// article <http://en.wikipedia.org/wiki/Variance_%28computer_science%29>.
104 /// *Note:* It is very unusual to have to add a covariant constraint.
105 /// If you are not sure, you probably want to use `InvariantType`.
109 /// Given a struct `S` that includes a type parameter `T`
110 /// but does not actually *reference* that type parameter:
115 /// struct S<T> { x: *() }
116 /// fn get<T>(s: &S<T>) -> T {
118 /// let x: *T = mem::transmute(s.x);
124 /// The type system would currently infer that the value of
125 /// the type parameter `T` is irrelevant, and hence a `S<int>` is
126 /// a subtype of `S<Box<int>>` (or, for that matter, `S<U>` for
127 /// any `U`). But this is incorrect because `get()` converts the
128 /// `*()` into a `*T` and reads from it. Therefore, we should include the
129 /// a marker field `CovariantType<T>` to inform the type checker that
130 /// `S<T>` is a subtype of `S<U>` if `T` is a subtype of `U`
131 /// (for example, `S<&'static int>` is a subtype of `S<&'a int>`
132 /// for some lifetime `'a`, but not the other way around).
133 #[lang="covariant_type"]
134 #[deriving(Clone, PartialEq, Eq, PartialOrd, Ord)]
135 pub struct CovariantType<T>;
137 impl<T> Copy for CovariantType<T> {}
139 /// A marker type whose type parameter `T` is considered to be
140 /// contravariant with respect to the type itself. This is (typically)
141 /// used to indicate that an instance of the type `T` will be consumed
142 /// (but not read from), even though that may not be apparent.
144 /// For more information about variance, refer to this Wikipedia
145 /// article <http://en.wikipedia.org/wiki/Variance_%28computer_science%29>.
147 /// *Note:* It is very unusual to have to add a contravariant constraint.
148 /// If you are not sure, you probably want to use `InvariantType`.
152 /// Given a struct `S` that includes a type parameter `T`
153 /// but does not actually *reference* that type parameter:
158 /// struct S<T> { x: *const () }
159 /// fn get<T>(s: &S<T>, v: T) {
161 /// let x: fn(T) = mem::transmute(s.x);
167 /// The type system would currently infer that the value of
168 /// the type parameter `T` is irrelevant, and hence a `S<int>` is
169 /// a subtype of `S<Box<int>>` (or, for that matter, `S<U>` for
170 /// any `U`). But this is incorrect because `get()` converts the
171 /// `*()` into a `fn(T)` and then passes a value of type `T` to it.
173 /// Supplying a `ContravariantType` marker would correct the
174 /// problem, because it would mark `S` so that `S<T>` is only a
175 /// subtype of `S<U>` if `U` is a subtype of `T`; given that the
176 /// function requires arguments of type `T`, it must also accept
177 /// arguments of type `U`, hence such a conversion is safe.
178 #[lang="contravariant_type"]
179 #[deriving(Clone, PartialEq, Eq, PartialOrd, Ord)]
180 pub struct ContravariantType<T>;
182 impl<T> Copy for ContravariantType<T> {}
184 /// A marker type whose type parameter `T` is considered to be
185 /// invariant with respect to the type itself. This is (typically)
186 /// used to indicate that instances of the type `T` may be read or
187 /// written, even though that may not be apparent.
189 /// For more information about variance, refer to this Wikipedia
190 /// article <http://en.wikipedia.org/wiki/Variance_%28computer_science%29>.
194 /// The Cell type is an example which uses unsafe code to achieve
195 /// "interior" mutability:
198 /// pub struct Cell<T> { value: T }
202 /// The type system would infer that `value` is only read here and
203 /// never written, but in fact `Cell` uses unsafe code to achieve
204 /// interior mutability.
205 #[lang="invariant_type"]
206 #[deriving(Clone, PartialEq, Eq, PartialOrd, Ord)]
207 pub struct InvariantType<T>;
209 impl<T> Copy for InvariantType<T> {}
211 /// As `CovariantType`, but for lifetime parameters. Using
212 /// `CovariantLifetime<'a>` indicates that it is ok to substitute
213 /// a *longer* lifetime for `'a` than the one you originally
214 /// started with (e.g., you could convert any lifetime `'foo` to
215 /// `'static`). You almost certainly want `ContravariantLifetime`
216 /// instead, or possibly `InvariantLifetime`. The only case where
217 /// it would be appropriate is that you have a (type-casted, and
218 /// hence hidden from the type system) function pointer with a
219 /// signature like `fn(&'a T)` (and no other uses of `'a`). In
220 /// this case, it is ok to substitute a larger lifetime for `'a`
221 /// (e.g., `fn(&'static T)`), because the function is only
222 /// becoming more selective in terms of what it accepts as
225 /// For more information about variance, refer to this Wikipedia
226 /// article <http://en.wikipedia.org/wiki/Variance_%28computer_science%29>.
227 #[lang="covariant_lifetime"]
228 #[deriving(Clone, PartialEq, Eq, PartialOrd, Ord)]
229 pub struct CovariantLifetime<'a>;
231 impl<'a> Copy for CovariantLifetime<'a> {}
233 /// As `ContravariantType`, but for lifetime parameters. Using
234 /// `ContravariantLifetime<'a>` indicates that it is ok to
235 /// substitute a *shorter* lifetime for `'a` than the one you
236 /// originally started with (e.g., you could convert `'static` to
237 /// any lifetime `'foo`). This is appropriate for cases where you
238 /// have an unsafe pointer that is actually a pointer into some
239 /// memory with lifetime `'a`, and thus you want to limit the
240 /// lifetime of your data structure to `'a`. An example of where
241 /// this is used is the iterator for vectors.
243 /// For more information about variance, refer to this Wikipedia
244 /// article <http://en.wikipedia.org/wiki/Variance_%28computer_science%29>.
245 #[lang="contravariant_lifetime"]
246 #[deriving(Clone, PartialEq, Eq, PartialOrd, Ord)]
247 pub struct ContravariantLifetime<'a>;
249 impl<'a> Copy for ContravariantLifetime<'a> {}
251 /// As `InvariantType`, but for lifetime parameters. Using
252 /// `InvariantLifetime<'a>` indicates that it is not ok to
253 /// substitute any other lifetime for `'a` besides its original
254 /// value. This is appropriate for cases where you have an unsafe
255 /// pointer that is actually a pointer into memory with lifetime `'a`,
256 /// and this pointer is itself stored in an inherently mutable
257 /// location (such as a `Cell`).
258 #[lang="invariant_lifetime"]
259 #[deriving(Clone, PartialEq, Eq, PartialOrd, Ord)]
260 pub struct InvariantLifetime<'a>;
262 impl<'a> Copy for InvariantLifetime<'a> {}
264 /// A type which is considered "not sendable", meaning that it cannot
265 /// be safely sent between tasks, even if it is owned. This is
266 /// typically embedded in other types, such as `Gc`, to ensure that
267 /// their instances remain thread-local.
268 #[lang="no_send_bound"]
269 #[deriving(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
272 /// A type which is considered "not POD", meaning that it is not
273 /// implicitly copyable. This is typically embedded in other types to
274 /// ensure that they are never copied, even if they lack a destructor.
275 #[lang="no_copy_bound"]
276 #[deriving(Clone, PartialEq, Eq, PartialOrd, Ord)]
277 #[allow(missing_copy_implementations)]
280 /// A type which is considered "not sync", meaning that
281 /// its contents are not threadsafe, hence they cannot be
282 /// shared between tasks.
283 #[lang="no_sync_bound"]
284 #[deriving(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
287 /// A type which is considered managed by the GC. This is typically
288 /// embedded in other types.
289 #[lang="managed_bound"]
290 #[deriving(Clone, PartialEq, Eq, PartialOrd, Ord)]
291 #[allow(missing_copy_implementations)]