Skip to main content

TypedGeneration

Struct TypedGeneration 

Source
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>

Source

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.

Source

pub const MAX: Self

The largest possible generation number, 2**63 - 1.

Source

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.

Source

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.

Source

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.

Source

pub const fn checked_next(&self) -> Option<Self>

Returns the next generation number, or None if it would exceed Self::MAX.

Source

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.

Source

pub const fn as_u64(self) -> u64

Returns the generation number as a u64.

Source

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.

Source

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, where DbConnKind is the general type and PgConnKind is a specific kind for Postgres.
  • Since every Postgres connection is also a database connection, a cast from TypedGeneration<PgConnKind> to TypedGeneration<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>

Available on crate feature proptest1 only.

Generates random TypedGeneration<T> instances.

Values are drawn uniformly from the range 0..=TypedGeneration::MAX.

Source§

type Parameters = TypedGenerationParams

The type of parameters that arbitrary_with accepts for configuration of the generated Strategy. Parameters must implement Default.
Source§

type Strategy = BoxedStrategy<TypedGeneration<T>>

The type of Strategy used to generate values of type Self.
Source§

fn arbitrary_with(_args: Self::Parameters) -> Self::Strategy

Generates a Strategy for producing arbitrary values of type the implementing type (Self). The strategy is passed the arguments given in args. Read more
Source§

fn arbitrary() -> Self::Strategy

Generates a Strategy for producing arbitrary values of type the implementing type (Self). Read more
Source§

impl<T: TypedGenerationKind> Clone for TypedGeneration<T>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: TypedGenerationKind> Copy for TypedGeneration<T>

Source§

impl<T: TypedGenerationKind> Debug for TypedGeneration<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
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.

This impl does not require T to implement Deserialize.

Source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<T: TypedGenerationKind> Diffable for TypedGeneration<T>

Available on crate feature daft01 only.

Diffs a TypedGeneration<T> as a leaf value.

This impl does not require T to implement Diffable.

Source§

type Diff<'daft> = Leaf<&'daft TypedGeneration<T>>

The type of the diff. Read more
Source§

fn diff<'daft>(&'daft self, other: &'daft Self) -> Self::Diff<'daft>

Compute the diff between two values.
Source§

impl<T: TypedGenerationKind> Display for TypedGeneration<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: TypedGenerationKind> Eq for TypedGeneration<T>

Source§

impl<T: TypedGenerationKind> From<&TypedGeneration<T>> for i64

Source§

fn from(g: &TypedGeneration<T>) -> Self

Converts to this type from the input type.
Source§

impl<T: TypedGenerationKind> From<TypedGeneration<T>> for u64

Source§

fn from(g: TypedGeneration<T>) -> Self

Converts to this type from the input type.
Source§

impl<T: TypedGenerationKind> From<TypedGeneration<T>> for i64

Source§

fn from(g: TypedGeneration<T>) -> Self

Converts to this type from the input type.
Source§

impl<T: TypedGenerationKind> From<u32> for TypedGeneration<T>

Source§

fn from(value: u32) -> Self

Converts to this type from the input type.
Source§

impl<T: TypedGenerationKind> FromStr for TypedGeneration<T>

Source§

type Err = ParseError

The associated error which can be returned from parsing.
Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl<T: TypedGenerationKind> GenericGeneration for TypedGeneration<T>

Source§

fn from_untyped_generation(generation: Generation) -> Self

Creates a new instance of Self from an untyped Generation.
Source§

fn into_untyped_generation(self) -> Generation

Converts self into an untyped Generation.
Source§

fn as_untyped_generation(&self) -> &Generation

Returns the inner Generation. Read more
Source§

impl<T: TypedGenerationKind> Hash for TypedGeneration<T>

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T> JsonSchema for TypedGeneration<T>

Available on crate feature schemars08 only.

Implements JsonSchema for TypedGeneration<T>, if T implements JsonSchema.

  • schema_name is set to "TypedGenerationFor", concatenated by the schema name of T.
  • schema_id is set to format!("oxide_generation::TypedGeneration<{}>", T::schema_id()).
  • json_schema is the same as the one for Generation (including its description), with the x-rust-type extension to allow automatic replacement in typify and progenitor.
Source§

fn schema_name() -> String

The name of the generated JSON Schema. Read more
Source§

fn schema_id() -> Cow<'static, str>

Returns a string that uniquely identifies the schema produced by this type. Read more
Source§

fn json_schema(generator: &mut SchemaGenerator) -> Schema

Generates a JSON Schema for this type. Read more
Source§

fn is_referenceable() -> bool

Whether JSON Schemas generated for this type should be re-used where possible using the $ref keyword. Read more
Source§

impl<T: TypedGenerationKind> Ord for TypedGeneration<T>

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<T: TypedGenerationKind> PartialEq for TypedGeneration<T>

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl<T: TypedGenerationKind> PartialOrd for TypedGeneration<T>

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T: TypedGenerationKind> Serialize for TypedGeneration<T>

Available on crate feature serde only.

Serializes a TypedGeneration<T> using the same format as Generation.

This impl does not require T to implement Serialize.

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<T: TypedGenerationKind> TryFrom<i64> for TypedGeneration<T>

Source§

type Error = GenerationNegativeError

The type returned in the event of a conversion error.
Source§

fn try_from(value: i64) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl<T: TypedGenerationKind> TryFrom<u64> for TypedGeneration<T>

Source§

type Error = GenerationOverflowError

The type returned in the event of a conversion error.
Source§

fn try_from(value: u64) -> Result<Self, Self::Error>

Performs the conversion.
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.

This impl does not require T to implement slog::Value.

Source§

fn serialize( &self, _rec: &Record<'_>, key: Key, serializer: &mut dyn Serializer, ) -> Result

Serialize self into Serializer Read more

Auto Trait Implementations§

§

impl<T> Freeze for TypedGeneration<T>

§

impl<T> RefUnwindSafe for TypedGeneration<T>
where T: RefUnwindSafe,

§

impl<T> Send for TypedGeneration<T>

§

impl<T> Sync for TypedGeneration<T>

§

impl<T> Unpin for TypedGeneration<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for TypedGeneration<T>

§

impl<T> UnwindSafe for TypedGeneration<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> SendSyncUnwindSafe for T
where T: Send + Sync + UnwindSafe + ?Sized,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V