Crates.io | unique-type-id |
lib.rs | unique-type-id |
version | 1.3.0 |
source | src |
created_at | 2017-12-09 08:01:33.37106 |
updated_at | 2023-11-29 09:11:16.631453 |
description | A unique id proc-macro generator for types. |
homepage | |
repository | https://github.com/vityafx/unique-type-id |
max_upload_size | |
id | 42225 |
size | 5,959 |
A rust procedural macro crate for generating unique id for the rust types.
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 name
there. You may also specify another file name if you want by using UniqueTypeIdFile
attribute. Speaking more detailed:
types.toml
file name as types file name, otherwise uses specified one.unique-type-id
as dependency in your Cargo.toml
:[dependencies]
unique-type-id = "1"
#[test]
fn unique_simple() {
use unique_type_id::UniqueTypeId;
#[derive(UniqueTypeId)]
struct Test1;
#[derive(UniqueTypeId)]
struct Test2;
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 with 0
,
for each type which was not found there. This is how it looks when you have predefined set of ids
for your types:
#[test]
fn unique_different_file() {
use unique_type_id::UniqueTypeId;
#[derive(UniqueTypeId)]
#[UniqueTypeIdFile = "types2.toml"]
struct Test1;
#[derive(UniqueTypeId)]
#[UniqueTypeIdFile = "types2.toml"]
struct Test2;
assert_eq!(Test1::id().0, 115u64);
assert_eq!(Test2::id().0, 232u64);
}
Here we set up ids for our types manually by creating the types2.toml
file.
UniqueTypeIdFile
- allows to specify the file name to write/read the IDs from.UniqueTypeIdType
- allows to change the ID number type from u64
(the default) to the
user-preferred one.UniqueTypeIdStart
- allows to set the starting ID number for the type. Can be used if the
type layout file is very well-known and guaranteed to avoid collisions.#[derive(UniqueTypeId)]
#[UniqueTypeIdFile = "types2.toml"]
struct Test1;
#[derive(UniqueTypeId)]
#[UniqueTypeIdType = "i16"]
struct Test;
#[derive(UniqueTypeId)]
#[UniqueTypeIdStart = "23"]
struct Test;
Default and custom type files are searched relatively to a directory where cargo build
is called.
This project is licensed under the MIT license.