[][src]Trait core_extensions::SelfOps

pub trait SelfOps {
    const T: VariantPhantom<Self>;
    const T_D: VariantDropPhantom<Self>;
    fn assert_ty(self, _other: VariantPhantom<Self>) -> Self
    where
        Self: Sized
, { ... }
fn assert_ty_ref(&self, _other: VariantPhantom<Self>) -> &Self
    where
        Self: Sized
, { ... }
fn assert_ty_mut(&mut self, _other: VariantPhantom<Self>) -> &mut Self
    where
        Self: Sized
, { ... }
fn ty_(&self) -> VariantPhantom<Self> { ... }
fn ty_d(&self) -> VariantDropPhantom<Self> { ... }
fn ty_inv(&self) -> InvariantPhantom<Self> { ... }
fn ty_inv_ref(&self) -> InvariantRefPhantom<Self> { ... }
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, _: VariantPhantom<T>) -> 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
, { ... } }

Extension trait for every type.

The most importand methods in this are:

  • piped: Allows emulating the pipeline operator.

  • mutated: Allows mutating self with a closure passing it along the method chain

  • observe: Observes the value of self with a closure passing it along the method chain unmodified.

  • into_, as_ref_, as_mut_: Alternative syntax for the standard conversion methods.

Associated Constants

const T: VariantPhantom<Self>

Represents Self by using a VariantPhantom, using the syntax Type::T to pass it in methods with _:VariantPhantom<T> parameters.

Example

use core_extensions::{SelfOps,IteratorExt};

assert_eq!((0..4).collect_(Vec::T       ),vec![0,1,2,3]);
assert_eq!((0..4).collect_(Vec::<_>::T  ),vec![0,1,2,3]);
assert_eq!((0..4).collect_(Vec::<i32>::T),vec![0,1,2,3]);

const T_D: VariantDropPhantom<Self>

Represents Self by using a VariantDropPhantom,for specialized cases.

For advanced use cases when one needs a drop check on a PhantomData.

Loading content...

Provided methods

fn assert_ty(self, _other: VariantPhantom<Self>) -> Self where
    Self: Sized

Asserts that other is the same type as self.

fn assert_ty_ref(&self, _other: VariantPhantom<Self>) -> &Self where
    Self: Sized

Asserts that other is the same type as self.

fn assert_ty_mut(&mut self, _other: VariantPhantom<Self>) -> &mut Self where
    Self: Sized

Asserts that other is the same type as self.

fn ty_(&self) -> VariantPhantom<Self>

Equivalent to SelfOps::T,as a method.

Reasons for calling this method instead:

  • The type is longer that the code required to instantiate it.

  • To assert that 2 variables have the same type,using var0.ty_()==var1.ty_().

fn ty_d(&self) -> VariantDropPhantom<Self>

Equivalent to [Self::ty_],for specialized cases.

For specialized cases when one needs a drop check on a PhantomData.

fn ty_inv(&self) -> InvariantPhantom<Self>

Equivalent to [Self::ty_] with an invariant type.

fn ty_inv_ref(&self) -> InvariantRefPhantom<Self>

Equivalent to [Self::ty_] with an invariant lifetime.

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

Identity comparison to another value of the same type.

Comparing 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);

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

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.");

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.

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

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

Observes the value of self passing it along unmodified. Useful in a long method chain.

Example

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

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

Performs a conversion using Into.

This method is defined to allow using the .into_(String::T) syntax.

type::T is an associated constant defined for every type here.

Example

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

let word="hello";
assert_eq!(word,word.into_(Cow::T));
assert_eq!(word,word.into_(Cow::<str>::T));
assert_eq!(word,word.into_(String::T));

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

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

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

Example

use core_extensions::SelfOps;
let s="the path";
assert_eq!( s,s.as_ref_::<str>());

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

Performs a mutable reference to mutable reference conversion using 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_::<[_]>());

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");
Loading content...

Implementors

impl<T: ?Sized> SelfOps for T[src]

Loading content...