Debian Codegraph @

src/lib.rs

raw symbols SCIP
1 //! [![github]](https://github.com/dtolnay/typeid) [![crates-io]](https://crates.io/crates/typeid) [![docs-rs]](https://docs.rs/typeid)
2 //!
3 //! [github]: https://img.shields.io/badge/github-8da0cb?style=for-the-badge&labelColor=555555&logo=github
4 //! [crates-io]: https://img.shields.io/badge/crates.io-fc8d62?style=for-the-badge&labelColor=555555&logo=rust
5 //! [docs-rs]: https://img.shields.io/badge/docs.rs-66c2a5?style=for-the-badge&labelColor=555555&logo=docs.rs
6 //!
7 //! # Const `TypeId` and non-'static `TypeId`
8 //!
9 //! <br>
10 //!
11 //! #### Const `TypeId`
12 //!
13 //! This crate provides [`ConstTypeId`], which is like [`core::any::TypeId`] but
14 //! is constructible in const in stable Rust. (The standard library's TypeId's
15 //! is nightly-only to construct in const; the tracking issue for this is
16 //! [rust#77125].)
17 //!
18 //! [rust#77125]: https://github.com/rust-lang/rust/issues/77125
19 //!
20 //! Being able to construct `ConstTypeId` in const makes it suitable for use
21 //! cases that rely on static promotion:
22 //!
23 //! ```
24 //! use std::fmt::{self, Debug, Display};
25 //! use std::ptr;
26 //! use typeid::ConstTypeId;
27 //!
28 //! pub struct ObjectVTable {
29 //! type_id: ConstTypeId,
30 //! drop_in_place: unsafe fn(*mut ()),
31 //! display: unsafe fn(*const (), &mut fmt::Formatter) -> fmt::Result,
32 //! debug: unsafe fn(*const (), &mut fmt::Formatter) -> fmt::Result,
33 //! }
34 //!
35 //! impl ObjectVTable {
36 //! pub const fn new<T: Display + Debug>() -> &'static Self {
37 //! &ObjectVTable {
38 //! type_id: const { ConstTypeId::of::<T>() },
39 //! drop_in_place: |ptr| unsafe { ptr::drop_in_place(ptr.cast::<T>()) },
40 //! display: |ptr, f| unsafe { Display::fmt(&*ptr.cast::<T>(), f) },
41 //! debug: |ptr, f| unsafe { Debug::fmt(&*ptr.cast::<T>(), f) },
42 //! }
43 //! }
44 //! }
45 //! ```
46 //!
47 //! and in associated constants:
48 //!
49 //! ```
50 //! use typeid::ConstTypeId;
51 //!
52 //! pub trait GetTypeId {
53 //! const TYPEID: ConstTypeId;
54 //! }
55 //!
56 //! impl<T: 'static> GetTypeId for T {
57 //! const TYPEID: ConstTypeId = ConstTypeId::of::<Self>();
58 //! }
59 //! ```
60 //!
61 //! <br>
62 //!
63 //! #### Non-'static `TypeId`
64 //!
65 //! This crate provides [`typeid::of`], which takes an arbitrary non-'static
66 //! type `T` and produces the `TypeId` for the type obtained by replacing all
67 //! lifetimes in `T` by `'static`, other than higher-rank lifetimes found in
68 //! trait objects.
69 //!
70 //! For example if `T` is `&'b dyn for<'a> Trait<'a, 'c>`, then
71 //! `typeid::of::<T>()` produces the TypeId of `&'static dyn for<'a> Trait<'a,
72 //! '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`
76 //! are the same type. However, there is a common special case where this
77 //! behavior is 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`
84 //! are the same type.
85 //!
86 //! ```
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 #![no_std]
109 #![doc(html_root_url = "https://docs.rs/typeid/1.0.3")]
110 #![allow(clippy::doc_markdown, clippy::inline_always)]
111
112 extern crate self as typeidrust-analyzer cargo typeid 1.0.3 typeid/;
113
114 use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::anyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core any/::TypeIdrust-analyzer cargo core https://github.com/rust-lang/rust/library/core any/TypeId#;
115 #[cfg(not(no_const_type_id))]
116 use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/::Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#;
117 #[cfg(not(no_const_type_id))]
118 use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::{selfrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug#};
119 #[cfg(not(no_const_type_id))]
120 use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/::{Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/Hash#, Hasherrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/Hasher#};
121 use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::markerrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/::PhantomDatarust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/PhantomData#;
122 use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/;
123
124 #[cfg(not(no_const_type_id))]
125 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:)]
126 pub struct ConstTypeIdrust-analyzer cargo typeid 1.0.3 ConstTypeId# {
127 type_id_fnrust-analyzer cargo typeid 1.0.3 ConstTypeId#type_id_fn.: fn() -> TypeIdrust-analyzer cargo core https://github.com/rust-lang/rust/library/core any/TypeId#,
128 }
129
130 #[cfg(not(no_const_type_id))]
131 impl ConstTypeIdrust-analyzer cargo typeid 1.0.3 ConstTypeId# {
132 #[must_use]
133 pub const fn ofrust-analyzer cargo typeid 1.0.3 impl#[ConstTypeId]of().<T>() -> Selfrust-analyzer cargo typeid 1.0.3 impl#[ConstTypeId]
134 where
135 T: ?Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#,
136 {
137 ConstTypeIdrust-analyzer cargo typeid 1.0.3 ConstTypeId# {
138 type_id_fnrust-analyzer cargo typeid 1.0.3 ConstTypeId#type_id_fn.: typeidrust-analyzer cargo typeid 1.0.3 crate/::ofrust-analyzer cargo typeid 1.0.3 of().::<T>,
139 }
140 }
141
142 #[inline]
143 fn getrust-analyzer cargo typeid 1.0.3 impl#[ConstTypeId]get().(self) -> TypeIdrust-analyzer cargo core https://github.com/rust-lang/rust/library/core any/TypeId# {
144 (self.type_id_fnrust-analyzer cargo typeid 1.0.3 ConstTypeId#type_id_fn.)()
145 }
146 }
147
148 #[cfg(not(no_const_type_id))]
149 impl Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug# for ConstTypeIdrust-analyzer cargo typeid 1.0.3 ConstTypeId# {
150 fn fmtrust-analyzer cargo typeid 1.0.3 impl#[ConstTypeId][Debug]fmt().(&self, formatter: &mut fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#) -> fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# {
151 Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug#::fmtrust-analyzer cargo core https://github.com/rust-lang/rust/library/core any/impl#[TypeId][Debug]fmt().(&self.getrust-analyzer cargo typeid 1.0.3 impl#[ConstTypeId]get().(), formatter)
152 }
153 }
154
155 #[cfg(not(no_const_type_id))]
156 impl PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq# for ConstTypeIdrust-analyzer cargo typeid 1.0.3 ConstTypeId# {
157 #[inline]
158 fn eqrust-analyzer cargo typeid 1.0.3 impl#[ConstTypeId][`PartialEq<Self>`]eq().(&self, other: &Selfrust-analyzer cargo typeid 1.0.3 impl#[ConstTypeId][`PartialEq<Self>`]) -> bool {
159 self.getrust-analyzer cargo typeid 1.0.3 impl#[ConstTypeId]get().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core any/impl#[TypeId][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core any/impl#[TypeId][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core any/impl#[TypeId][`PartialEq<Self>`]eq().other.getrust-analyzer cargo typeid 1.0.3 impl#[ConstTypeId]get().()
160 }
161 }
162
163 #[cfg(not(no_const_type_id))]
164 impl PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#<TypeIdrust-analyzer cargo core https://github.com/rust-lang/rust/library/core any/TypeId#> for ConstTypeIdrust-analyzer cargo typeid 1.0.3 ConstTypeId# {
165 fn eqrust-analyzer cargo typeid 1.0.3 impl#[ConstTypeId][`PartialEq<TypeId>`]eq().(&self, other: &TypeIdrust-analyzer cargo core https://github.com/rust-lang/rust/library/core any/TypeId#) -> bool {
166 self.getrust-analyzer cargo typeid 1.0.3 impl#[ConstTypeId]get().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core any/impl#[TypeId][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core any/impl#[TypeId][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core any/impl#[TypeId][`PartialEq<Self>`]eq().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().other
167 }
168 }
169
170 #[cfg(not(no_const_type_id))]
171 impl Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq# for ConstTypeIdrust-analyzer cargo typeid 1.0.3 ConstTypeId# {}
172
173 #[cfg(not(no_const_type_id))]
174 impl PartialOrdrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd# for ConstTypeIdrust-analyzer cargo typeid 1.0.3 ConstTypeId# {
175 #[inline]
176 fn partial_cmprust-analyzer cargo typeid 1.0.3 impl#[ConstTypeId][`PartialOrd<Self>`]partial_cmp().(&self, other: &Selfrust-analyzer cargo typeid 1.0.3 impl#[ConstTypeId][`PartialOrd<Self>`]) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering#> {
177 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Ordrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord#::cmprust-analyzer cargo typeid 1.0.3 impl#[ConstTypeId][Ord]cmp().(self, other))
178 }
179 }
180
181 #[cfg(not(no_const_type_id))]
182 impl Ordrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord# for ConstTypeIdrust-analyzer cargo typeid 1.0.3 ConstTypeId# {
183 #[inline]
184 fn cmprust-analyzer cargo typeid 1.0.3 impl#[ConstTypeId][Ord]cmp().(&self, other: &Selfrust-analyzer cargo typeid 1.0.3 impl#[ConstTypeId][Ord]) -> Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ordering# {
185 Ordrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord#::cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core any/impl#[TypeId][Ord]cmp().(&self.getrust-analyzer cargo typeid 1.0.3 impl#[ConstTypeId]get().(), &other.getrust-analyzer cargo typeid 1.0.3 impl#[ConstTypeId]get().())
186 }
187 }
188
189 #[cfg(not(no_const_type_id))]
190 impl Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/Hash# for ConstTypeIdrust-analyzer cargo typeid 1.0.3 ConstTypeId# {
191 fn hashrust-analyzer cargo typeid 1.0.3 impl#[ConstTypeId][Hash]hash().<H: Hasherrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/Hasher#>(&self, state: &mut H) {
192 self.getrust-analyzer cargo typeid 1.0.3 impl#[ConstTypeId]get().().hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core any/impl#[TypeId][Hash]hash().(state);
193 }
194 }
195
196 #[must_use]
197 #[inline(always)]
198 pub fn ofrust-analyzer cargo typeid 1.0.3 of().<T>() -> TypeIdrust-analyzer cargo core https://github.com/rust-lang/rust/library/core any/TypeId#
199 where
200 T: ?Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#,
201 {
202 trait NonStaticAnyrust-analyzer cargo typeid 1.0.3 NonStaticAny# {
203 fn get_type_idrust-analyzer cargo typeid 1.0.3 NonStaticAny#get_type_id().(&self) -> TypeIdrust-analyzer cargo core https://github.com/rust-lang/rust/library/core any/TypeId#
204 where
205 Self: 'static;
206 }
207
208 impl<T: ?Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized#> NonStaticAnyrust-analyzer cargo typeid 1.0.3 NonStaticAny# for PhantomDatarust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/PhantomData#<T> {
209 #[inline(always)]
210 fn get_type_idrust-analyzer cargo typeid 1.0.3 impl#[`PhantomData<T>`][NonStaticAny]get_type_id().(&self) -> TypeIdrust-analyzer cargo core https://github.com/rust-lang/rust/library/core any/TypeId#
211 where
212 Selfrust-analyzer cargo typeid 1.0.3 impl#[`PhantomData<T>`][NonStaticAny]: 'static,
213 {
214 TypeIdrust-analyzer cargo core https://github.com/rust-lang/rust/library/core any/TypeId#::ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core any/impl#[TypeId]of().::<T>()
215 }
216 }
217
218 let phantom_data = PhantomDatarust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/PhantomData#::<T>;
219 NonStaticAnyrust-analyzer cargo typeid 1.0.3 NonStaticAny#::get_type_idrust-analyzer cargo typeid 1.0.3 NonStaticAny#get_type_id().(unsafe {
220 memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::transmuterust-analyzer cargo core https://github.com/rust-lang/rust/library/core intrinsics/transmute().::<&dyn NonStaticAnyrust-analyzer cargo typeid 1.0.3 NonStaticAny#, &(dyn NonStaticAnyrust-analyzer cargo typeid 1.0.3 NonStaticAny# + 'static)>(&phantom_data)
221 })
222 }