2013-07-22 02:20:52 +02:00
|
|
|
// Copyright 2013 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-10-27 23:37:07 +01:00
|
|
|
#![allow(missing_docs)]
|
2015-08-13 02:23:48 +02:00
|
|
|
#![unstable(feature = "raw", issue = "27751")]
|
2014-02-16 09:04:33 +01:00
|
|
|
|
2014-03-03 01:01:13 +01:00
|
|
|
//! Contains struct definitions for the layout of compiler built-in types.
|
|
|
|
//!
|
|
|
|
//! They can be used as targets of transmutes in unsafe code for manipulating
|
|
|
|
//! the raw representations directly.
|
|
|
|
//!
|
2014-04-21 06:49:39 +02:00
|
|
|
//! Their definition should always match the ABI defined in `rustc::back::abi`.
|
2014-03-03 01:01:13 +01:00
|
|
|
|
2015-02-12 12:50:44 +01:00
|
|
|
/// The representation of a trait object like `&SomeTrait`.
|
|
|
|
///
|
|
|
|
/// This struct has the same layout as types like `&SomeTrait` and
|
2015-04-16 22:12:13 +02:00
|
|
|
/// `Box<AnotherTrait>`. The [Trait Objects chapter of the
|
2015-02-12 12:50:44 +01:00
|
|
|
/// Book][moreinfo] contains more details about the precise nature of
|
|
|
|
/// these internals.
|
|
|
|
///
|
2017-03-15 17:13:55 +01:00
|
|
|
/// [moreinfo]: ../../book/first-edition/trait-objects.html#representation
|
2015-02-12 12:50:44 +01:00
|
|
|
///
|
|
|
|
/// `TraitObject` is guaranteed to match layouts, but it is not the
|
|
|
|
/// type of trait objects (e.g. the fields are not directly accessible
|
|
|
|
/// on a `&SomeTrait`) nor does it control that layout (changing the
|
2015-04-22 23:06:32 +02:00
|
|
|
/// definition will not change the layout of a `&SomeTrait`). It is
|
2015-02-12 12:50:44 +01:00
|
|
|
/// only designed to be used by unsafe code that needs to manipulate
|
|
|
|
/// the low-level details.
|
|
|
|
///
|
2016-08-04 02:22:43 +02:00
|
|
|
/// There is no way to refer to all trait objects generically, so the only
|
2015-02-12 12:50:44 +01:00
|
|
|
/// way to create values of this type is with functions like
|
2016-08-04 02:22:43 +02:00
|
|
|
/// [`std::mem::transmute`][transmute]. Similarly, the only way to create a true
|
2015-02-12 12:50:44 +01:00
|
|
|
/// trait object from a `TraitObject` value is with `transmute`.
|
|
|
|
///
|
2016-08-04 02:22:43 +02:00
|
|
|
/// [transmute]: ../intrinsics/fn.transmute.html
|
|
|
|
///
|
2015-02-12 12:50:44 +01:00
|
|
|
/// Synthesizing a trait object with mismatched types—one where the
|
|
|
|
/// vtable does not correspond to the type of the value to which the
|
|
|
|
/// data pointer points—is highly likely to lead to undefined
|
2015-10-13 15:44:11 +02:00
|
|
|
/// behavior.
|
2015-02-12 12:50:44 +01:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
2015-07-27 16:50:19 +02:00
|
|
|
/// #![feature(raw)]
|
|
|
|
///
|
2016-08-04 02:22:43 +02:00
|
|
|
/// use std::{mem, raw};
|
2015-02-12 12:50:44 +01:00
|
|
|
///
|
|
|
|
/// // an example trait
|
|
|
|
/// trait Foo {
|
|
|
|
/// fn bar(&self) -> i32;
|
|
|
|
/// }
|
2016-08-04 02:22:43 +02:00
|
|
|
///
|
2015-02-12 12:50:44 +01:00
|
|
|
/// impl Foo for i32 {
|
|
|
|
/// fn bar(&self) -> i32 {
|
|
|
|
/// *self + 1
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// let value: i32 = 123;
|
|
|
|
///
|
|
|
|
/// // let the compiler make a trait object
|
|
|
|
/// let object: &Foo = &value;
|
|
|
|
///
|
|
|
|
/// // look at the raw representation
|
|
|
|
/// let raw_object: raw::TraitObject = unsafe { mem::transmute(object) };
|
|
|
|
///
|
|
|
|
/// // the data pointer is the address of `value`
|
|
|
|
/// assert_eq!(raw_object.data as *const i32, &value as *const _);
|
|
|
|
///
|
|
|
|
/// let other_value: i32 = 456;
|
|
|
|
///
|
|
|
|
/// // construct a new object, pointing to a different `i32`, being
|
|
|
|
/// // careful to use the `i32` vtable from `object`
|
|
|
|
/// let synthesized: &Foo = unsafe {
|
|
|
|
/// mem::transmute(raw::TraitObject {
|
|
|
|
/// data: &other_value as *const _ as *mut (),
|
2016-08-04 02:22:43 +02:00
|
|
|
/// vtable: raw_object.vtable,
|
2015-02-12 12:50:44 +01:00
|
|
|
/// })
|
|
|
|
/// };
|
2014-03-03 01:01:13 +01:00
|
|
|
///
|
2016-08-04 02:22:43 +02:00
|
|
|
/// // it should work just as if we had constructed a trait object out of
|
2015-02-12 12:50:44 +01:00
|
|
|
/// // `other_value` directly
|
|
|
|
/// assert_eq!(synthesized.bar(), 457);
|
|
|
|
/// ```
|
2014-10-25 19:28:17 +02:00
|
|
|
#[repr(C)]
|
2015-03-30 15:40:52 +02:00
|
|
|
#[derive(Copy, Clone)]
|
2016-03-05 03:49:43 +01:00
|
|
|
#[allow(missing_debug_implementations)]
|
DST coercions and DST structs
[breaking-change]
1. The internal layout for traits has changed from (vtable, data) to (data, vtable). If you were relying on this in unsafe transmutes, you might get some very weird and apparently unrelated errors. You should not be doing this! Prefer not to do this at all, but if you must, you should use raw::TraitObject rather than hardcoding rustc's internal representation into your code.
2. The minimal type of reference-to-vec-literals (e.g., `&[1, 2, 3]`) is now a fixed size vec (e.g., `&[int, ..3]`) where it used to be an unsized vec (e.g., `&[int]`). If you want the unszied type, you must explicitly give the type (e.g., `let x: &[_] = &[1, 2, 3]`). Note in particular where multiple blocks must have the same type (e.g., if and else clauses, vec elements), the compiler will not coerce to the unsized type without a hint. E.g., `[&[1], &[1, 2]]` used to be a valid expression of type '[&[int]]'. It no longer type checks since the first element now has type `&[int, ..1]` and the second has type &[int, ..2]` which are incompatible.
3. The type of blocks (including functions) must be coercible to the expected type (used to be a subtype). Mostly this makes things more flexible and not less (in particular, in the case of coercing function bodies to the return type). However, in some rare cases, this is less flexible. TBH, I'm not exactly sure of the exact effects. I think the change causes us to resolve inferred type variables slightly earlier which might make us slightly more restrictive. Possibly it only affects blocks with unreachable code. E.g., `if ... { fail!(); "Hello" }` used to type check, it no longer does. The fix is to add a semicolon after the string.
2014-08-04 14:20:11 +02:00
|
|
|
pub struct TraitObject {
|
2014-08-06 11:59:40 +02:00
|
|
|
pub data: *mut (),
|
|
|
|
pub vtable: *mut (),
|
DST coercions and DST structs
[breaking-change]
1. The internal layout for traits has changed from (vtable, data) to (data, vtable). If you were relying on this in unsafe transmutes, you might get some very weird and apparently unrelated errors. You should not be doing this! Prefer not to do this at all, but if you must, you should use raw::TraitObject rather than hardcoding rustc's internal representation into your code.
2. The minimal type of reference-to-vec-literals (e.g., `&[1, 2, 3]`) is now a fixed size vec (e.g., `&[int, ..3]`) where it used to be an unsized vec (e.g., `&[int]`). If you want the unszied type, you must explicitly give the type (e.g., `let x: &[_] = &[1, 2, 3]`). Note in particular where multiple blocks must have the same type (e.g., if and else clauses, vec elements), the compiler will not coerce to the unsized type without a hint. E.g., `[&[1], &[1, 2]]` used to be a valid expression of type '[&[int]]'. It no longer type checks since the first element now has type `&[int, ..1]` and the second has type &[int, ..2]` which are incompatible.
3. The type of blocks (including functions) must be coercible to the expected type (used to be a subtype). Mostly this makes things more flexible and not less (in particular, in the case of coercing function bodies to the return type). However, in some rare cases, this is less flexible. TBH, I'm not exactly sure of the exact effects. I think the change causes us to resolve inferred type variables slightly earlier which might make us slightly more restrictive. Possibly it only affects blocks with unreachable code. E.g., `if ... { fail!(); "Hello" }` used to type check, it no longer does. The fix is to add a semicolon after the string.
2014-08-04 14:20:11 +02:00
|
|
|
}
|