-
Notifications
You must be signed in to change notification settings - Fork 640
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
FutureObj::from only allows Output = () #1347
Comments
The code you linked is from the standard library. You probably meant this instead? Equivalent link to |
@hcpl: Yes, you are correct. I meant the link that you have sent. |
What do you want to use |
@Nemo157 : I have this trait in my code: pub trait Connector {
type Address;
type SendItem;
type RecvItem;
fn connect(&mut self, address: Self::Address)
-> FutureObj<Option<ConnPair<Self::SendItem, Self::RecvItem>>>;
} From what I understand you propose to change it to something like: fn connect(&mut self, address: Self::Address)
-> Pin<Box<Future<Output=Option<ConnPair<Self::SendItem, Self::RecvItem>>>>>; I remember vaguely that I tried it before and something couldn't compile at some point, but I can't exactly remember what went wrong. I can try it out again and send here the compilation error if I do reach one. I hope to get you updated this week. |
Yeah, that would have needed EDIT: see #1348 for more details. |
@Nemo157 : Great respect for your work on this. It's an exciting time to be alive! I managed to port most of my code to use This is what the trait I previously sent looks like now: pub trait Connector {
type Address;
type SendItem;
type RecvItem;
fn connect(&mut self, address: Self::Address)
-> Pin<Box<dyn Future<Output=Option<ConnPair<Self::SendItem, Self::RecvItem>>> + Send>>;
} First, I had to add The second compilation issue which I couldn't yet solve is that I can't manage to implement the Connector trait due to lifetime issues. Example for one implementation attempt: impl<A,C,S> Connector for ClientConnector<C,S>
where
A: Sync + Send + 'static,
C: Connector<Address=A, SendItem=Vec<u8>, RecvItem=Vec<u8>> + Sync + Send,
S: Spawn + Clone + Sync + Send,
{
type Address = (A, PublicKey);
type SendItem = Vec<u8>;
type RecvItem = Vec<u8>;
fn connect(&mut self, address: (A, PublicKey))
-> Pin<Box<dyn Future<Output=Option<ConnPair<Self::SendItem, Self::RecvItem>>> + Send>> {
let (relay_address, remote_public_key) = address;
let relay_connect = self.relay_connect(relay_address, remote_public_key)
.map(|res| res.ok());
Box::pinned(relay_connect)
}
} This is the compilation error I get: |
109 | fn connect(&mut self, address: (A, PublicKey))
| - let's call the lifetime of this reference `'1`
...
115 | Box::pinned(relay_connect)
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ returning this value requires that `'1` must outlive `'static` When I was using I realize that I might be slightly off topic at this point with this issue. EDIT: I managed to get the code compiling by changing the trait method to be: fn connect<'a>(&'a mut self, address: Self::Address)
-> Pin<Box<dyn Future<Output=Option<ConnPair<Self::SendItem, Self::RecvItem>>> + Send + 'a>>; Thank you for your help! |
That sounds correct,
That's what the old signature basically was, it was able to use lifetime elision with fn connect(&mut self, address: Self::Address)
-> Pin<Box<dyn Future<Output=Option<ConnPair<Self::SendItem, Self::RecvItem>>> + Send + '_>>; where This was also briefly discussed on Discord a while ago. I think there will probably be a type alias introduced for this at some point to reduce the extra overhead of using it compared to type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;
fn connect(&mut self, address: Self::Address)
-> BoxFuture<'_, Option<ConnPair<Self::SendItem, Self::RecvItem>>>>; |
Hi, I have seen this piece of code at alloc/boxed.rs (Futures 0.3.0-alpha.9):
Full listing here: https://doc.rust-lang.org/src/alloc/boxed.rs.html#833-837
I have a future that returns an output that is not
()
. When I tried to compile:I was surprised to find out that
()
is the only Output supported forFutureObj::from
. As a workaround I am usingFutureObj::new
. Is this behaviour intentional? I apologize ahead of time if I'm asking something trivial here, I'm still new to this codebase.The text was updated successfully, but these errors were encountered: