Trait call_by::Convert[][src]

pub trait Convert<'a, From: Convention, To: Convention> where
    Self: By<'a, To> + By<'a, From>, 
{ fn convert(from: Self::Type) -> Self::Type; }

Convert between different calling conventions.

Only some conversions are sensible in Rust, due to the ownership system. These are the valid conversions, with the constraints on the underlying type T noted:

Can I convert…… to Val (T)… to Ref (&'a T)… to Mut (&'a mut T)
from Val (T) …(valid for all T)❌*❌*
from Ref (&'a T) …T: 'a + CloneT: 'a❌**
from Mut (&'a mut T) …T: 'a + CloneT: 'aT: 'a

* Impossible because references can’t outlive the data they borrow.

** Impossible because potentially-aliased data can’t be mutably referenced.

Required methods

fn convert(from: Self::Type) -> Self::Type[src]

Convert from one calling convention to another.

Because of the generic parameters on the trait, this often requires rather explicit type annotations.

Examples

use call_by::*;

let a: u8 = <u8 as Convert<Val, Val>>::convert(1);
let b: u8 = <u8 as Convert<Ref, Val>>::convert(&2);      // implicit clone
let c: u8 = <u8 as Convert<Mut, Val>>::convert(&mut 3);  // implicit clone

let d: &u8 = <u8 as Convert<Ref, Ref>>::convert(&4);
let e: &u8 = <u8 as Convert<Mut, Ref>>::convert(&mut 5);

let b: &mut u8 = <u8 as Convert<Mut, Mut>>::convert(&mut 6);
Loading content...

Implementors

impl<'a, T> Convert<'a, Val, Val> for T[src]

impl<'a, T: 'a + Clone> Convert<'a, Mut, Val> for T[src]

impl<'a, T: 'a + Clone> Convert<'a, Ref, Val> for T[src]

impl<'a, T: 'a> Convert<'a, Mut, Mut> for T[src]

impl<'a, T: 'a> Convert<'a, Mut, Ref> for T[src]

impl<'a, T: 'a> Convert<'a, Ref, Ref> for T[src]

Loading content...