Skip to content

Commit

Permalink
Merge pull request #10 from alecmocatta/ptr_metadata
Browse files Browse the repository at this point in the history
Bump from raw::TraitObject to ptr_metadata to fix on nightly
  • Loading branch information
alecmocatta authored Jul 10, 2021
2 parents 998f4f6 + 389514b commit 87981f8
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 36 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "metatype"
version = "0.2.0"
version = "0.2.1"
license = "MIT OR Apache-2.0"
authors = ["Alec Mocatta <alec@mocatta.net>"]
categories = ["development-tools","rust-patterns"]
Expand All @@ -10,7 +10,7 @@ Helper methods to determine whether a type is `TraitObject`, `Slice` or `Concret
"""
repository = "https://github.com/alecmocatta/metatype"
homepage = "https://github.com/alecmocatta/metatype"
documentation = "https://docs.rs/metatype/0.2.0"
documentation = "https://docs.rs/metatype/0.2.1"
readme = "README.md"
edition = "2018"

Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

[![Crates.io](https://img.shields.io/crates/v/metatype.svg?maxAge=86400)](https://crates.io/crates/metatype)
[![MIT / Apache 2.0 licensed](https://img.shields.io/crates/l/metatype.svg?maxAge=2592000)](#License)
[![Build Status](https://dev.azure.com/alecmocatta/metatype/_apis/build/status/tests?branchName=master)](https://dev.azure.com/alecmocatta/metatype/_build/latest?branchName=master)
[![Build Status](https://dev.azure.com/alecmocatta/metatype/_apis/build/status/tests?branchName=master)](https://dev.azure.com/alecmocatta/metatype/_build?definitionId=7)

[Docs](https://docs.rs/metatype/0.2.0)
[📖 Docs](https://docs.rs/metatype/0.2) | [💬 Chat](https://constellation.zulipchat.com/#narrow/stream/213236-subprojects)

Helper methods to determine whether a type is `TraitObject`, `Slice` or `Concrete`, and work with them respectively.

Expand Down Expand Up @@ -35,6 +35,7 @@ println!("vtable: {:?}", meta.vtable);
This currently requires Rust nightly for the `raw`, `specialization` and `arbitrary_self_types` features.

## License

Licensed under either of

* Apache License, Version 2.0, ([LICENSE-APACHE.txt](LICENSE-APACHE.txt) or http://www.apache.org/licenses/LICENSE-2.0)
Expand Down
12 changes: 6 additions & 6 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ jobs:
endpoint: alecmocatta
default:
rust_toolchain: nightly
rust_lint_toolchain: nightly-2019-10-17
rust_lint_toolchain: nightly-2021-07-09
rust_flags: ''
rust_features: ''
rust_target_check: ''
rust_target_build: ''
rust_target_run: ''
matrix:
windows:
imageName: 'vs2017-win2016'
rust_target_run: 'x86_64-pc-windows-msvc i686-pc-windows-msvc' # currently broken building crate-type=lib: x86_64-pc-windows-gnu i686-pc-windows-gnu
imageName: 'windows-latest'
rust_target_run: 'x86_64-pc-windows-msvc i686-pc-windows-msvc x86_64-pc-windows-gnu'
mac:
imageName: 'macos-10.13'
rust_target_run: 'x86_64-apple-darwin i686-apple-darwin'
imageName: 'macos-latest'
rust_target_run: 'x86_64-apple-darwin'
linux:
imageName: 'ubuntu-16.04'
imageName: 'ubuntu-latest'
rust_target_run: 'x86_64-unknown-linux-gnu i686-unknown-linux-gnu x86_64-unknown-linux-musl i686-unknown-linux-musl'
46 changes: 20 additions & 26 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,9 @@
//! This currently requires Rust nightly for the `raw`, `specialization` and
//! `arbitrary_self_types` features.

#![doc(html_root_url = "https://docs.rs/metatype/0.2.0")]
#![doc(html_root_url = "https://docs.rs/metatype/0.2.1")]
#![feature(arbitrary_self_types)]
#![feature(raw)]
#![feature(slice_from_raw_parts)]
#![feature(ptr_metadata)]
#![feature(specialization)]
#![warn(
missing_copy_implementations,
Expand All @@ -49,11 +48,13 @@
#![allow(
clippy::must_use_candidate,
clippy::not_unsafe_ptr_arg_deref,
clippy::use_self
clippy::use_self,
clippy::missing_panics_doc,
incomplete_features
)]

use std::{
any::{type_name, TypeId}, hash::{Hash, Hasher}, marker::PhantomData, mem::{align_of, align_of_val, forget, size_of, size_of_val, transmute_copy}, ptr::{slice_from_raw_parts_mut, NonNull}, raw
any::{type_name, TypeId}, hash::{Hash, Hasher}, marker::PhantomData, mem::{align_of, align_of_val, forget, size_of, size_of_val, transmute_copy}, ptr::{slice_from_raw_parts_mut, NonNull}
};

/// Implemented on all types, it provides helper methods to determine whether a type is `TraitObject`, `Slice` or `Concrete`, and work with them respectively.
Expand Down Expand Up @@ -111,24 +112,18 @@ impl<T: ?Sized> Type for T {
default type Meta = TraitObject;
#[inline]
default fn meta(self: *const Self) -> Self::Meta {
let trait_object = unsafe { transmute_coerce::<*const Self, raw::TraitObject>(self) };
assert_eq!(self as *const (), trait_object.data);
let ret = TraitObject {
vtable: unsafe { &*trait_object.vtable },
vtable: unsafe { transmute_coerce(std::ptr::metadata(self)) },
};
type_coerce(ret)
}
#[inline]
default fn data(self: *const Self) -> *const () {
let trait_object = unsafe { transmute_coerce::<*const Self, raw::TraitObject>(self) };
assert_eq!(self as *const (), trait_object.data);
self as *const ()
self.cast()
}
#[inline]
default fn data_mut(self: *mut Self) -> *mut () {
let trait_object = unsafe { transmute_coerce::<*const Self, raw::TraitObject>(self) };
assert_eq!(self as *mut (), trait_object.data);
self as *mut ()
self.cast()
}
#[inline]
default fn dangling(t: Self::Meta) -> NonNull<Self> {
Expand All @@ -153,8 +148,7 @@ impl<T: ?Sized> Type for T {
let t: TraitObject = type_coerce(t);
let vtable: *const () = t.vtable;
let vtable = vtable as *mut ();
let ret = raw::TraitObject { data: thin, vtable };
unsafe { transmute_coerce::<raw::TraitObject, *mut Self>(ret) }
std::ptr::from_raw_parts_mut(thin, unsafe { transmute_coerce(vtable) })
}
}
#[doc(hidden)]
Expand All @@ -167,11 +161,11 @@ impl<T: Sized> Type for T {
}
#[inline]
fn data(self: *const Self) -> *const () {
self as *const ()
self.cast()
}
#[inline]
fn data_mut(self: *mut Self) -> *mut () {
self as *mut ()
self.cast()
}
fn dangling(_t: Self::Meta) -> NonNull<Self> {
NonNull::dangling()
Expand All @@ -195,11 +189,11 @@ impl<T: Sized> Type for [T] {
}
#[inline]
fn data(self: *const Self) -> *const () {
self as *const ()
self.cast()
}
#[inline]
fn data_mut(self: *mut Self) -> *mut () {
self as *mut ()
self.cast()
}
fn dangling(t: Self::Meta) -> NonNull<Self> {
let slice = slice_from_raw_parts_mut(NonNull::<T>::dangling().as_ptr(), t.len);
Expand All @@ -221,11 +215,11 @@ impl Type for str {
}
#[inline]
fn data(self: *const Self) -> *const () {
self as *const ()
self.cast()
}
#[inline]
fn data_mut(self: *mut Self) -> *mut () {
self as *mut ()
self.cast()
}
fn dangling(t: Self::Meta) -> NonNull<Self> {
let bytes: *mut [u8] = <[u8]>::dangling(t).as_ptr();
Expand Down Expand Up @@ -311,11 +305,11 @@ mod tests {
assert_eq!(Type::meta_type(&a), MetaType::Concrete);
let meta: TraitObject = type_coerce(Type::meta(&*a));
let dangling = <dyn any::Any as Type>::dangling(type_coerce(meta));
let _fat = <dyn any::Any as Type>::fatten(dangling.as_ptr() as *mut (), type_coerce(meta));
let _fat = <dyn any::Any as Type>::fatten(dangling.as_ptr().cast(), type_coerce(meta));
let mut x: usize = 0;
let x_ptr: *mut usize = &mut x;
let mut x_ptr: NonNull<dyn any::Any> = NonNull::new(<dyn any::Any as Type>::fatten(
x_ptr as *mut (),
x_ptr.cast(),
type_coerce(meta),
))
.unwrap();
Expand All @@ -327,7 +321,7 @@ mod tests {
let a: &[usize] = &[1, 2, 3];
assert_eq!(Type::meta_type(a), MetaType::Slice);
let dangling = <[String] as Type>::dangling(Slice { len: 100 });
let _fat = <[String] as Type>::fatten(dangling.as_ptr() as *mut (), Slice { len: 100 });
let _fat = <[String] as Type>::fatten(dangling.as_ptr().cast(), Slice { len: 100 });

let a: Box<[usize]> = vec![1_usize, 2, 3].into_boxed_slice();
assert_eq!(Type::meta_type(&*a), MetaType::Slice);
Expand All @@ -337,6 +331,6 @@ mod tests {
assert_eq!(Type::meta_type(a), MetaType::Slice);
assert_eq!(Type::meta_type(&a), MetaType::Concrete);
let dangling = <str as Type>::dangling(Slice { len: 100 });
let _fat = <str as Type>::fatten(dangling.as_ptr() as *mut (), Slice { len: 100 });
let _fat = <str as Type>::fatten(dangling.as_ptr().cast(), Slice { len: 100 });
}
}

0 comments on commit 87981f8

Please sign in to comment.