2 unstable releases
Uses old Rust 2015
0.2.0 | Oct 26, 2017 |
---|---|
0.1.0 | Oct 14, 2017 |
#20 in #dynamically-sized
12KB
302 lines
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 {}
derive_DynSized!(Closure<Output=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<F: FnMove> implemnts FnOnce, and we're able to call the ThinBox<Closure>
directly.
Dependencies
~14KB