Struct Output

Source
pub struct Output<T> { /* private fields */ }
Expand description

Represents the output of an operation along with a Vec of any Warnings generated by the operation.

If the contained type T implements the Positional trait, then Output<T> will also implement the Positional trait, and will apply any changes to both the contained output and the contained Warnings.

Implementations§

Source§

impl<T> Output<T>

Source

pub fn new(value: T) -> Self

Creates a new Output with the given value and no Warnings

§Arguments
  • value - The output value
§Examples
use tweep::Output;
let out:Output<u32> = Output::new(23);
assert_eq!(*out.get_output(), 23);
assert!(!out.has_warnings());
Source

pub fn with_warnings(self, warnings: Vec<Warning>) -> Self

Builder method to add Warnings to an Output and return the object

§Arguments
  • warnings - The list of Warnings to add to the object
§Examples
use tweep::{FullContext, Output, Warning, WarningKind};
let warnings = vec![ Warning::new(WarningKind::MissingStoryTitle, Some(context)) ];
let out:Output<u32> = Output::new(23).with_warnings(warnings.clone());
assert!(out.has_warnings());
assert_eq!(*out.get_warnings(), warnings);
Source

pub fn get_output(&self) -> &T

Returns a reference to the output field

§Examples
use tweep::Output;
let out = Output::new("hail eris");
assert_eq!(*out.get_output(), "hail eris");
Source

pub fn mut_output(&mut self) -> &mut T

Returns a mutable reference to the output field

§Examples
use tweep::Output;
let mut out = Output::new(23 as usize);
*out.mut_output() = 5;
assert_eq!(*out.get_output(), 5);
Source

pub fn has_warnings(&self) -> bool

Returns true if the object has associated Warnings

§Examples
use tweep::{FullContext, Output, Warning, WarningKind};
let out:Output<u8> = Output::new(5);
assert!(!out.has_warnings());
let out:Output<u8> = Output::new(5)
    .with_warnings(vec![ Warning::new(WarningKind::UnclosedLink, Some(context)) ]);
assert!(out.has_warnings());
Source

pub fn get_warnings(&self) -> &Vec<Warning>

Returns a reference to the associated Vec of Warnings

§Examples
use tweep::{FullContext, Output, Warning, WarningKind};
let out:Output<u8> = Output::new(5)
    .with_warnings(vec![ Warning::new(WarningKind::UnclosedLink, Some(context.clone())) ]);
assert_eq!(out.get_warnings(), &vec![ Warning::new(WarningKind::UnclosedLink, Some(context)) ]);
Source

pub fn take(self) -> (T, Vec<Warning>)

Consumes the Output and returns the output and warnings as a tuple

§Examples
use tweep::{FullContext, Output, Warning, WarningKind};
let warnings = vec![ Warning::new(WarningKind::MissingStoryTitle, Some(context.clone())) ];
let out:Output<u32> = Output::new(23).with_warnings(warnings.clone());
let (t, w) = out.take();
assert_eq!(t, 23);
assert_eq!(w, warnings);

// Error!
// let _ = out.get_output();
Source§

impl<T, E> Output<Result<T, E>>

This provides a handful of utility methods for an Output that contains a Result as its contained output

Source

pub fn is_ok(&self) -> bool

Returns true if the contained Result is Ok

§Examples
use tweep::{Output, Error};
let out:Output<Result<u32, Error>> = Output::new(Ok(23));
assert!(out.is_ok());
Source

pub fn is_err(&self) -> bool

Returns true if the contained Result is Err

§Examples
use tweep::{Output, Error, ErrorKind, FullContext};
let context = FullContext::from(None, "::".to_string());
let err = Error::new(ErrorKind::EmptyName, Some(context));
let out:Output<Result<u32, Error>> = Output::new(Err(err));
assert!(out.is_err());
Source

pub fn into_ok<U, F>(self) -> Output<Result<U, F>>
where T: Into<U>,

Converts this Output<Result<T,E>> into an Output<Result<U,F>>, where T can be converted into U.

§Panics

Panics if the contained Result<T,E> is not Ok

§Examples
use tweep::{Output, Error};
let out:Output<Result<u8, Error>> = Output::new(Ok(23));

// The destination Error type does not need to be convertible from the source
let other:Output<Result<u32, String>> = out.into_ok();
assert!(other.is_ok());
assert_eq!(*other.get_output(), Ok(23));
Source

pub fn into_err<U, F>(self) -> Output<Result<U, F>>
where E: Into<F>,

Converts this Output<Result<T,E>> into an Output<Result<U,F>>, where E can be converted into F

§Panics

Panics if the contained Result<T,E> is not Err

§Examples
use tweep::Output;
let out:Output<Result<u8, u8>> = Output::new(Err(5));

// The destination Ok type does not need to be convertible from the source
let other:Output<Result<String, u32>> = out.into_err();
assert!(other.is_err());
assert_eq!(*other.get_output(), Err(5));
Source

pub fn into_result<U, F>(self) -> Output<Result<U, F>>
where T: Into<U>, E: Into<F>,

Converts this Output<Result<T,E>> into an Output<Result<U,F>>, where T can be converted into U and E can be converted into F

§Examples
use tweep::Output;
let mut out:Output<Result<u8, u8>> = Output::new(Ok(23));
let mut other:Output<Result<u32, u32>> = out.into_result();
assert!(other.is_ok());
assert_eq!(*other.get_output(), Ok(23));
out = Output::new(Err(5));
other = out.into_result();
assert!(other.is_err());
assert_eq!(*other.get_output(), Err(5));

Auto Trait Implementations§

§

impl<T> Freeze for Output<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for Output<T>
where T: RefUnwindSafe,

§

impl<T> Send for Output<T>
where T: Send,

§

impl<T> Sync for Output<T>
where T: Sync,

§

impl<T> Unpin for Output<T>
where T: Unpin,

§

impl<T> UnwindSafe for Output<T>
where T: 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> 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<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.