These are chat archives for rust-lang/rust

1st
Apr 2018
str4d
@str4d
Apr 01 2018 08:08
I'm having trouble migrating from tokio-core to tokio. Specifically, if I have a function that returns a Box<Future<T, E>>, and I try to pass its result (after possibly applying more chaining) to tokio::run() or tokio::spawn(), I get something along the lines of "the trait std::marker::Send is not implemented for futures::Future<Error=E, Item=T>". I don't get this if I pass the function's boxed result to tokio::run() instead of returning.
Roman Proskuryakov
@kpp
Apr 01 2018 08:16

if I have a function that returns a Box<Future<T, E>>

You have to return Box<Future<T, E> + Send>

str4d
@str4d
Apr 01 2018 08:17
Aha. Thanks!
SEGFAULT
@IGBC
Apr 01 2018 15:16

Hi I am having a strange issue trying to add a boxed trait to an object:

error[E038]: the trait `cursive::backend::Backend` cannot be made into an object
  --> bifrost/src/window/mod.rs:11:5
   |
11 |     backend: Box<Backend>,
   |     ^^^^^^^^^^^^^^^^^^^^^ the trait `cursive::backend::Backend` cannot be made into an object
   |
   = note: method `init` has no receiver
   = note: method `with_color` has generic type parameters
   = note: method `with_effect` has generic type parameters

Does anyone know what these error messages mean?

Andy Grove
@andygrove
Apr 01 2018 15:17
can you paste the trait
Andy Grove
@andygrove
Apr 01 2018 15:17
if you have generic parameters then I would guess you need Box<Backend<T>> ?
your init needs a self or &self param
if you want to use it as a trait object
SEGFAULT
@IGBC
Apr 01 2018 15:21
Damn that means I need to wrap the two implementers I want to use in another struct -.-
SEGFAULT
@IGBC
Apr 01 2018 15:33
Is it possible to override a trait?
Andy Grove
@andygrove
Apr 01 2018 15:33
traits have inheritance and you can have multiple implementations of a trait
SEGFAULT
@IGBC
Apr 01 2018 15:34
can they be partial?
Andy Grove
@andygrove
Apr 01 2018 15:34
starting to get beyond my level of knowledge now
you could break your trait up into multiple traits
SEGFAULT
@IGBC
Apr 01 2018 15:35
not my trait
Nikolay Kim
@fafhrd91
Apr 01 2018 15:35
Specialization, but it is nightly
SEGFAULT
@IGBC
Apr 01 2018 15:35
#notmytrait
Nikolay Kim
@fafhrd91
Apr 01 2018 15:36
You can’t
SEGFAULT
@IGBC
Apr 01 2018 15:36
hmm I think I am gonna have to approach the problem from another angle
thanks anyway
Nikolay Kim
@fafhrd91
Apr 01 2018 15:37
You can create new type
SEGFAULT
@IGBC
Apr 01 2018 18:04
the trait cannot require that `Self : Sized`
Umm... why not?
Denis Lisov
@tanriol
Apr 01 2018 18:08
@IGBC A trait can require Self: Sized, but such a trait cannot be used as a trait object because a trait object is not Sized.
SEGFAULT
@IGBC
Apr 01 2018 18:08
ahh
TLDR I am trying to create a trait object that can take a FnOnce as an argument on a couple of it's methods.
usages of the methods error out with "method cannot be invoked on a trait object"
Method declaration:
fn with_color<F: FnOnce()>(&self, colors: theme::ColorPair, f: F) where Self: Sized;
there's a second one but it's functionally identical
Denis Lisov
@tanriol
Apr 01 2018 18:14
Yeah, a method with where Self: Sized cannot be called on a trait object.
Furthermore, a generic method cannot be called on a trait object at all, IIRC.
SEGFAULT
@IGBC
Apr 01 2018 18:16
well without where Self: Sized it complains "the trait backend::Backend cannot be made into an object"
Denis Lisov
@tanriol
Apr 01 2018 18:19
Exactly. The problem is that with_color has a type parameter, so it's effectively an infinite family of methods and thus you cannot call it through a trait object's vtable... you need a non-generic method instead.
SEGFAULT
@IGBC
Apr 01 2018 18:21
any way of passing in a lambda without using a Generic you know of?
Denis Lisov
@tanriol
Apr 01 2018 18:22
Not if it captures anything. If it does not, you can cast it to fn().
SEGFAULT
@IGBC
Apr 01 2018 18:22
I will check, I am hacking the backend of a library
    pub fn with_color<F>(&self, c: ColorStyle, f: F)
    where
        F: FnOnce(&Printer),
    {
        self.backend
            .with_color(c.resolve(&self.theme.palette), || f(self));
    }
I think that counts as a capture, but I'll ask to be sure
Denis Lisov
@tanriol
Apr 01 2018 18:25
Yeah, it does. I assume nightly-only is not an option?
SEGFAULT
@IGBC
Apr 01 2018 18:26

for me, for now it's fine, but I think that might upset the library's owner XD

Hit me, what have you got

Denis Lisov
@tanriol
Apr 01 2018 18:27
Then there's also FnBox
SEGFAULT
@IGBC
Apr 01 2018 18:28
I am told that is already awaiting merging into stable
Although I am actually not sure how to use it
Denis Lisov
@tanriol
Apr 01 2018 18:34
Hmm... not sure, I could not find that it was going to stable as it is. IIRC, changes are expected when rust-lang/rfcs#1909 is implemented.
You can store a closure as Box<FnBox()> and call it from this box :-)