pub trait Convert<'a, From: Convention, To: Convention>{
// 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 + Clone | T: 'a | ❌** |
from Mut (&'a mut T) … | T: 'a + Clone | T: 'a | T: 'a |
* Impossible because references can’t outlive the data they borrow.
** Impossible because potentially-aliased data can’t be mutably referenced.
Required Methods§
Sourcefn convert(from: <Self as By<'a, From>>::Type) -> <Self as By<'a, To>>::Type
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.