🏷️ Moonshine Tag
Cheap, fast, mostly unique identifiers designed for Bevy.
Overview
A Tag represents a cheap, generic, somewhat unique identifier which may be used to associate "things" with each other or to dynamically flag entities.
use *;
use ;
tags!
let mut world = new;
// Define some fruits!
let fruits = ;
// Only crunchy, edible apples, please! :)
let filter: Filter = filter! & filter!;
for fruit in &fruits
# assert!;
Features
- Tags are cheap to create, cheap to copy, cheap to compare and "unique enough". It's just a
u64. - Serialization support for both tags and tag filters
- Ability to define complex tag filter expressions
- Simple implementation with no boilerplate and no procedural macros 🧘
Usage
Tags
You may define Tags from any arbitrary string:
use *;
tags! ; // Convenient macro
const A1: Tag = new; // Manual constant
let a2 = new; // Runtime
assert_eq!;
assert_eq!;
Any two tags with the same name are considered equal.
Tags is a specialized collection for managing sets of tags:
use *;
tags!
let a = from;
let c = from;
let ab = from;
let ac = a.union;
Tags may be used as a Component or on its own as a generic collection of tags.
Tag Filters
A tag Filter is used to test if a given Tags set matches a certain pattern:
use ;
tags!
let a = from;
let c = from;
let a_or_b: Filter = any_of;
assert!;
assert!;
Tag filters may be combined which each other to create complex expressions:
use ;
tags!
let ab = from;
let cd = from;
let c = from;
let filter = & any_of;
assert!;
assert!;
assert!;
There is also a convenient filter! macro for constructing tag filters from tag expressions:
use ;
tags!
let _: Filter = filter!; // Matches any tag set containing A or B
let _: Filter = filter!; // Matches any tag set which contains A or B, or exactly C
let _: Filter = filter!; // Matches any tag set not containing C
⚠️ This macro is still in development.
Limitations and Guidelines
Internally, tags are just an FNV-1a (Why?) hash of their string representation. This makes them very cheap to use, but this means they are NOT guaranteed to be unique.
It is the assumption of this library that in most game application domains, this is a minor and unlikely problem.
In most applications, the chance of collision between two different tags within the same subsystem is very low, non-fatal, and easily correctable (just rename one of the tags!).
However, you should NOT use tags for any cryptographic purposes, or as globally unique identifiers.
Instead, prefer to use them for convenient, dynamic pattern matching or flagging "things" within your systems, especially entities.
Installation
Add the following to your Cargo.toml:
[]
= "0.2.0"
This crate is also included as part of 🍸 Moonshine Core.
Support
Please post an issue for any bugs, questions, or suggestions.
You may also contact me on the official Bevy Discord server as @Zeenobit.