pub enum Auto<T> {
Fixed(T),
Auto,
}Available on crate feature
db only.Expand description
A wrapper over a value that can be either a fixed value or be automatically generated by the database.
This is primarily used for auto-incrementing primary keys.
§Examples
use cot::db::{Auto, Model, model};
#[model]
struct MyModel {
#[model(primary_key)]
id: Auto<i32>,
}
let database = Database::new("sqlite::memory:").await?;
let mut my_model = MyModel { id: Auto::auto() };
my_model.save(&database).await?;
assert!(matches!(my_model.id, Auto::Fixed(_)));
Variants§
Implementations§
Source§impl<T> Auto<T>
impl<T> Auto<T>
Sourcepub const fn auto() -> Self
pub const fn auto() -> Self
Creates a new Auto instance that is automatically generated by the
database.
§Examples
use cot::db::Auto;
let auto = Auto::<i32>::auto();
assert!(matches!(auto, Auto::Auto));Sourcepub const fn fixed(value: T) -> Self
pub const fn fixed(value: T) -> Self
Creates a new Auto instance with a fixed value.
§Examples
use cot::db::Auto;
let auto = Auto::fixed(42);
assert!(matches!(auto, Auto::Fixed(42)));
assert_eq!(auto.unwrap(), 42);Sourcepub fn unwrap(self) -> T
pub fn unwrap(self) -> T
Returns the value, if the object is the Auto::Fixed variant; panics
otherwise.
This is an equivalent of Option::unwrap.
§Panics
If the object is the Auto::Auto variant.
§Examples
use cot::db::Auto;
let auto = Auto::fixed(42);
assert_eq!(auto.unwrap(), 42);Sourcepub fn expect(self, message: &str) -> T
pub fn expect(self, message: &str) -> T
Returns the value, if the object is the Auto::Fixed variant; panics
with given message otherwise.
This is an equivalent of Option::expect.
§Panics
If the object is the Auto::Auto variant.
§Examples
use cot::db::Auto;
let auto = Auto::fixed(42);
assert_eq!(auto.expect("expected a fixed value"), 42);Trait Implementations§
Source§impl<T: AsFormField> AsFormField for Auto<T>
impl<T: AsFormField> AsFormField for Auto<T>
Source§type Type = <T as AsFormField>::Type
type Type = <T as AsFormField>::Type
The form field type associated with the field.
Source§fn new_field(
options: FormFieldOptions,
custom_options: <Self::Type as FormField>::CustomOptions,
) -> Self::Type
fn new_field( options: FormFieldOptions, custom_options: <Self::Type as FormField>::CustomOptions, ) -> Self::Type
Creates a new form field with the given options and custom options. Read more
Source§fn clean_value(field: &Self::Type) -> Result<Self, FormFieldValidationError>where
Self: Sized,
fn clean_value(field: &Self::Type) -> Result<Self, FormFieldValidationError>where
Self: Sized,
Validates the value of the field and converts it to the final type. This
method should return an error if the value is invalid. Read more
Source§fn to_field_value(&self) -> String
fn to_field_value(&self) -> String
Returns
self as a value that can be set with FormField::set_value.Source§impl<T: DatabaseField> DatabaseField for Auto<T>
impl<T: DatabaseField> DatabaseField for Auto<T>
Source§impl<T: DatabaseField> FromDbValue for Auto<T>
impl<T: DatabaseField> FromDbValue for Auto<T>
Source§fn from_sqlite(value: SqliteValueRef<'_>) -> Result<Self>where
Self: Sized,
fn from_sqlite(value: SqliteValueRef<'_>) -> Result<Self>where
Self: Sized,
Available on crate feature
sqlite only.Converts the given SQLite database value to a Rust value. Read more
Source§fn from_postgres(value: PostgresValueRef<'_>) -> Result<Self>where
Self: Sized,
fn from_postgres(value: PostgresValueRef<'_>) -> Result<Self>where
Self: Sized,
Available on crate feature
postgres only.Converts the given PostgreSQL database value to a Rust value. Read more
Source§fn from_mysql(value: MySqlValueRef<'_>) -> Result<Self>where
Self: Sized,
fn from_mysql(value: MySqlValueRef<'_>) -> Result<Self>where
Self: Sized,
Available on crate feature
mysql only.Converts the given MySQL database value to a Rust value. Read more
Source§impl<T: DatabaseField> ToDbFieldValue for Auto<T>
impl<T: DatabaseField> ToDbFieldValue for Auto<T>
Source§fn to_db_field_value(&self) -> DbFieldValue
fn to_db_field_value(&self) -> DbFieldValue
Converts the Rust value to a
DbFieldValue that indicates whether
the value should be automatically generated by the database, or
contains a specific, explicitly provided value.impl<T: Copy> Copy for Auto<T>
impl<T: Eq> Eq for Auto<T>
impl<T: PrimaryKey> PrimaryKey for Auto<T>
impl<T> StructuralPartialEq for Auto<T>
Auto Trait Implementations§
impl<T> Freeze for Auto<T>where
T: Freeze,
impl<T> RefUnwindSafe for Auto<T>where
T: RefUnwindSafe,
impl<T> Send for Auto<T>where
T: Send,
impl<T> Sync for Auto<T>where
T: Sync,
impl<T> Unpin for Auto<T>where
T: Unpin,
impl<T> UnwindSafe for Auto<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key and return true if they are equal.Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoField<Auto<T>> for T
impl<T> IntoField<Auto<T>> for T
Source§fn into_field(self) -> Auto<T>
fn into_field(self) -> Auto<T>
Available on crate feature
db only.Converts the type to the field type.
Source§impl<T> IntoField<T> for Twhere
T: ToDbFieldValue,
impl<T> IntoField<T> for Twhere
T: ToDbFieldValue,
Source§fn into_field(self) -> T
fn into_field(self) -> T
Available on crate feature
db only.Converts the type to the field type.