From 14ae7c879579658b8b150174c9a4c0cba2309e70 Mon Sep 17 00:00:00 2001 From: DongHun Kwak Date: Tue, 16 May 2023 16:02:59 +0900 Subject: [PATCH 1/1] Import ptr_meta 0.2.0 --- .cargo_vcs_info.json | 6 + Cargo.toml | 34 ++++ Cargo.toml.orig | 21 ++ LICENSE | 7 + README.md | 54 +++++ crates-io.md | 51 +++++ src/impls.rs | 6 + src/lib.rs | 472 +++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 651 insertions(+) create mode 100644 .cargo_vcs_info.json create mode 100644 Cargo.toml create mode 100644 Cargo.toml.orig create mode 100644 LICENSE create mode 100644 README.md create mode 100644 crates-io.md create mode 100644 src/impls.rs create mode 100644 src/lib.rs diff --git a/.cargo_vcs_info.json b/.cargo_vcs_info.json new file mode 100644 index 0000000..78ea48d --- /dev/null +++ b/.cargo_vcs_info.json @@ -0,0 +1,6 @@ +{ + "git": { + "sha1": "61c9ccd4f324e2d169e65fa575e04539d3991302" + }, + "path_in_vcs": "ptr_meta" +} \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..cbf72b8 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,34 @@ +# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO +# +# When uploading crates to the registry Cargo will automatically +# "normalize" Cargo.toml files for maximal compatibility +# with all versions of Cargo and also rewrite `path` dependencies +# to registry (e.g., crates.io) dependencies. +# +# If you are reading this file be aware that the original Cargo.toml +# will likely look very different (and much more reasonable). +# See Cargo.toml.orig for the original contents. + +[package] +edition = "2018" +name = "ptr_meta" +version = "0.2.0" +authors = ["David Koloski "] +description = "A radioactive stabilization of the ptr_meta rfc" +documentation = "https://docs.rs/ptr_meta" +readme = "crates-io.md" +keywords = [ + "ptr", + "meta", + "no_std", +] +categories = ["no-std"] +license = "MIT" +repository = "https://github.com/djkoloski/ptr_meta" + +[dependencies.ptr_meta_derive] +version = "=0.2.0" + +[features] +default = ["std"] +std = [] diff --git a/Cargo.toml.orig b/Cargo.toml.orig new file mode 100644 index 0000000..d3d8647 --- /dev/null +++ b/Cargo.toml.orig @@ -0,0 +1,21 @@ +[package] +name = "ptr_meta" +version = "0.2.0" +authors = ["David Koloski "] +edition = "2018" +description = "A radioactive stabilization of the ptr_meta rfc" +license = "MIT" +documentation = "https://docs.rs/ptr_meta" +repository = "https://github.com/djkoloski/ptr_meta" +keywords = ["ptr", "meta", "no_std"] +categories = ["no-std"] +readme = "crates-io.md" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +ptr_meta_derive = { version = "=0.2.0", path = "../ptr_meta_derive" } + +[features] +default = ["std"] +std = [] diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..f451631 --- /dev/null +++ b/LICENSE @@ -0,0 +1,7 @@ +Copyright 2021 David Koloski + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..eb509fa --- /dev/null +++ b/README.md @@ -0,0 +1,54 @@ +# ptr_meta   [![Latest Version]][crates.io] [![License]][license path] [![requires: rustc 1.47+]][Rust 1.47] + +[Latest Version]: https://img.shields.io/crates/v/ptr_meta.svg +[crates.io]: https://crates.io/crates/ptr_meta +[License]: https://img.shields.io/badge/license-MIT-blue.svg +[license path]: https://github.com/djkoloski/ptr_meta/blob/master/LICENSE +[requires: rustc 1.47+]: https://img.shields.io/badge/rustc-1.47+-lightgray.svg +[Rust 1.47]: https://blog.rust-lang.org/2020/10/08/Rust-1.47.html + +# ptr_meta + +A radioactive stabilization of the [`ptr_meta` RFC][rfc]. + +[rfc]: https://rust-lang.github.io/rfcs/2580-ptr-meta.html + +## Usage + +### Sized types + +Sized types already have `Pointee` implemented for them, so most of the time you won't have to worry +about them. However, trying to derive `Pointee` for a struct that may or may not have a DST as its +last field will cause an implementation conflict with the automatic sized implementation. + +### `slice`s and `str`s + +These core types have implementations built in. + +### Structs with a DST as its last field + +You can derive `Pointee` for last-field DSTs: + +```rust +use ptr_meta::Pointee; + +#[derive(Pointee)] +struct Block { + header: H, + elements: [T], +} +``` + +### Trait objects + +You can generate a `Pointee` for trait objects: + +```rust +use ptr_meta::pointee; + +// Generates Pointee for dyn Stringy +#[pointee] +trait Stringy { + fn as_string(&self) -> String; +} +``` diff --git a/crates-io.md b/crates-io.md new file mode 100644 index 0000000..5e5aee1 --- /dev/null +++ b/crates-io.md @@ -0,0 +1,51 @@ +A radioactive stabilization of the [`ptr_meta` RFC][rfc]. + +[rfc]: https://rust-lang.github.io/rfcs/2580-ptr-meta.html + +# Usage + +## Sized types + +All `Sized` types have `Pointee` implemented for them with a blanket implementation. You do not +need to derive `Pointee` for these types. + +## `slice`s and `str`s + +These core types have implementations built in. + +# `dyn Any` + +The trait object for this standard library type comes with an implementation built in. + +## Structs with a DST as its last field + +You can derive `Pointee` for structs with a trailing DST: + +```rust +use ptr_meta::Pointee; + +#[derive(Pointee)] +struct Block { + header: H, + elements: [T], +} +``` + +Note that this will only work when the last field is guaranteed to be a DST. Structs with a +generic last field may have a conflicting blanket impl since the generic type may be `Sized`. In +these cases, a collection of specific implementations may be required with the generic parameter +set to a slice, `str`, or specific trait object. + +## Trait objects + +You can generate a `Pointee` implementation for trait objects: + +```rust +use ptr_meta::pointee; + +// Generates Pointee for dyn Stringy +#[pointee] +trait Stringy { + fn as_string(&self) -> String; +} +``` diff --git a/src/impls.rs b/src/impls.rs new file mode 100644 index 0000000..4ee4219 --- /dev/null +++ b/src/impls.rs @@ -0,0 +1,6 @@ +use crate::{DynMetadata, Pointee}; +use core::any::Any; + +impl Pointee for dyn Any { + type Metadata = DynMetadata; +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..7b6ffac --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,472 @@ +//! A radioactive stabilization of the [`ptr_meta` RFC][rfc]. +//! +//! [rfc]: https://rust-lang.github.io/rfcs/2580-ptr-meta.html +//! +//! # Usage +//! +//! ## Sized types +//! +//! All `Sized` types have `Pointee` implemented for them with a blanket implementation. You do not +//! need to derive `Pointee` for these types. +//! +//! ## `slice`s and `str`s +//! +//! These core types have implementations built in. +//! +//!## `dyn Any` +//! +//! The trait object for this standard library type comes with an implementation built in. +//! +//! ## Structs with a DST as its last field +//! +//! You can derive `Pointee` for structs with a trailing DST: +//! +//! ``` +//! use ptr_meta::Pointee; +//! +//! #[derive(Pointee)] +//! struct Block { +//! header: H, +//! elements: [T], +//! } +//! ``` +//! +//! Note that this will only work when the last field is guaranteed to be a DST. Structs with a +//! generic last field may have a conflicting blanket impl since the generic type may be `Sized`. In +//! these cases, a collection of specific implementations may be required with the generic parameter +//! set to a slice, `str`, or specific trait object. +//! +//! ## Trait objects +//! +//! You can generate a `Pointee` implementation for trait objects: +//! +//! ``` +//! use ptr_meta::pointee; +//! +//! // Generates Pointee for dyn Stringy +//! #[pointee] +//! trait Stringy { +//! fn as_string(&self) -> String; +//! } +//! ``` + +#![cfg_attr(not(feature = "std"), no_std)] + +mod impls; + +use core::{alloc::Layout, cmp, fmt, hash, marker::PhantomData, ptr}; + +pub use ptr_meta_derive::{pointee, Pointee}; + +/// Provides the pointer metadata type of any pointed-to type. +/// +/// # Pointer metadata +/// +/// Raw pointer types and reference types in Rust can be thought of as made of two parts: a data +/// pointer that contains the memory address of the value, and some additional metadata. +/// +/// For statically-sized types (that implement `Sized`) as well as `extern` types, pointers are said +/// to be "thin". That is, their metadata is zero-sized and its type is `()`. +/// +/// Pointers to [dynamically-sized types][dst] are said to be "wide" or "fat", and they have +/// metadata that is not zero-sized: +/// +/// * For structs with a DST as the last field, the metadata of the struct is the metadata of the +/// last field. +/// * For the `str` type, the metadata is the length in bytes as a `usize`. +/// * For slice types like `[T]`, the metadata is the length in items as a `usize`. +/// * For trait objects like `dyn SomeTrait`, the metadata is [`DynMetadata`][DynMetadata] +/// (e.g. `DynMetadata`) which contains a pointer to the trait object's vtable. +/// +/// In the future, the Rust language may gain new kinds of types that have different pointer +/// metadata. +/// +/// [dst]: https://doc.rust-lang.org/nomicon/exotic-sizes.html#dynamically-sized-types-dsts +/// +/// +/// # The `Pointee` trait +/// +/// The point of this trait is its associated `Metadata` type, which may be `()`, `usize`, or +/// `DynMetadata<_>` as described above. It is automatically implemented for `Sized` types, slices, +/// and `str`s. An implementation can be generated for structs with a trailing DST and trait objects +/// using the [derive macro] and [attribute macro] respectively. +/// +/// [derive macro]: ptr_meta_derive::Pointee +/// [attribute macro]: ptr_meta_derive::pointee +/// +/// # Usage +/// +/// Raw pointers can be decomposed into the data address and metadata components with their +/// [`to_raw_parts`] methods. +/// +/// Alternatively, metadata alone can be extracted with the [`metadata`] function. A reference can +/// be passed to [`metadata`] and be implicitly coerced to a pointer. +/// +/// A (possibly wide) pointer can be put back together from its address and metadata with +/// [`from_raw_parts`] or [`from_raw_parts_mut`]. +/// +/// [`to_raw_parts`]: PtrExt::to_raw_parts +pub trait Pointee { + /// The type for metadata in pointers and references to `Self`. + type Metadata: Copy + Send + Sync + Ord + hash::Hash + Unpin; +} + +impl Pointee for T { + type Metadata = (); +} + +impl Pointee for [T] { + type Metadata = usize; +} + +impl Pointee for str { + type Metadata = usize; +} + +#[cfg(feature = "std")] +impl Pointee for ::std::ffi::CStr { + type Metadata = usize; +} + +#[cfg(feature = "std")] +impl Pointee for ::std::ffi::OsStr { + type Metadata = usize; +} + +#[repr(C)] +pub(crate) union PtrRepr { + pub(crate) const_ptr: *const T, + pub(crate) mut_ptr: *mut T, + pub(crate) components: PtrComponents, +} + +#[repr(C)] +pub(crate) struct PtrComponents { + pub(crate) data_address: *const (), + pub(crate) metadata: ::Metadata, +} + +impl Clone for PtrComponents { + fn clone(&self) -> Self { + Self { + data_address: self.data_address.clone(), + metadata: self.metadata.clone(), + } + } +} + +impl Copy for PtrComponents {} + +/// Returns the metadata component of a pointer. +/// +/// Values of type `*mut T`, `&T`, or `&mut T` can be passed directly to this function as they +/// implicitly coerce to `*const T`. +/// +/// # Example +/// +/// ``` +/// use ptr_meta::metadata; +/// +/// assert_eq!(metadata("foo"), 3_usize); +/// ``` +pub fn metadata(ptr: *const T) -> ::Metadata { + unsafe { PtrRepr { const_ptr: ptr }.components.metadata } +} + +/// Forms a (possibly wide) raw pointer from a data address and metadata. +/// +/// This function is safe to call, but the returned pointer is not necessarily safe to dereference. +/// For slices, see the documentation of [`slice::from_raw_parts`] for safety requirements. For +/// trait objects, the metadata must come from a pointer to the same underlying erased type. +/// +/// [`slice::from_raw_parts`]: core::slice::from_raw_parts +pub fn from_raw_parts( + data_address: *const (), + metadata: ::Metadata, +) -> *const T { + unsafe { + PtrRepr { + components: PtrComponents { + data_address, + metadata, + }, + } + .const_ptr + } +} + +/// Performs the same functionality as [`from_raw_parts`], except that a raw `*mut` pointer is +/// returned, as opposed to a raw `*const` pointer. +/// +/// See the documentation of [`from_raw_parts`] for more details. +pub fn from_raw_parts_mut( + data_address: *mut (), + metadata: ::Metadata, +) -> *mut T { + unsafe { + PtrRepr { + components: PtrComponents { + data_address, + metadata, + }, + } + .mut_ptr + } +} + +/// Extension methods for [`NonNull`](core::ptr::NonNull). +pub trait NonNullExt { + /// Creates a new non-null pointer from its raw parts. + fn from_raw_parts(raw: ptr::NonNull<()>, meta: ::Metadata) -> Self; + /// Converts a non-null pointer to its raw parts. + fn to_raw_parts(self) -> (ptr::NonNull<()>, ::Metadata); +} + +impl NonNullExt for ptr::NonNull { + fn from_raw_parts(raw: ptr::NonNull<()>, meta: ::Metadata) -> Self { + unsafe { Self::new_unchecked(from_raw_parts_mut(raw.as_ptr(), meta)) } + } + + fn to_raw_parts(self) -> (ptr::NonNull<()>, ::Metadata) { + let (raw, meta) = PtrExt::to_raw_parts(self.as_ptr()); + unsafe { (ptr::NonNull::new_unchecked(raw), meta) } + } +} + +/// Extension methods for pointers. +pub trait PtrExt { + /// The type's raw pointer (`*const ()` or `*mut ()`). + type Raw; + + /// Decompose a (possibly wide) pointer into its address and metadata components. + /// + /// The pointer can be later reconstructed with [`from_raw_parts`]. + fn to_raw_parts(self) -> (Self::Raw, ::Metadata); +} + +impl PtrExt for *const T { + type Raw = *const (); + + fn to_raw_parts(self) -> (Self::Raw, ::Metadata) { + unsafe { + (&self as *const Self) + .cast::<(Self::Raw, ::Metadata)>() + .read() + } + } +} + +impl PtrExt for *mut T { + type Raw = *mut (); + + fn to_raw_parts(self) -> (Self::Raw, ::Metadata) { + unsafe { + (&self as *const Self) + .cast::<(Self::Raw, ::Metadata)>() + .read() + } + } +} + +/// The metadata for a `Dyn = dyn SomeTrait` trait object type. +/// +/// It is a pointer to a vtable (virtual call table) that represents all the necessary information +/// to manipulate the concrete type stored inside a trait object. The vtable notably contains: +/// +/// * Type size +/// * Type alignment +/// * A pointer to the type’s `drop_in_place` impl (may be a no-op for plain-old-data) +/// * Pointers to all the methods for the type’s implementation of the trait +/// +/// Note that the first three are special because they’re necessary to allocate, drop, and +/// deallocate any trait object. +/// +/// It is possible to name this struct with a type parameter that is not a `dyn` trait object (for +/// example `DynMetadata`), but not to obtain a meaningful value of that struct. +#[repr(transparent)] +pub struct DynMetadata { + vtable_ptr: &'static VTable, + phantom: PhantomData, +} + +#[repr(C)] +struct VTable { + drop_in_place: fn(*mut ()), + size_of: usize, + align_of: usize, +} + +impl DynMetadata { + /// Returns the size of the type associated with this vtable. + pub fn size_of(self) -> usize { + self.vtable_ptr.size_of + } + + /// Returns the alignment of the type associated with this vtable. + pub fn align_of(self) -> usize { + self.vtable_ptr.align_of + } + + /// Returns the size and alignment together as a `Layout`. + pub fn layout(self) -> Layout { + unsafe { Layout::from_size_align_unchecked(self.size_of(), self.align_of()) } + } +} + +unsafe impl Send for DynMetadata {} +unsafe impl Sync for DynMetadata {} +impl fmt::Debug for DynMetadata { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_tuple("DynMetadata") + .field(&(self.vtable_ptr as *const VTable)) + .finish() + } +} +impl Unpin for DynMetadata {} +impl Copy for DynMetadata {} +impl Clone for DynMetadata { + #[inline] + fn clone(&self) -> Self { + *self + } +} +impl cmp::Eq for DynMetadata {} +impl cmp::PartialEq for DynMetadata { + #[inline] + fn eq(&self, other: &Self) -> bool { + ptr::eq(self.vtable_ptr, other.vtable_ptr) + } +} +impl cmp::Ord for DynMetadata { + #[inline] + fn cmp(&self, other: &Self) -> cmp::Ordering { + (self.vtable_ptr as *const VTable).cmp(&(other.vtable_ptr as *const VTable)) + } +} +impl cmp::PartialOrd for DynMetadata { + #[inline] + fn partial_cmp(&self, other: &Self) -> Option { + Some(self.cmp(other)) + } +} +impl hash::Hash for DynMetadata { + fn hash(&self, hasher: &mut H) { + ptr::hash(self.vtable_ptr, hasher) + } +} + +#[cfg(test)] +mod tests { + use super::{from_raw_parts, pointee, Pointee, PtrExt}; + use crate as ptr_meta; + + fn test_pointee(value: &T) { + let ptr = value as *const T; + let (raw, meta) = PtrExt::to_raw_parts(ptr); + let re_ptr = from_raw_parts::(raw, meta); + assert_eq!(ptr, re_ptr); + } + + #[test] + fn sized_types() { + test_pointee(&()); + test_pointee(&42); + test_pointee(&true); + test_pointee(&[1, 2, 3, 4]); + + struct TestUnit; + + test_pointee(&TestUnit); + + #[allow(dead_code)] + struct TestStruct { + a: (), + b: i32, + c: bool, + } + + test_pointee(&TestStruct { + a: (), + b: 42, + c: true, + }); + + struct TestTuple((), i32, bool); + + test_pointee(&TestTuple((), 42, true)); + + struct TestGeneric(T); + + test_pointee(&TestGeneric(42)); + } + + #[test] + fn unsized_types() { + test_pointee("hello world"); + test_pointee(&[1, 2, 3, 4] as &[i32]); + } + + #[test] + fn trait_objects() { + #[pointee] + trait TestTrait { + fn foo(&self); + } + + struct A; + + impl TestTrait for A { + fn foo(&self) {} + } + + let trait_object = &A as &dyn TestTrait; + + test_pointee(trait_object); + + let (_, meta) = PtrExt::to_raw_parts(trait_object as *const dyn TestTrait); + + assert_eq!(meta.size_of(), 0); + assert_eq!(meta.align_of(), 1); + + struct B(i32); + + impl TestTrait for B { + fn foo(&self) {} + } + + let b = B(42); + let trait_object = &b as &dyn TestTrait; + + test_pointee(trait_object); + + let (_, meta) = PtrExt::to_raw_parts(trait_object as *const dyn TestTrait); + + assert_eq!(meta.size_of(), 4); + assert_eq!(meta.align_of(), 4); + } + + #[test] + fn last_field_dst() { + #[allow(dead_code)] + #[derive(Pointee)] + struct Test { + head: H, + tail: [T], + } + + #[allow(dead_code)] + #[derive(Pointee)] + struct TestDyn { + tail: dyn core::any::Any, + } + + #[pointee] + trait TestTrait {} + + #[allow(dead_code)] + #[derive(Pointee)] + struct TestCustomDyn { + tail: dyn TestTrait, + } + } +} -- 2.34.1