Warned

Struct Warned 

Source
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>

Source

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"]);
Source

pub fn unwrap<V>(self, warnings: &mut Vec<V>) -> T
where 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"]);
Source

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"]);
Source

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"]);
Source

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"]);
Source

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"]));
Source

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"]);
Source

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"]);
Source

pub fn from_result_or_default(src: Result<T, W>) -> Self
where 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>

Source

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: Clone, W: Clone> Clone for Warned<T, W>

Source§

fn clone(&self) -> Warned<T, W>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug, W: Debug> Debug for Warned<T, W>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Default, W> Default for Warned<T, W>

Source§

fn default() -> Self

use warned::*;
let a = Warned::<Vec<i32>, String>::default();
assert!(a.value.is_empty());
assert!(a.warnings.is_empty());
Source§

impl<T, W> Deref for Warned<T, W>

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &T

Dereferences the value.
Source§

impl<T1, T2, W> From<(Warned<T1, W>, Warned<T2, W>)> for Warned<(T1, T2), W>

Source§

fn from((a, b): (Warned<T1, W>, Warned<T2, W>)) -> Self

use warned::Warned;
let a = Warned::new(123, vec!["x", "xx"]);
let b = Warned::new(321, vec!["y", "yy"]);
let c: Warned<(_, _), _> = (a, b).into();
assert_eq!(c.value, (123, 321));
assert_eq!(c.warnings, vec!["x", "xx", "y", "yy"]);
Source§

impl<T, E> From<Result<T, E>> for Warned<Option<T>, E>

Source§

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: Default, E> From<Result<T, E>> for Warned<T, E>

Source§

fn from(result: Result<T, E>) -> Self

use warned::Warned;

let a: Warned<i32, &str> = Ok(111).into();
assert_eq!(a.value, 111);
assert!(a.warnings.is_empty());

let b: Warned<i32, &str> = Err::<i32, _>("oops").into();
assert_eq!(b.value, 0);
assert_eq!(b.warnings, vec!["oops"]);
Source§

impl<T, W> From<T> for Warned<T, W>

Source§

fn from(value: T) -> Self

use warned::*;
let a: Warned<i32, String> = (111).into();
assert_eq!(a.value, 111);
assert!(a.warnings.is_empty());
Source§

impl<T, W> From<Warned<T, W>> for Result<T, Vec<W>>

Source§

fn from(src: Warned<T, W>) -> Self

use warned::*;
assert_eq!(Ok(123), Warned::<_, &str>::new(123, []).into());
assert_eq!(Err(vec!["warning"]), Warned::new(123, ["warning"]).into());
Source§

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

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>

Source§

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"]);
Source§

impl<T: PartialEq, W: PartialEq> PartialEq for Warned<T, W>

Source§

fn eq(&self, other: &Warned<T, W>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: Eq, W: Eq> Eq for Warned<T, W>

Source§

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>

§

impl<T, W> Send for Warned<T, W>
where T: Send, W: Send,

§

impl<T, W> Sync for Warned<T, W>
where T: Sync, W: Sync,

§

impl<T, W> Unpin for Warned<T, W>
where T: Unpin, W: Unpin,

§

impl<T, W> UnwindSafe for Warned<T, W>
where T: UnwindSafe, W: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T, U> ForceFrom<T> for U
where T: Into<U>,

Source§

impl<T, U> ForceInto<U> for T
where U: ForceFrom<T>,

Source§

type Warning = <U as ForceFrom<T>>::Warning

Source§

fn force_into(self) -> Warned<U, <T as ForceInto<U>>::Warning>

Source§

impl<T> From<!> for T

Source§

fn from(t: !) -> T

Converts to this type from the input type.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.