Struct zng_app_context::AppId
source · pub struct AppId(/* private fields */);
Expand description
Identifies an app instance.
§Memory
The internal number is a NonZeroU32
, that means that
Option<AppId>
and
AppId
are the same size as u32
.
§As Hash
The generated internal number has good statistical distribution and can be used as its own hash,
although it is not cryptographically safe, as it is simply a sequential counter scrambled using a modified
splitmix64
.
§Static
The unique ID cannot be generated at compile time, but you can use the new_static
constructor to
create a lightweight lazy ID factory that will generate the ID on the first get.
Implementations§
source§impl AppId
impl AppId
sourcepub fn new_unique() -> Self
pub fn new_unique() -> Self
Generates a new unique ID.
sourcepub const fn new_static() -> StaticAppId
pub const fn new_static() -> StaticAppId
New static ID that will be generated on the first get.
sourcepub fn sequential(self) -> u32
pub fn sequential(self) -> u32
Un-scramble the underlying value to get the original sequential count number.
If two IDs, id0
and id1
where generated by the same thread then id0.sequential() < id1.sequential()
.
sourcepub fn from_raw(raw: u32) -> Self
pub fn from_raw(raw: u32) -> Self
Creates an ID from a raw value.
The value must not be zero, panics if it is, the value must have been provided by get
otherwise
the ID will not be unique.
sourcepub fn from_sequential(num: u32) -> Self
pub fn from_sequential(num: u32) -> Self
Creates an ID from a sequential
number.
§Safety
The value must not be zero, panics if it is, the value must have been provided by sequential
otherwise
the ID will not be unique.
source§impl AppId
impl AppId
sourcepub fn named(name: impl Into<Txt>) -> Self
pub fn named(name: impl Into<Txt>) -> Self
Get or generate an ID with associated name.
If the name
is already associated with an ID, returns it.
If the name
is new, generates a new ID and associated it with the name.
If name
is an empty string just returns a new ID.
sourcepub fn debug_named(name: impl Into<Txt>) -> Self
pub fn debug_named(name: impl Into<Txt>) -> Self
Calls named
in a debug build and new_unique
in a release build.
The named
function causes a hash-map lookup, but if you are only naming a widget to find
it in the Inspector you don’t need that lookup in a release build, so you can set the id
to this function call instead.
sourcepub fn named_new(name: impl Into<Txt>) -> Result<Self, IdNameError<Self>>
pub fn named_new(name: impl Into<Txt>) -> Result<Self, IdNameError<Self>>
Generate a new ID with associated name.
If the name
is already associated with an ID, returns the NameUsed
error.
If the name
is an empty string just returns a new ID.
sourcepub fn set_name(self, name: impl Into<Txt>) -> Result<(), IdNameError<Self>>
pub fn set_name(self, name: impl Into<Txt>) -> Result<(), IdNameError<Self>>
Associate a name
with the ID, if it is not named.
If the name
is already associated with a different ID, returns the NameUsed
error.
If the ID is already named, with a name different from name
, returns the AlreadyNamed
error.
If the name
is an empty string or already is the name of the ID, does nothing.