Skip to content

AngelicosPhosphoros/small_type_id

Repository files navigation

Crates.io Version tests docs.rs

Small Type Id

This crate provides trait HasTypeId with associated constant TYPE_ID and derive macro to implement it for your types.

There are 4 guarantees:

  1. TYPE_ID is a constant.
  2. Size is 32 bit.
  3. TypeId cannot be zero which allows niche optimizations (size_of::<Option<TypeId>>() is 4 bytes).
  4. Most significant bit (MSB) is guaranteed to be zero:
    • Allows users to use this bit to distinguish with some other kind of id in a union (e.g. Runtime types from some scripting engine).
    • Does not allow niche optimizations on current version of Rust yet.

Those guarantees would never be removed (even in semver breaking releases) so you can update dependency on this crate without validating your code that rely on that guarantees.

Uniqueness of TypeIds is enforced by running code before fn main() using ctor crate.

Comparison with std::any::TypeId

With std::any::TypeId as at Rust 1.87.

Advantages

  1. TYPE_ID is a constant.
  2. Size of small_type_id::TypeId is guaranteed to be 32 bits.
  3. Size of small_type_id::TypeId is significantly smaller(4 vs 16 bytes), allowing better performance due to less usage of CPU cache.
  4. small_type_id::TypeId supports niche optimization for Option<small_type_id::TypeId>.
  5. small_type_id::TypeId guarantees that MSB is zero, allowing creating 32 bit identifiers by users using unions:
    • Since user types would need to set MSB to 1, resulting value is still cannot be zero, allowing niche optimization.

Downsides

  1. small_type_id::HasTypeId needs to be derived for supported types, it doesn't work automatically.
  2. small_type_id::HasTypeId doesn't support generic types.

Comparison with typeid::ConstTypeId

With typeid version 1.0.3

Advantages

  1. Has smaller size (32 bit vs 64 bit on 64-bit targets).
  2. Has defined internal representation that can be utilized by users.

Disadvantages

  1. Doesn't support every type.

How it works and how to use API.

Example:

use small_type_id::HasTypeId as _;

#[derive(small_type_id::HasTypeId)]
pub struct Struct {
    pub a: u32,
    pub b: usize,
}

#[derive(small_type_id::HasTypeId)]
pub enum Enum {
    A(u32), B(String)
}

// Check that they are different:
assert_ne!(Struct::TYPE_ID, Enum::TYPE_ID);
// Or even in compile time:
const { assert!(Struct::TYPE_ID.as_u32() != Enum::TYPE_ID.as_u32()); };

More examples and implementation explanation are available in documentation.

Safety

Uniqueness is tested before running fn main() (unless opt-out), so code can rely on type ids being unique. Code is tested in CI on following platforms:

  1. macos
  2. Windows
  3. Linux with the following libc implementations:

There is also some testing with Address-Sanitizer.

Acknowledgments

  • Thanks to mmastrac for crate ctor used for implementing this crate.
  • Thanks to dtolnay for crate linkme which helped to learn me about using link sections for gathering statics.
  • Thanks to Raymond Chen for his explanations about linker sections.

About

No description, website, or topics provided.

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Packages

No packages published