Struct Adapter

Source
pub struct Adapter<'a, T>
where T: FromStr + Display,
{ /* private fields */ }
Expand description

Wraps a reference to a String representation of some type

The String can be accessed as if it were the type. An Adapter can be made for any type that implements FromStr and Display. An Adapter must be dropped before the String can be accessed again.

§Example

use kai::*;

// A `Vec` of number strings
let mut nums: Vec<String> = vec![
    "4".into(),
    "1".into(),
    "-1".into(),
];

// Iterate over `Adapters` that wrap the number strings
// The `Adapter`s can be modified as if they are numbers
for mut n in nums.iter_mut().filter_map(|s| Adapter::<i32>::from(s).ok()) {
    *n += 2;
    *n *= 2;
}

assert_eq!(
    vec!["12".to_string(), "6".into(), "2".into()],
    nums,
);

Implementations§

Source§

impl<'a, T> Adapter<'a, T>
where T: FromStr + Display,

Source

pub fn from(string: &'a mut String) -> Result<Adapter<'a, T>, T::Err>

Create a new Adapter from a String

Source

pub fn finish(self)

Force a drop, returning ownership to the string

This function only needs to be called if you want access to the string before the Adapter would normally be dropped.

Trait Implementations§

Source§

impl<'a, T> AsRef<T> for Adapter<'a, T>
where T: FromStr + Display,

Source§

fn as_ref(&self) -> &T

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<'a, T> Borrow<T> for Adapter<'a, T>
where T: FromStr + Display,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<'a, T> Debug for Adapter<'a, T>
where T: FromStr + Display + Debug,

Source§

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

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

impl<'a, T> Deref for Adapter<'a, T>
where T: FromStr + Display,

Source§

type Target = T

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<'a, T> DerefMut for Adapter<'a, T>
where T: FromStr + Display,

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
Source§

impl<'a, T> Display for Adapter<'a, T>
where T: FromStr + Display,

Source§

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

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

impl<'a, T> Drop for Adapter<'a, T>
where T: FromStr + Display,

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<'a, T> Freeze for Adapter<'a, T>
where T: Freeze,

§

impl<'a, T> RefUnwindSafe for Adapter<'a, T>
where T: RefUnwindSafe,

§

impl<'a, T> Send for Adapter<'a, T>
where T: Send,

§

impl<'a, T> Sync for Adapter<'a, T>
where T: Sync,

§

impl<'a, T> Unpin for Adapter<'a, T>
where T: Unpin,

§

impl<'a, T> !UnwindSafe for Adapter<'a, T>

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> Bind for T

Source§

fn bind_mut<F>(self, f: F) -> Self
where F: FnMut(&mut Self),

Binds the value, mutates it, and returns it
Source§

fn bind_map<F, R>(self, f: F) -> R
where F: FnMut(Self) -> R,

Binds the value, maps it with the function, and returns the mapped value
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<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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.