SmallBox: Box on stack!


#21

Ok, thanks for explanation. Yeah, using box makes sense, I forgot about the Unsize requirement…


#22

Looks really nice!

I was just reading about different painful ways to return Futures in Tokio… and I thought, what if:

struct MyFuture<I, E> {
    inner: SmallBox<Future<Item = I, Error = E>, [u8; 256]>
}

impl<I, E> Future for MyFuture<I, E> { 
    ...
}

impl Service for App {
   type Future = MyFuture<Self::Response,Self::Error>;
}

It’s easier to find out composed type size than type name, right? :smile:

Or with macro:

macro_rules! small_future {
    ($name:ident; $size:expr) => {
        pub struct $name<I, E> {
            pub inner: SmallBox<Future<Item = I, Error = E>, [u8; $size]>,
        }

        impl<I, E> $name<I, E> {
           pub fn new<F: Future<Item = I, Error = E> + 'static>(f: F) -> Self {
                $name { inner: SmallBox::new(f) }
           }
        }

        impl<I, E> Future for $name<I, E> {
            type Item = I;
            type Error = E;

            fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
               self.inner.poll()
            }
        }
    };
}

small_future![MyFuture; 256];

Then it’s probably possible to create a variance that gives a compile time error if one doesn’t fit onto stack.