-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Add documentation about lifetimes to thread::scope. #94763
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -107,6 +107,24 @@ impl ScopeData { | |||||||||
/// a.push(4); | ||||||||||
/// assert_eq!(x, a.len()); | ||||||||||
/// ``` | ||||||||||
/// | ||||||||||
/// # Lifetimes | ||||||||||
/// | ||||||||||
/// Scoped threads involve two lifetimes: `'scope` and `'env`. | ||||||||||
/// | ||||||||||
/// The `'scope` lifetime represents the lifetime of the scope itself. | ||||||||||
/// That is: the time during which new scoped threads may be spawned, | ||||||||||
/// and also the time during which they might still be running. | ||||||||||
/// Once this lifetime ends, all scoped threads are joined. | ||||||||||
/// This lifetime starts within the `scope` function, before `f` (the argument to `scope`) starts. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's a bit weird to speak of the beginning of a lifetime, it's not really something that comes up in practice or is important. But I guess it's okay for the sake of explaining stuff:
Suggested change
|
||||||||||
/// It ends after `f` returns and all scoped threads have been joined, but before `scope` returns. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
/// | ||||||||||
/// The `'env` lifetime represents the lifetime of whatever is borrowed by the scoped threads. | ||||||||||
/// This lifetime must outlast the call to `scope`, and thus cannot be smaller than `'scope`. | ||||||||||
/// It can be as small as the call to `scope`, meaning that anything that outlives this call, | ||||||||||
/// such as local variables defined right before the scope, can be borrowed by the scoped threads. | ||||||||||
/// | ||||||||||
/// The `'env: 'scope` bound is part of the definition of the `Scope` type. | ||||||||||
#[track_caller] | ||||||||||
pub fn scope<'env, F, T>(f: F) -> T | ||||||||||
where | ||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I care about this one because that's the right ordering:
'scope
by very definition, outlives theScope
andPacket
instances, so the right property is rather "once the scoped threads are all joined, this lifetime may end", hence my suggestion