Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ActiveValue::try_as_ref() #2197

Merged
merged 1 commit into from
May 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion src/entity/active_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,26 @@ where
None => ActiveValue::NotSet,
};
}

/// Get the inner value, unless `self` is [NotSet][ActiveValue::NotSet].
///
/// There's also a panicking version: [ActiveValue::as_ref].
///
/// ## Examples
///
/// ```
/// # use sea_orm::ActiveValue;
/// #
/// assert_eq!(ActiveValue::Unchanged(42).try_as_ref(), Some(&42));
/// assert_eq!(ActiveValue::Set(42).try_as_ref(), Some(&42));
/// assert_eq!(ActiveValue::NotSet.try_as_ref(), None::<&i32>);
/// ```
pub fn try_as_ref(&self) -> Option<&V> {
match self {
ActiveValue::Set(value) | ActiveValue::Unchanged(value) => Some(value),
ActiveValue::NotSet => None,
}
}
}

impl<V> std::convert::AsRef<V> for ActiveValue<V>
Expand All @@ -857,7 +877,9 @@ where
{
/// # Panics
///
/// Panics if it is [ActiveValue::NotSet]
/// Panics if it is [ActiveValue::NotSet].
///
/// See [ActiveValue::try_as_ref] for a fallible non-panicking version.
fn as_ref(&self) -> &V {
match self {
ActiveValue::Set(value) | ActiveValue::Unchanged(value) => value,
Expand Down
Loading