-
Notifications
You must be signed in to change notification settings - Fork 634
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
FutureExt::shared does not specify potentially important behaviour in docs. #2913
Comments
Also, must_use messages of
|
^ where is this said explicitly for What your saying about the implementation of the |
Doing nothing unless polled is the normal behavior of futures, so I don't feel the need to add the same explanation to each individual future about it. (Or am I misunderstanding your comments? What is the document you specifically want?) |
Its not about futures in general its about this specific function, there is nothing that stops the implementation of (going to sleep now) |
Perhaps this will help you understand my perspective on this. Consider the following god awful code: struct ThunkAwareFuture<T> {
thunk: *mut futures::future::Shared<ThunkAwareFuture<T>>,
_t: PhantomData<T>
// other fields that would define behaviour
}
impl<T> Future for ThunkAwareFuture<T> {
type Output = T;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
// on some branch...
{
// do something with knowledge that this is a thunk
let _ = unsafe { self.thunk.as_ref().unwrap_unchecked() }.clone();
}
// polling things...
todo!()
}
}
let ptr = Box::into_raw(Box::<futures::future::Shared<ThunkAwareFuture<()>>>::new_uninit())
as *mut futures::future::Shared<ThunkAwareFuture<()>>;
let thunk = ThunkAwareFuture {
thunk: ptr,
_t: PhantomData
}.shared();
unsafe { ptr.write(thunk.clone()) };
thunk.await Putting aside concerns about memory leakage, this is (or at least hopefully demonstrates that one may make similarly) a function that should not produce UB and could be the internals to some safe API. Now if this is a valid usage of This means that a non breaking change would induce UB / other nasties in seemingly safe code! Now do I think that anyone would change the working of |
There is no need to document on every concrete implementation the lazy behavior of Just as we don't need to document that it doesn't randomly panic, or spawn a thread, or all sorts of other things it doesn't do. |
The future extension method FutureExt::shared does not specify in its documentation that it does not poll the inner future until the
Shared
it creates is polled. For people finding or being pointed to this method after looking for a "Thunk" (a construct for lazy evaluation and result sharing) this is incredibly important, as eager evaluation where lazy evaluation is expected can be catastrophic. I know this is rather minor but I think its important to clarify if this behaviour is or is not part of the API, as whilst it can probably be expected, that sort of assumption doesn't seem to be the rust way.The text was updated successfully, but these errors were encountered: