Trait core_extensions::SelfOps

source ·
pub trait SelfOps {
    // Provided methods
    fn eq_id(&self, other: &Self) -> bool { ... }
    fn piped<F, U>(self, f: F) -> U
       where F: FnOnce(Self) -> U,
             Self: Sized { ... }
    fn piped_ref<'a, F, U>(&'a self, f: F) -> U
       where F: FnOnce(&'a Self) -> U { ... }
    fn piped_mut<'a, F, U>(&'a mut self, f: F) -> U
       where F: FnOnce(&'a mut Self) -> U { ... }
    fn mutated<F>(self, f: F) -> Self
       where F: FnOnce(&mut Self),
             Self: Sized { ... }
    fn observe<F>(self, f: F) -> Self
       where F: FnOnce(&Self),
             Self: Sized { ... }
    fn into_<T>(self) -> T
       where Self: Into<T> { ... }
    fn as_ref_<T: ?Sized>(&self) -> &T
       where Self: AsRef<T> { ... }
    fn as_mut_<T: ?Sized>(&mut self) -> &mut T
       where Self: AsMut<T> { ... }
    fn drop_(self)
       where Self: Sized { ... }
}
Expand description

Extension trait for every type.

Provided Methods§

source

fn eq_id(&self, other: &Self) -> bool

Compares the address of self with the address of other.

§Example
use core_extensions::SelfOps;

let a = 5.to_string();
let b = 5.to_string();

assert!(!a.eq_id(&b));
assert!(!b.eq_id(&a));
assert!( a.eq_id(&a));
assert!( b.eq_id(&b));
assert_eq!(a,b);
source

fn piped<F, U>(self, f: F) -> U
where F: FnOnce(Self) -> U, Self: Sized,

Emulates the pipeline operator, allowing method syntax in more places.

Allows calling functions as part of a method chain.

§Example
use core_extensions::SelfOps;

use std::sync::{Mutex, Arc};

let hello = "hello"
    .to_string()
    .mutated(|s| s.push_str("_world"))
    .piped(Mutex::new)
    .piped(Arc::new);

assert_eq!(hello.lock().unwrap().as_str(), "hello_world");
§Example,calling functions
use core_extensions::SelfOps;

 
"what"
    .piped(|x| opposed(x) + "-")
    .observe(|s| assert_eq!(s, "whattahw-"))
    .piped(opposed)
    .observe(|s| assert_eq!(s, "whattahw--whattahw"));
source

fn piped_ref<'a, F, U>(&'a self, f: F) -> U
where F: FnOnce(&'a Self) -> U,

The same as piped except that the function takes &Self Useful for functions that take &Self instead of Self.

§Example
use core_extensions::SelfOps;

let problem = "air".to_string();
let edited = problem.piped_ref(|s| format!("{} problems.", s));

println!("{:?}", problem); // problem wasn't consumed by `.piped_ref`
assert_eq!(edited, "air problems.");
source

fn piped_mut<'a, F, U>(&'a mut self, f: F) -> U
where F: FnOnce(&'a mut Self) -> U,

The same as piped, except that the function takes &mut Self. Useful for functions that take &mut Self instead of Self.

source

fn mutated<F>(self, f: F) -> Self
where F: FnOnce(&mut Self), Self: Sized,

Mutates self using a closure taking self by mutable reference, passing it along the method chain.

This is useful for initializing a variable and then freezing it.

§Example of initialization
use core_extensions::SelfOps;

let list = Vec::new().mutated(|v|{
    v.push("This");
    v.push("is");
    v.push("[redacted]");
});

assert_eq!(list.join(" "), "This is [redacted]");
§Example of mutating in a method chain
use core_extensions::SelfOps;

"what".to_string()
    .mutated(|v| v.push_str(" the") )
    .observe(|v| assert_eq!(v, "what the") );
source

fn observe<F>(self, f: F) -> Self
where F: FnOnce(&Self), Self: Sized,

Observes the value of self, passing it along unmodified. Useful in long method chains.

§Example
use core_extensions::SelfOps;

let v = "1234"
    .parse()
    .observe(|d| assert_eq!(&Ok(1234), d))
    .unwrap();

assert_eq!(v,1234);
source

fn into_<T>(self) -> T
where Self: Into<T>,

Performs a conversion with Into. using the turbofish .into_::<_>() syntax.

§Example
use core_extensions::SelfOps;
use std::borrow::Cow;

let word = "hello";

assert_eq!(word, word.into_::<Cow<'_, _>>());
assert_eq!(word, word.into_::<Cow<'_, str>>());
assert_eq!(word, word.into_::<String>());

let vec_=||vec![0,1,2,3];
assert_eq!(vec_().into_::<Cow<'_, _>>(), vec_());
assert_eq!(vec_().into_::<Cow<'_, _>>(), vec_());
assert_eq!(vec_().into_::<Vec<_>>()    , vec_());
assert_eq!(vec_().into_::<Vec<_>>()    , vec_());
source

fn as_ref_<T: ?Sized>(&self) -> &T
where Self: AsRef<T>,

Performs a reference to reference conversion with AsRef, using the turbofish .as_ref_::<_>() syntax.

§Example
use core_extensions::SelfOps;

let s = "the path";

assert_eq!(s, s.as_ref_::<str>());
source

fn as_mut_<T: ?Sized>(&mut self) -> &mut T
where Self: AsMut<T>,

Performs a mutable reference to mutable reference conversion with AsMut, using the turbofish .as_mut_::<_>() syntax.

§Example
use core_extensions::SelfOps;

let mut s_0 = vec![1, 2, 3, 4];
let mut s_1 = s_0.clone();

assert_eq!(s_1, s_0.as_mut_::<[_]>());
source

fn drop_(self)
where Self: Sized,

Drops self using method notation. Alternative to std::mem::drop.

§Example,ignore #[must_use] values.
#![deny(unused_must_use)]
use std::fmt::Write;

use core_extensions::SelfOps;

let mut buff=String::new();

buff.write_str("hello_").drop_();
buff.write_str("world").drop_();

assert_eq!(buff, "hello_world");

Object Safety§

This trait is not object safe.

Implementors§

source§

impl<T: ?Sized> SelfOps for T