Add `MaybeUndefined::as_opt_ref` and `MaybeUndefined::as_opt_deref` methods. #688

This commit is contained in:
Sunli 2021-11-04 09:44:16 +08:00
parent 4c0a137993
commit 5c6fd0cd5a
2 changed files with 78 additions and 5 deletions

View File

@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## Unreleased
- Add `chrono::Duration` custom scalar. [#689](https://github.com/async-graphql/async-graphql/pull/689)
- Implement `From<Option<Option<T>>>` for `MaybeUndefined<T>`.
- Add `MaybeUndefined::as_opt_ref` and `MaybeUndefined::as_opt_deref` methods.
## [2.11.0] 2021-11-03

View File

@ -1,4 +1,5 @@
use std::borrow::Cow;
use std::ops::Deref;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
@ -61,25 +62,25 @@ impl<T> Default for MaybeUndefined<T> {
}
impl<T> MaybeUndefined<T> {
/// Returns true if the MaybeUndefined<T> is undefined.
/// Returns true if the `MaybeUndefined<T>` is undefined.
#[inline]
pub fn is_undefined(&self) -> bool {
matches!(self, MaybeUndefined::Undefined)
}
/// Returns true if the MaybeUndefined<T> is null.
/// Returns true if the `MaybeUndefined<T>` is null.
#[inline]
pub fn is_null(&self) -> bool {
matches!(self, MaybeUndefined::Null)
}
/// Returns true if the MaybeUndefined<T> is value.
/// Returns true if the `MaybeUndefined<T>` contains value.
#[inline]
pub fn is_value(&self) -> bool {
matches!(self, MaybeUndefined::Value(_))
}
/// Borrow the value, returns `None` if the value is `undefined` or `null`, otherwise returns `Some(T)`.
/// Borrow the value, returns `None` if the the `MaybeUndefined<T>` is `undefined` or `null`, otherwise returns `Some(T)`.
#[inline]
pub fn value(&self) -> Option<&T> {
match self {
@ -88,7 +89,7 @@ impl<T> MaybeUndefined<T> {
}
}
/// Convert MaybeUndefined<T> to Option<T>.
/// Converts the `MaybeUndefined<T>` to `Option<T>`.
#[inline]
pub fn take(self) -> Option<T> {
match self {
@ -96,6 +97,30 @@ impl<T> MaybeUndefined<T> {
_ => None,
}
}
/// Converts the `MaybeUndefined<T>` to `Option<Option<T>>`.
#[inline]
pub fn as_opt_ref(&self) -> Option<Option<&T>> {
match self {
MaybeUndefined::Undefined => None,
MaybeUndefined::Null => Some(None),
MaybeUndefined::Value(value) => Some(Some(value)),
}
}
/// Converts the `MaybeUndefined<T>` to `Option<Option<&U>>`.
#[inline]
pub fn as_opt_deref<U>(&self) -> Option<Option<&U>>
where
U: ?Sized,
T: Deref<Target = U>,
{
match self {
MaybeUndefined::Undefined => None,
MaybeUndefined::Null => Some(None),
MaybeUndefined::Value(value) => Some(Some(value.deref())),
}
}
}
impl<T: Type> Type for MaybeUndefined<T> {
@ -166,6 +191,16 @@ impl<T> From<MaybeUndefined<T>> for Option<Option<T>> {
}
}
impl<T> From<Option<Option<T>>> for MaybeUndefined<T> {
fn from(value: Option<Option<T>>) -> Self {
match value {
Some(Some(value)) => Self::Value(value),
Some(None) => Self::Null,
None => Self::Undefined,
}
}
}
#[cfg(test)]
mod tests {
use crate::*;
@ -260,4 +295,40 @@ mod tests {
Some(Some(42))
);
}
#[test]
fn test_as_opt_ref() {
let mut value: MaybeUndefined<String>;
let mut r: Option<Option<&String>>;
value = MaybeUndefined::Undefined;
r = value.as_opt_ref();
assert_eq!(r, None);
value = MaybeUndefined::Null;
r = value.as_opt_ref();
assert_eq!(r, Some(None));
value = MaybeUndefined::Value("abc".to_string());
r = value.as_opt_ref();
assert_eq!(r, Some(Some(&"abc".to_string())));
}
#[test]
fn test_as_opt_deref() {
let mut value: MaybeUndefined<String>;
let mut r: Option<Option<&str>>;
value = MaybeUndefined::Undefined;
r = value.as_opt_deref();
assert_eq!(r, None);
value = MaybeUndefined::Null;
r = value.as_opt_deref();
assert_eq!(r, Some(None));
value = MaybeUndefined::Value("abc".to_string());
r = value.as_opt_deref();
assert_eq!(r, Some(Some("abc")));
}
}