| 1 | Const `TypeId` and non-'static `TypeId` |
| 2 | ======================================= |
| 3 | |
| 4 | [<img alt="github" src="https://img.shields.io/badge/github-dtolnay/typeid-8da0cb?style=for-the-badge&labelColor=555555&logo=github" height="20">](https://github.com/dtolnay/typeid) |
| 5 | [<img alt="crates.io" src="https://img.shields.io/crates/v/typeid.svg?style=for-the-badge&color=fc8d62&logo=rust" height="20">](https://crates.io/crates/typeid) |
| 6 | [<img alt="docs.rs" src="https://img.shields.io/badge/docs.rs-typeid-66c2a5?style=for-the-badge&labelColor=555555&logo=docs.rs" height="20">](https://docs.rs/typeid) |
| 7 | [<img alt="build status" src="https://img.shields.io/github/actions/workflow/status/dtolnay/typeid/ci.yml?branch=master&style=for-the-badge" height="20">](https://github.com/dtolnay/typeid/actions?query=branch%3Amaster) |
| 8 | |
| 9 | [`ConstTypeId`]: https://docs.rs/typeid/1/typeid/struct.ConstTypeId.html |
| 10 | [`typeid::of`]: https://docs.rs/typeid/1/typeid/fn.of.html |
| 11 | |
| 12 | ### Const `TypeId` |
| 13 | |
| 14 | This crate provides [`ConstTypeId`], which is like [`core::any::TypeId`] but is |
| 15 | constructible in const in stable Rust. (The standard library's TypeId's is |
| 16 | nightly-only to construct in const; the tracking issue for this is |
| 17 | [rust#77125].) |
| 18 | |
| 19 | [`core::any::TypeId`]: https://doc.rust-lang.org/core/any/struct.TypeId.html |
| 20 | [rust#77125]: https://github.com/rust-lang/rust/issues/77125 |
| 21 | |
| 22 | Being able to construct `ConstTypeId` in const makes it suitable for use cases |
| 23 | that rely on static promotion: |
| 24 | |
| 25 | ```rust |
| 26 | use std::fmt::{self, Debug, Display}; |
| 27 | use std::ptr; |
| 28 | use typeid::ConstTypeId; |
| 29 | |
| 30 | pub struct ObjectVTable { |
| 31 | type_id: ConstTypeId, |
| 32 | drop_in_place: unsafe fn(*mut ()), |
| 33 | display: unsafe fn(*const (), &mut fmt::Formatter) -> fmt::Result, |
| 34 | debug: unsafe fn(*const (), &mut fmt::Formatter) -> fmt::Result, |
| 35 | } |
| 36 | |
| 37 | impl ObjectVTable { |
| 38 | pub const fn new<T: Display + Debug>() -> &'static Self { |
| 39 | &ObjectVTable { |
| 40 | type_id: const { ConstTypeId::of::<T>() }, |
| 41 | drop_in_place: |ptr| unsafe { ptr::drop_in_place(ptr.cast::<T>()) }, |
| 42 | display: |ptr, f| unsafe { Display::fmt(&*ptr.cast::<T>(), f) }, |
| 43 | debug: |ptr, f| unsafe { Debug::fmt(&*ptr.cast::<T>(), f) }, |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | ``` |
| 48 | |
| 49 | and in associated constants: |
| 50 | |
| 51 | ```rust |
| 52 | use typeid::ConstTypeId; |
| 53 | |
| 54 | pub trait GetTypeId { |
| 55 | const TYPEID: ConstTypeId; |
| 56 | } |
| 57 | |
| 58 | impl<T: 'static> GetTypeId for T { |
| 59 | const TYPEID: ConstTypeId = ConstTypeId::of::<Self>(); |
| 60 | } |
| 61 | ``` |
| 62 | |
| 63 | <br> |
| 64 | |
| 65 | ### Non-'static `TypeId` |
| 66 | |
| 67 | This crate provides [`typeid::of`], which takes an arbitrary non-'static type |
| 68 | `T` and produces the `TypeId` for the type obtained by replacing all lifetimes |
| 69 | in `T` by `'static`, other than higher-rank lifetimes found in trait objects. |
| 70 | |
| 71 | For example if `T` is `&'b dyn for<'a> Trait<'a, 'c>`, then `typeid::of::<T>()` |
| 72 | produces the TypeId of `&'static dyn for<'a> Trait<'a, 'static>`. |
| 73 | |
| 74 | It should be obvious that unlike with the standard library's TypeId, |
| 75 | `typeid::of::<A>() == typeid::of::<B>()` does **not** mean that `A` and `B` are |
| 76 | the same type. However, there is a common special case where this behavior is |
| 77 | exactly what is needed. If: |
| 78 | |
| 79 | - `A` is an arbitrary non-'static type parameter, _and_ |
| 80 | - `B` is 'static, _and_ |
| 81 | - all types with the same id as `B` are also 'static |
| 82 | |
| 83 | then `typeid::of::<A>() == typeid::of::<B>()` guarantees that `A` and `B` are |
| 84 | the same type. |
| 85 | |
| 86 | ```rust |
| 87 | use core::any::TypeId; |
| 88 | use core::slice; |
| 89 | |
| 90 | pub fn example<T>(slice: &[T]) { |
| 91 | // T is arbitrary and non-'static. |
| 92 | |
| 93 | if typeid::of::<T>() == TypeId::of::<u8>() { |
| 94 | // T is definitely u8 |
| 95 | let bytes = unsafe { slice::from_raw_parts(slice.as_ptr().cast(), slice.len()) }; |
| 96 | process_bytes(bytes); |
| 97 | } else { |
| 98 | for t in slice { |
| 99 | process(t); |
| 100 | } |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | fn process<T>(_: &T) {/* ... */} |
| 105 | fn process_bytes(_: &[u8]) {/* ... */} |
| 106 | ``` |
| 107 | |
| 108 | <br> |
| 109 | |
| 110 | #### License |
| 111 | |
| 112 | <sup> |
| 113 | Licensed under either of <a href="LICENSE-APACHE">Apache License, Version |
| 114 | 2.0</a> or <a href="LICENSE-MIT">MIT license</a> at your option. |
| 115 | </sup> |
| 116 | |
| 117 | <br> |
| 118 | |
| 119 | <sub> |
| 120 | Unless you explicitly state otherwise, any contribution intentionally submitted |
| 121 | for inclusion in this crate by you, as defined in the Apache-2.0 license, shall |
| 122 | be dual licensed as above, without any additional terms or conditions. |
| 123 | </sub> |