Remove the deprecated core::raw
and std::raw
module.
This commit is contained in:
parent
cd48e61c5d
commit
0d1919c7ab
8 changed files with 15 additions and 146 deletions
|
@ -255,7 +255,6 @@ pub mod option;
|
||||||
pub mod panic;
|
pub mod panic;
|
||||||
pub mod panicking;
|
pub mod panicking;
|
||||||
pub mod pin;
|
pub mod pin;
|
||||||
pub mod raw;
|
|
||||||
pub mod result;
|
pub mod result;
|
||||||
#[unstable(feature = "async_stream", issue = "79024")]
|
#[unstable(feature = "async_stream", issue = "79024")]
|
||||||
pub mod stream;
|
pub mod stream;
|
||||||
|
|
|
@ -1,90 +0,0 @@
|
||||||
#![allow(missing_docs)]
|
|
||||||
#![unstable(feature = "raw", issue = "27751")]
|
|
||||||
#![rustc_deprecated(
|
|
||||||
since = "1.53.0",
|
|
||||||
reason = "use pointer metadata APIs instead https://github.com/rust-lang/rust/issues/81513"
|
|
||||||
)]
|
|
||||||
|
|
||||||
//! 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.
|
|
||||||
//!
|
|
||||||
//! Their definition should always match the ABI defined in
|
|
||||||
//! `rustc_middle::ty::layout`.
|
|
||||||
|
|
||||||
/// The representation of a trait object like `&dyn SomeTrait`.
|
|
||||||
///
|
|
||||||
/// This struct has the same layout as types like `&dyn SomeTrait` and
|
|
||||||
/// `Box<dyn AnotherTrait>`.
|
|
||||||
///
|
|
||||||
/// `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 `&dyn SomeTrait`) nor does it control that layout (changing the
|
|
||||||
/// definition will not change the layout of a `&dyn SomeTrait`). It is
|
|
||||||
/// only designed to be used by unsafe code that needs to manipulate
|
|
||||||
/// the low-level details.
|
|
||||||
///
|
|
||||||
/// There is no way to refer to all trait objects generically, so the only
|
|
||||||
/// way to create values of this type is with functions like
|
|
||||||
/// [`std::mem::transmute`][transmute]. Similarly, the only way to create a true
|
|
||||||
/// trait object from a `TraitObject` value is with `transmute`.
|
|
||||||
///
|
|
||||||
/// [transmute]: crate::intrinsics::transmute
|
|
||||||
///
|
|
||||||
/// 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
|
|
||||||
/// behavior.
|
|
||||||
///
|
|
||||||
/// # Examples
|
|
||||||
///
|
|
||||||
/// ```
|
|
||||||
/// #![feature(raw)]
|
|
||||||
///
|
|
||||||
/// use std::{mem, raw};
|
|
||||||
///
|
|
||||||
/// // an example trait
|
|
||||||
/// trait Foo {
|
|
||||||
/// fn bar(&self) -> i32;
|
|
||||||
/// }
|
|
||||||
///
|
|
||||||
/// impl Foo for i32 {
|
|
||||||
/// fn bar(&self) -> i32 {
|
|
||||||
/// *self + 1
|
|
||||||
/// }
|
|
||||||
/// }
|
|
||||||
///
|
|
||||||
/// let value: i32 = 123;
|
|
||||||
///
|
|
||||||
/// // let the compiler make a trait object
|
|
||||||
/// let object: &dyn 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: &dyn Foo = unsafe {
|
|
||||||
/// mem::transmute(raw::TraitObject {
|
|
||||||
/// data: &other_value as *const _ as *mut (),
|
|
||||||
/// vtable: raw_object.vtable,
|
|
||||||
/// })
|
|
||||||
/// };
|
|
||||||
///
|
|
||||||
/// // it should work just as if we had constructed a trait object out of
|
|
||||||
/// // `other_value` directly
|
|
||||||
/// assert_eq!(synthesized.bar(), 457);
|
|
||||||
/// ```
|
|
||||||
#[repr(C)]
|
|
||||||
#[derive(Copy, Clone)]
|
|
||||||
#[allow(missing_debug_implementations)]
|
|
||||||
pub struct TraitObject {
|
|
||||||
pub data: *mut (),
|
|
||||||
pub vtable: *mut (),
|
|
||||||
}
|
|
|
@ -29,7 +29,6 @@
|
||||||
#![feature(try_find)]
|
#![feature(try_find)]
|
||||||
#![feature(is_sorted)]
|
#![feature(is_sorted)]
|
||||||
#![feature(pattern)]
|
#![feature(pattern)]
|
||||||
#![feature(raw)]
|
|
||||||
#![feature(sort_internals)]
|
#![feature(sort_internals)]
|
||||||
#![feature(slice_partition_at_index)]
|
#![feature(slice_partition_at_index)]
|
||||||
#![feature(maybe_uninit_uninit_array)]
|
#![feature(maybe_uninit_uninit_array)]
|
||||||
|
|
|
@ -97,28 +97,6 @@ fn test_transmute_copy() {
|
||||||
assert_eq!(1, unsafe { transmute_copy(&1) });
|
assert_eq!(1, unsafe { transmute_copy(&1) });
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remove this test when `std::raw` is removed.
|
|
||||||
// The replacement pointer metadata APIs are tested in library/core/tests/ptr.rs
|
|
||||||
#[allow(deprecated)]
|
|
||||||
#[test]
|
|
||||||
fn test_transmute() {
|
|
||||||
trait Foo {
|
|
||||||
fn dummy(&self) {}
|
|
||||||
}
|
|
||||||
impl Foo for isize {}
|
|
||||||
|
|
||||||
let a = box 100isize as Box<dyn Foo>;
|
|
||||||
unsafe {
|
|
||||||
let x: ::core::raw::TraitObject = transmute(a);
|
|
||||||
assert!(*(x.data as *const isize) == 100);
|
|
||||||
let _x: Box<dyn Foo> = transmute(x);
|
|
||||||
}
|
|
||||||
|
|
||||||
unsafe {
|
|
||||||
assert_eq!(transmute::<_, Vec<u8>>("L".to_string()), [76]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
fn test_discriminant_send_sync() {
|
fn test_discriminant_send_sync() {
|
||||||
|
|
|
@ -23,7 +23,6 @@
|
||||||
#![feature(unwind_attributes)]
|
#![feature(unwind_attributes)]
|
||||||
#![feature(abi_thiscall)]
|
#![feature(abi_thiscall)]
|
||||||
#![feature(rustc_attrs)]
|
#![feature(rustc_attrs)]
|
||||||
#![feature(raw)]
|
|
||||||
#![panic_runtime]
|
#![panic_runtime]
|
||||||
#![feature(panic_runtime)]
|
#![feature(panic_runtime)]
|
||||||
// `real_imp` is unused with Miri, so silence warnings.
|
// `real_imp` is unused with Miri, so silence warnings.
|
||||||
|
|
|
@ -303,7 +303,6 @@
|
||||||
#![feature(pin_static_ref)]
|
#![feature(pin_static_ref)]
|
||||||
#![feature(prelude_import)]
|
#![feature(prelude_import)]
|
||||||
#![feature(ptr_internals)]
|
#![feature(ptr_internals)]
|
||||||
#![feature(raw)]
|
|
||||||
#![feature(ready_macro)]
|
#![feature(ready_macro)]
|
||||||
#![feature(rustc_attrs)]
|
#![feature(rustc_attrs)]
|
||||||
#![feature(rustc_private)]
|
#![feature(rustc_private)]
|
||||||
|
@ -455,9 +454,6 @@ pub use core::pin;
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
pub use core::ptr;
|
pub use core::ptr;
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
#[stable(feature = "rust1", since = "1.0.0")]
|
||||||
#[allow(deprecated, deprecated_in_future)]
|
|
||||||
pub use core::raw;
|
|
||||||
#[stable(feature = "rust1", since = "1.0.0")]
|
|
||||||
pub use core::result;
|
pub use core::result;
|
||||||
#[unstable(feature = "async_stream", issue = "79024")]
|
#[unstable(feature = "async_stream", issue = "79024")]
|
||||||
pub use core::stream;
|
pub use core::stream;
|
||||||
|
|
|
@ -1,12 +1,6 @@
|
||||||
// run-pass
|
// run-pass
|
||||||
|
|
||||||
// Remove this file when `std::raw` is removed.
|
#![feature(ptr_metadata)]
|
||||||
// The replacement pointer metadata APIs are tested in library/core/tests/ptr.rs
|
|
||||||
#![allow(deprecated)]
|
|
||||||
#![feature(raw)]
|
|
||||||
|
|
||||||
use std::mem;
|
|
||||||
use std::raw;
|
|
||||||
|
|
||||||
trait Foo {
|
trait Foo {
|
||||||
fn foo(&self) {}
|
fn foo(&self) {}
|
||||||
|
@ -31,13 +25,10 @@ fn main() {
|
||||||
|
|
||||||
// And conversion to a void pointer/address for trait objects too.
|
// And conversion to a void pointer/address for trait objects too.
|
||||||
let a: *mut dyn Foo = &mut Bar;
|
let a: *mut dyn Foo = &mut Bar;
|
||||||
let b = a as *mut ();
|
let b = a as *mut () as usize;
|
||||||
let c = a as *const () as usize;
|
let c = a as *const () as usize;
|
||||||
let d = unsafe {
|
let d = a.to_raw_parts().0 as usize;
|
||||||
let r: raw::TraitObject = mem::transmute(a);
|
|
||||||
r.data
|
|
||||||
};
|
|
||||||
|
|
||||||
assert_eq!(b, d);
|
assert_eq!(b, d);
|
||||||
assert_eq!(c, d as usize);
|
assert_eq!(c, d);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,12 +1,11 @@
|
||||||
// run-pass
|
// run-pass
|
||||||
// Test structs with always-unsized fields.
|
// Test structs with always-unsized fields.
|
||||||
|
|
||||||
|
|
||||||
#![allow(warnings)]
|
#![allow(warnings)]
|
||||||
#![feature(box_syntax, unsize, raw)]
|
#![feature(box_syntax, unsize, ptr_metadata)]
|
||||||
|
|
||||||
use std::mem;
|
use std::mem;
|
||||||
use std::raw;
|
use std::ptr;
|
||||||
use std::slice;
|
use std::slice;
|
||||||
|
|
||||||
struct Foo<T> {
|
struct Foo<T> {
|
||||||
|
@ -28,7 +27,7 @@ trait Tr {
|
||||||
}
|
}
|
||||||
|
|
||||||
struct St {
|
struct St {
|
||||||
f: usize
|
f: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Tr for St {
|
impl Tr for St {
|
||||||
|
@ -38,7 +37,7 @@ impl Tr for St {
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Qux<'a> {
|
struct Qux<'a> {
|
||||||
f: Tr+'a
|
f: Tr + 'a,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
|
@ -56,7 +55,7 @@ pub fn main() {
|
||||||
|
|
||||||
unsafe {
|
unsafe {
|
||||||
struct Foo_<T> {
|
struct Foo_<T> {
|
||||||
f: [T; 3]
|
f: [T; 3],
|
||||||
}
|
}
|
||||||
|
|
||||||
let data: Box<Foo_<i32>> = box Foo_ { f: [1, 2, 3] };
|
let data: Box<Foo_<i32>> = box Foo_ { f: [1, 2, 3] };
|
||||||
|
@ -69,8 +68,8 @@ pub fn main() {
|
||||||
f2: [u8; 5],
|
f2: [u8; 5],
|
||||||
}
|
}
|
||||||
|
|
||||||
let data: Box<_> = box Baz_ {
|
let data: Box<_> =
|
||||||
f1: 42, f2: ['a' as u8, 'b' as u8, 'c' as u8, 'd' as u8, 'e' as u8] };
|
box Baz_ { f1: 42, f2: ['a' as u8, 'b' as u8, 'c' as u8, 'd' as u8, 'e' as u8] };
|
||||||
let x: &Baz = mem::transmute(slice::from_raw_parts(&*data, 5));
|
let x: &Baz = mem::transmute(slice::from_raw_parts(&*data, 5));
|
||||||
assert_eq!(x.f1, 42);
|
assert_eq!(x.f1, 42);
|
||||||
let chs: Vec<char> = x.f2.chars().collect();
|
let chs: Vec<char> = x.f2.chars().collect();
|
||||||
|
@ -82,15 +81,13 @@ pub fn main() {
|
||||||
assert_eq!(chs[4], 'e');
|
assert_eq!(chs[4], 'e');
|
||||||
|
|
||||||
struct Qux_ {
|
struct Qux_ {
|
||||||
f: St
|
f: St,
|
||||||
}
|
}
|
||||||
|
|
||||||
let obj: Box<St> = box St { f: 42 };
|
let obj: Box<St> = box St { f: 42 };
|
||||||
let obj: &Tr = &*obj;
|
let obj: &Tr = &*obj;
|
||||||
let obj: raw::TraitObject = mem::transmute(&*obj);
|
|
||||||
let data: Box<_> = box Qux_ { f: St { f: 234 } };
|
let data: Box<_> = box Qux_ { f: St { f: 234 } };
|
||||||
let x: &Qux = mem::transmute(raw::TraitObject { vtable: obj.vtable,
|
let x: &Qux = &*ptr::from_raw_parts::<Qux>((&*data as *const _).cast(), ptr::metadata(obj));
|
||||||
data: mem::transmute(&*data) });
|
|
||||||
assert_eq!(x.f.foo(), 234);
|
assert_eq!(x.f.foo(), 234);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue