Skip to content

Commit

Permalink
Add ActiveValue::try_as_ref() (#2197)
Browse files Browse the repository at this point in the history
  • Loading branch information
Expurple committed May 9, 2024
1 parent aa626fc commit e162a1b
Showing 1 changed file with 23 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/entity/active_model.rs
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

0 comments on commit e162a1b

Please sign in to comment.