pub struct Warned<T, W> {
pub value: T,
pub warnings: Vec<W>,
}Expand description
Represents a value with warnings.
Fields§
§value: T§warnings: Vec<W>Implementations§
Source§impl<T, W> Warned<T, W>
impl<T, W> Warned<T, W>
Sourcepub fn new(value: T, warnings: impl IntoIterator<Item = W>) -> Self
pub fn new(value: T, warnings: impl IntoIterator<Item = W>) -> Self
use warned::*;
let a = Warned::new(123, vec!["x", "y"]);
assert_eq!(a.value, 123);
assert_eq!(a.warnings, vec!["x", "y"]);Sourcepub fn unwrap<V>(self, warnings: &mut Vec<V>) -> Twhere
W: Into<V>,
pub fn unwrap<V>(self, warnings: &mut Vec<V>) -> Twhere
W: Into<V>,
use warned::*;
let mut warnings: Vec<&str> = vec![];
assert_eq!(Warned::new(123, vec!["x", "y"]).unwrap(&mut warnings), 123);
assert_eq!(warnings, vec!["x", "y"]);Sourcepub fn map<U>(self, f: impl FnOnce(T) -> U) -> Warned<U, W>
pub fn map<U>(self, f: impl FnOnce(T) -> U) -> Warned<U, W>
use warned::*;
let a = Warned::new(123, vec!["x", "y"]);
let b = a.map(|value| 2 * value);
assert_eq!(b.value, 246);
assert_eq!(b.warnings, vec!["x", "y"]);Sourcepub fn map_warnings<V>(self, f: impl Fn(W) -> V) -> Warned<T, V>
pub fn map_warnings<V>(self, f: impl Fn(W) -> V) -> Warned<T, V>
use warned::*;
let a = Warned::new(123, vec!["x", "y"]);
let b = a.map_warnings(|s| s.to_uppercase());
assert_eq!(b.value, 123);
assert_eq!(b.warnings, vec!["X", "Y"]);Sourcepub fn and_then<U>(self, f: impl Fn(T) -> Warned<U, W>) -> Warned<U, W>
pub fn and_then<U>(self, f: impl Fn(T) -> Warned<U, W>) -> Warned<U, W>
use warned::*;
let a = Warned::new(123, vec!["x", "y"]);
let b = a.and_then(|value| Warned::new(value as f64 / 2.0, vec!["z"]));
assert_eq!(b.value, 123.0 / 2.0);
assert_eq!(b.warnings, vec!["x", "y", "z"]);Sourcepub fn into_result(self) -> Result<T, Vec<W>>
pub fn into_result(self) -> Result<T, Vec<W>>
Returns Ok only if self has no warnings. Otherwise, returns Err.
use warned::*;
assert_eq!(Warned::<_, &str>::new(123, []).into_result(), Ok(123));
assert_eq!(Warned::new(123, ["warning"]).into_result(), Err(vec!["warning"]));Sourcepub fn from_result_or_else(src: Result<T, W>, f: impl FnOnce() -> T) -> Self
pub fn from_result_or_else(src: Result<T, W>, f: impl FnOnce() -> T) -> Self
Returns a value with no warnings if the src is Ok.
Otherwise, returns a value of f() with single warning.
use warned::Warned;
let a = Warned::<_, &str>::from_result_or_else(Ok(1), || 2);
assert_eq!(a.value, 1);
assert!(a.warnings.is_empty());
let b = Warned::<_, &str>::from_result_or_else(Err("oops"), || 2);
assert_eq!(b.value, 2);
assert_eq!(b.warnings, vec!["oops"]);Sourcepub fn from_result_or(src: Result<T, W>, default: T) -> Self
pub fn from_result_or(src: Result<T, W>, default: T) -> Self
Returns a value with no warnings if the src is Ok.
Otherwise, returns a given default value with single warning.
use warned::Warned;
let a = Warned::<_, &str>::from_result_or(Ok(1), 2);
assert_eq!(a.value, 1);
assert!(a.warnings.is_empty());
let b = Warned::<_, &str>::from_result_or(Err("oops"), 2);
assert_eq!(b.value, 2);
assert_eq!(b.warnings, vec!["oops"]);Sourcepub fn from_result_or_default(src: Result<T, W>) -> Selfwhere
T: Default,
pub fn from_result_or_default(src: Result<T, W>) -> Selfwhere
T: Default,
Returns a value with no warnings if the src is Ok.
Otherwise, returns a T::default() value with single warning.
use warned::Warned;
let a = Warned::<i32, &str>::from_result_or_default(Ok(1));
assert_eq!(a.value, 1);
assert!(a.warnings.is_empty());
let b = Warned::<i32, &str>::from_result_or_default(Err("oops"));
assert_eq!(b.value, 0);
assert_eq!(b.warnings, vec!["oops"]);Source§impl<T, W> Warned<Option<T>, W>
impl<T, W> Warned<Option<T>, W>
Sourcepub fn from_result(src: Result<T, W>) -> Self
pub fn from_result(src: Result<T, W>) -> Self
Returns a Some value with no warnings if the src is Ok.
Otherwise, returns None with single warning.
use warned::Warned;
let a = Warned::<Option<i32>, &str>::from_result(Ok(111));
assert_eq!(a.value, Some(111));
assert!(a.warnings.is_empty());
let b = Warned::<Option<i32>, &str>::from_result(Err("oops"));
assert!(b.value.is_none());
assert_eq!(b.warnings, vec!["oops"]);Trait Implementations§
Source§impl<T, E> From<Result<T, E>> for Warned<Option<T>, E>
impl<T, E> From<Result<T, E>> for Warned<Option<T>, E>
Source§fn from(result: Result<T, E>) -> Self
fn from(result: Result<T, E>) -> Self
use warned::Warned;
let a: Warned<Option<i32>, &str> = Ok(111).into();
assert_eq!(a.value, Some(111));
assert!(a.warnings.is_empty());
let b: Warned<Option<i32>, &str> = Err::<i32, _>("oops").into();
assert!(b.value.is_none());
assert_eq!(b.warnings, vec!["oops"]);Source§impl<T, Ts: FromIterator<T>, W> FromIterator<Result<T, W>> for Warned<Ts, W>
impl<T, Ts: FromIterator<T>, W> FromIterator<Result<T, W>> for Warned<Ts, W>
Source§fn from_iter<I: IntoIterator<Item = Result<T, W>>>(iter: I) -> Self
fn from_iter<I: IntoIterator<Item = Result<T, W>>>(iter: I) -> Self
use warned::Warned;
let src = vec![Ok(111), Err("oops"), Err("oops2"), Ok(222)];
let dst = src.into_iter().collect::<Warned<Vec<_>, _>>();
assert_eq!(dst.value, vec![111, 222]);
assert_eq!(dst.warnings, vec!["oops", "oops2"]);Source§impl<T, Ts: FromIterator<T>, W> FromIterator<Warned<T, W>> for Warned<Ts, W>
impl<T, Ts: FromIterator<T>, W> FromIterator<Warned<T, W>> for Warned<Ts, W>
Source§fn from_iter<I: IntoIterator<Item = Warned<T, W>>>(iter: I) -> Self
fn from_iter<I: IntoIterator<Item = Warned<T, W>>>(iter: I) -> Self
use warned::Warned;
let src: Vec<Warned<i32, &str>> = vec![
Warned::new(111, vec![]),
Warned::new(222, vec!["oops"]),
Warned::new(333, vec!["foo", "bar"])
];
let dst = src.into_iter().collect::<Warned<Vec<_>, _>>();
assert_eq!(dst.value, vec![111, 222, 333]);
assert_eq!(dst.warnings, vec!["oops", "foo", "bar"]);impl<T: Eq, W: Eq> Eq for Warned<T, W>
impl<T, W> StructuralPartialEq for Warned<T, W>
Auto Trait Implementations§
impl<T, W> Freeze for Warned<T, W>where
T: Freeze,
impl<T, W> RefUnwindSafe for Warned<T, W>where
T: RefUnwindSafe,
W: RefUnwindSafe,
impl<T, W> Send for Warned<T, W>
impl<T, W> Sync for Warned<T, W>
impl<T, W> Unpin for Warned<T, W>
impl<T, W> UnwindSafe for Warned<T, W>where
T: UnwindSafe,
W: 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