Crate thin [] [src]

Provides thin pointer types to dynamically sized types that implement the DynSized trait. By "thin", that means they store the metadata (i.e. slice length or vtable pointer) together with the data, instead of in the pointer itself. Currently provides ThinBox, a thin version of Box.

Example: storing a closure in a ThinBox;

extern crate thin;
#[macro_use] extern crate dyn_sized;
use thin::{ThinBox, FnMove};

// Define our own subtrait to get around the orphan rule for trait impls
// Have to use `FnMove` instead of `FnOnce` or `FnBox`
trait Closure: FnMove(&str) -> String {}

fn main() {
  let s1 = String::from("Hello");

  let closure: ThinBox<FnMove(&'static str) -> String> = ThinBox::new(move |s2|{
      s1 + " " + s2 + "!"

  assert_eq!("Hello World!", closure("World"));

There's a couple things to notice here: one is that we needed to derive DynSized for our closure trait object type in order to store it in a ThinBox. And because of the orphan rule for trait impls, we needed to define our own trait Closure to do that. And the other thing to notice, is that our Closure trait has FnMove as a supertrait instead of FnBox or FnOnce, in order to be callable from inside a ThinBox. That's alright, though, since ThinBox implemnts FnOnce, and we're able to call the ThinBox<Closure> directly.


pub extern crate dyn_sized;



The storage format for thin pointers. Stores the metadata and the value together.


A thin version of Box.



A trait for dynamically sized types, similar in principle to the Sized trait. Allows conversion between fat and thin pointers.


A version of FnOnce that takes self by &mut reference instead of by value.