Convert

Trait Convert 

Source
pub trait Convert<'a, From: Convention, To: Convention>
where Self: By<'a, To> + By<'a, From>,
{ // Required method fn convert(from: <Self as By<'a, From>>::Type) -> <Self as By<'a, To>>::Type; }
Expand description

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§

Source

fn convert(from: <Self as By<'a, From>>::Type) -> <Self as By<'a, To>>::Type

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

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<'a, T> Convert<'a, Val, Val> for T

Source§

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

Source§

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

Source§

impl<'a, T: 'a> Convert<'a, Mut, Mut> for T

Source§

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

Source§

impl<'a, T: 'a> Convert<'a, Ref, Ref> for T