forked from transact-rs/sqlx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresult_ext.rs
More file actions
29 lines (24 loc) 路 732 Bytes
/
Copy pathresult_ext.rs
File metadata and controls
29 lines (24 loc) 路 732 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
use crate::error::{Error, UnexpectedNullError};
pub trait ResultExt<T>: Sized {
fn try_unwrap_optional(self) -> crate::Result<T>;
}
impl<T> ResultExt<T> for crate::Result<T> {
fn try_unwrap_optional(self) -> crate::Result<T> {
self
}
}
impl<T> ResultExt<Option<T>> for crate::Result<T> {
fn try_unwrap_optional(self) -> crate::Result<Option<T>> {
match self {
Ok(val) => Ok(Some(val)),
Err(Error::Decode(error)) => {
if let Some(UnexpectedNullError) = error.downcast_ref() {
Ok(None)
} else {
Err(Error::Decode(error))
}
}
Err(e) => Err(e),
}
}
}