pub struct TypedGeneration<T: TypedGenerationKind> { /* private fields */ }Expand description
A generation number with type-level information about what it’s used for.
For more, see the library documentation.
Implementations§
Source§impl<T: TypedGenerationKind> TypedGeneration<T>
impl<T: TypedGenerationKind> TypedGeneration<T>
Sourcepub const ZERO: Self
pub const ZERO: Self
The generation number before any change has been applied, 0.
Generation numbers conventionally start at 1 (see Self::new). Zero is
reserved to represent an initial or empty state that precedes the first
generation.
Sourcepub const fn new() -> Self
pub const fn new() -> Self
Creates the first generation number of this type, which is 1.
There is deliberately no Default impl: whether a value should start at
Self::ZERO or at the first generation is a choice callers must make
explicitly.
Sourcepub const fn from_u32(value: u32) -> Self
pub const fn from_u32(value: u32) -> Self
Creates a generation number of this type from a u32.
Every u32 is a valid generation number, so this conversion is infallible.
Sourcepub const fn next(&self) -> Self
pub const fn next(&self) -> Self
Returns the next generation number.
§Panics
Panics if the next generation number would exceed Self::MAX. Use
Self::checked_next to handle overflow instead.
Sourcepub const fn checked_next(&self) -> Option<Self>
pub const fn checked_next(&self) -> Option<Self>
Returns the next generation number, or None if it would exceed Self::MAX.
Sourcepub const fn prev(&self) -> Option<Self>
pub const fn prev(&self) -> Option<Self>
Returns the previous generation number, or None if this is the first generation.
Both 1 and Self::ZERO are treated as having no predecessor: prev never
returns Self::ZERO.
Sourcepub const fn as_i64(self) -> i64
pub const fn as_i64(self) -> i64
Returns the generation number as an i64.
This conversion is infallible because generation numbers are restricted to 2**63 - 1.
Sourcepub const fn upcast<U: TypedGenerationKind>(self) -> TypedGeneration<U>where
T: Into<U>,
pub const fn upcast<U: TypedGenerationKind>(self) -> TypedGeneration<U>where
T: Into<U>,
Converts the generation number to one with looser semantics.
By default, generation kinds are considered independent, and conversions
between them must happen via the GenericGeneration interface. But in
some cases, there may be a relationship between two different generation
kinds, and you may wish to easily convert generation numbers from one
kind to another.
Typically, a conversion from TypedGeneration<T> to TypedGeneration<U>
is most useful when T’s semantics are a superset of U’s, or in other
words, when every TypedGeneration<T> is logically also a
TypedGeneration<U>.
For instance:
- Imagine you have
TypedGenerationKinds for different types of database connections, whereDbConnKindis the general type andPgConnKindis a specific kind for Postgres. - Since every Postgres connection is also a database connection,
a cast from
TypedGeneration<PgConnKind>toTypedGeneration<DbConnKind>makes sense. - The inverse cast would not make sense, as a database connection may not necessarily be a Postgres connection.
This interface provides an alternative, safer way to perform this
conversion. Indicate your intention to allow a conversion between kinds
by implementing From<T> for U, as shown in the example below.
§Examples
use oxide_generation::{TypedGeneration, TypedGenerationKind, TypedGenerationTag};
// Let's say that these generation numbers track repositories for
// different version control systems, such that you have a generic
// RepoKind:
pub enum RepoKind {}
impl TypedGenerationKind for RepoKind {
const TAG: TypedGenerationTag = TypedGenerationTag::new("repo");
}
// You also have more specific kinds:
pub enum GitRepoKind {}
impl TypedGenerationKind for GitRepoKind {
const TAG: TypedGenerationTag = TypedGenerationTag::new("git_repo");
}
// (and HgRepoKind, JujutsuRepoKind, etc...)
// First, define a `From` impl. This impl indicates your desire
// to convert from one kind to another.
impl From<GitRepoKind> for RepoKind {
fn from(value: GitRepoKind) -> Self {
match value {}
}
}
// Now you can convert between them:
let git_generation: TypedGeneration<GitRepoKind> = TypedGeneration::from_u32(5);
let repo_generation: TypedGeneration<RepoKind> = git_generation.upcast();Trait Implementations§
Source§impl<T> Arbitrary for TypedGeneration<T>where
T: TypedGenerationKind,
Available on crate feature proptest1 only.Generates random TypedGeneration<T> instances.
impl<T> Arbitrary for TypedGeneration<T>where
T: TypedGenerationKind,
proptest1 only.Generates random TypedGeneration<T> instances.
Values are drawn uniformly from the range 0..=TypedGeneration::MAX.
Source§type Parameters = TypedGenerationParams
type Parameters = TypedGenerationParams
arbitrary_with accepts for configuration
of the generated Strategy. Parameters must implement Default.Source§type Strategy = BoxedStrategy<TypedGeneration<T>>
type Strategy = BoxedStrategy<TypedGeneration<T>>
Strategy used to generate values of type Self.Source§fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy
fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy
Source§impl<T: TypedGenerationKind> Clone for TypedGeneration<T>
impl<T: TypedGenerationKind> Clone for TypedGeneration<T>
impl<T: TypedGenerationKind> Copy for TypedGeneration<T>
Source§impl<T: TypedGenerationKind> Debug for TypedGeneration<T>
impl<T: TypedGenerationKind> Debug for TypedGeneration<T>
Source§impl<'de, T: TypedGenerationKind> Deserialize<'de> for TypedGeneration<T>
Available on crate feature serde only.Deserializes a TypedGeneration<T> using the same format as Generation.
impl<'de, T: TypedGenerationKind> Deserialize<'de> for TypedGeneration<T>
serde only.Deserializes a TypedGeneration<T> using the same format as Generation.
This impl does not require T to implement Deserialize.
Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Source§impl<T: TypedGenerationKind> Diffable for TypedGeneration<T>
Available on crate feature daft01 only.Diffs a TypedGeneration<T> as a leaf value.
impl<T: TypedGenerationKind> Diffable for TypedGeneration<T>
daft01 only.Diffs a TypedGeneration<T> as a leaf value.
This impl does not require T to implement Diffable.
Source§impl<T: TypedGenerationKind> Display for TypedGeneration<T>
impl<T: TypedGenerationKind> Display for TypedGeneration<T>
impl<T: TypedGenerationKind> Eq for TypedGeneration<T>
Source§impl<T: TypedGenerationKind> From<&TypedGeneration<T>> for i64
impl<T: TypedGenerationKind> From<&TypedGeneration<T>> for i64
Source§fn from(g: &TypedGeneration<T>) -> Self
fn from(g: &TypedGeneration<T>) -> Self
Source§impl<T: TypedGenerationKind> From<TypedGeneration<T>> for u64
impl<T: TypedGenerationKind> From<TypedGeneration<T>> for u64
Source§fn from(g: TypedGeneration<T>) -> Self
fn from(g: TypedGeneration<T>) -> Self
Source§impl<T: TypedGenerationKind> From<TypedGeneration<T>> for i64
impl<T: TypedGenerationKind> From<TypedGeneration<T>> for i64
Source§fn from(g: TypedGeneration<T>) -> Self
fn from(g: TypedGeneration<T>) -> Self
Source§impl<T: TypedGenerationKind> From<u32> for TypedGeneration<T>
impl<T: TypedGenerationKind> From<u32> for TypedGeneration<T>
Source§impl<T: TypedGenerationKind> FromStr for TypedGeneration<T>
impl<T: TypedGenerationKind> FromStr for TypedGeneration<T>
Source§impl<T: TypedGenerationKind> GenericGeneration for TypedGeneration<T>
impl<T: TypedGenerationKind> GenericGeneration for TypedGeneration<T>
Source§fn from_untyped_generation(generation: Generation) -> Self
fn from_untyped_generation(generation: Generation) -> Self
Self from an untyped Generation.Source§fn into_untyped_generation(self) -> Generation
fn into_untyped_generation(self) -> Generation
self into an untyped Generation.Source§fn as_untyped_generation(&self) -> &Generation
fn as_untyped_generation(&self) -> &Generation
Generation. Read moreSource§impl<T: TypedGenerationKind> Hash for TypedGeneration<T>
impl<T: TypedGenerationKind> Hash for TypedGeneration<T>
Source§impl<T> JsonSchema for TypedGeneration<T>where
T: TypedGenerationKind + JsonSchema,
Available on crate feature schemars08 only.Implements JsonSchema for TypedGeneration<T>, if T implements JsonSchema.
impl<T> JsonSchema for TypedGeneration<T>where
T: TypedGenerationKind + JsonSchema,
schemars08 only.Implements JsonSchema for TypedGeneration<T>, if T implements JsonSchema.
schema_nameis set to"TypedGenerationFor", concatenated by the schema name ofT.schema_idis set toformat!("oxide_generation::TypedGeneration<{}>", T::schema_id()).json_schemais the same as the one forGeneration(including its description), with thex-rust-typeextension to allow automatic replacement in typify and progenitor.
Source§fn schema_name() -> String
fn schema_name() -> String
Source§fn schema_id() -> Cow<'static, str>
fn schema_id() -> Cow<'static, str>
Source§fn json_schema(generator: &mut SchemaGenerator) -> Schema
fn json_schema(generator: &mut SchemaGenerator) -> Schema
Source§fn is_referenceable() -> bool
fn is_referenceable() -> bool
$ref keyword. Read moreSource§impl<T: TypedGenerationKind> Ord for TypedGeneration<T>
impl<T: TypedGenerationKind> Ord for TypedGeneration<T>
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl<T: TypedGenerationKind> PartialEq for TypedGeneration<T>
impl<T: TypedGenerationKind> PartialEq for TypedGeneration<T>
Source§impl<T: TypedGenerationKind> PartialOrd for TypedGeneration<T>
impl<T: TypedGenerationKind> PartialOrd for TypedGeneration<T>
Source§impl<T: TypedGenerationKind> Serialize for TypedGeneration<T>
Available on crate feature serde only.Serializes a TypedGeneration<T> using the same format as Generation.
impl<T: TypedGenerationKind> Serialize for TypedGeneration<T>
serde only.Serializes a TypedGeneration<T> using the same format as Generation.
This impl does not require T to implement Serialize.
Source§impl<T: TypedGenerationKind> TryFrom<i64> for TypedGeneration<T>
impl<T: TypedGenerationKind> TryFrom<i64> for TypedGeneration<T>
Source§impl<T: TypedGenerationKind> TryFrom<u64> for TypedGeneration<T>
impl<T: TypedGenerationKind> TryFrom<u64> for TypedGeneration<T>
Source§impl<T: TypedGenerationKind> Value for TypedGeneration<T>
Available on crate feature slog2 only.Logs a TypedGeneration<T> as an integer, using the same format as Generation.
impl<T: TypedGenerationKind> Value for TypedGeneration<T>
slog2 only.Logs a TypedGeneration<T> as an integer, using the same format as Generation.
This impl does not require T to implement slog::Value.