Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

A procedural macro for generating unique ids for rust types

License

NotificationsYou must be signed in to change notification settings

iddm/unique-type-id

Repository files navigation

A rust procedural macro crate for generating unique id for the rust types.

Crates badgeCIDocumentationMIT licensedGitHub Sponsors

What does it do?

It simply implements a trait for the type where is only one method -id() -> TypeId which returns a unique positive number. For id generation, the procedural macro reads the file called "types.toml" and searches for the type namethere. You may also specify another file name if you want by usingUniqueTypeIdFile attribute. Speaking more detailed:

  1. The procedural macro reads the attributes on a type.
  2. If there are no attributes, it usestypes.toml file name as types file name, otherwise uses specified one.
  3. For each type the macro is used it tries to find the type name in the types file. If it can find it, it returnsit's id, otherwise it returns the available id. Reading tests helps in understanding this.

Usage

  1. Addunique-type-id as dependency in yourCargo.toml:
[dependencies]unique-type-id ="1"
  1. Create a struct or enum and use the trait:
#[test]fnunique_simple(){use unique_type_id::UniqueTypeId;#[derive(UniqueTypeId)]structTest1;#[derive(UniqueTypeId)]structTest2;assert_eq!(Test1::id().0,1u64);assert_eq!(Test2::id().0,2u64);}

This will generate a types file if it has not been created yet and put there ids, starting with0,for each type which was not found there. This is how it looks when you have predefined set of idsfor your types:

#[test]fnunique_different_file(){use unique_type_id::UniqueTypeId;#[derive(UniqueTypeId)]#[UniqueTypeIdFile ="types2.toml"]structTest1;#[derive(UniqueTypeId)]#[UniqueTypeIdFile ="types2.toml"]structTest2;assert_eq!(Test1::id().0,115u64);assert_eq!(Test2::id().0,232u64);}

Here we set up ids for our types manually by creating thetypes2.toml file.

Options

  • UniqueTypeIdFile - allows to specify the file name to write/read the IDs from.
  • UniqueTypeIdType - allows to change the ID number type fromu64 (the default) to theuser-preferred one.
  • UniqueTypeIdStart - allows to set the starting ID number for the type. Can be used if thetype layout file is very well-known and guaranteed to avoid collisions.

UniqueTypeIdFile

#[derive(UniqueTypeId)]#[UniqueTypeIdFile ="types2.toml"]structTest1;

UniqueTypeIdType

#[derive(UniqueTypeId)]#[UniqueTypeIdType ="i16"]structTest;

UniqueTypeIdStart

#[derive(UniqueTypeId)]#[UniqueTypeIdStart ="23"]structTest;

Note

Default and custom type files are searched relatively to a directory wherecargo build is called.

License

This project islicensed under the MIT license.

About

A procedural macro for generating unique ids for rust types

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

 

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp