@@ -9,23 +9,24 @@ use crate::sync::Arc;
99/// A scope to spawn scoped threads in.
1010///
1111/// See [`scope`] for details.
12- pub struct Scope < ' env > {
12+ pub struct Scope < ' scope , ' env : ' scope > {
1313 data : ScopeData ,
14- /// Invariance over 'env , to make sure 'env cannot shrink,
14+ /// Invariance over 'scope , to make sure 'scope cannot shrink,
1515 /// which is necessary for soundness.
1616 ///
1717 /// Without invariance, this would compile fine but be unsound:
1818 ///
19- /// ```compile_fail
19+ /// ```compile_fail,E0373
2020 /// #![feature(scoped_threads)]
2121 ///
2222 /// std::thread::scope(|s| {
23- /// s.spawn(|s | {
23+ /// s.spawn(|| {
2424 /// let a = String::from("abcd");
25- /// s.spawn(|_ | println!("{:?}", a)); // might run after `a` is dropped
25+ /// s.spawn(|| println!("{:?}", a)); // might run after `a` is dropped
2626 /// });
2727 /// });
2828 /// ```
29+ scope : PhantomData < & ' scope mut & ' scope ( ) > ,
2930 env : PhantomData < & ' env mut & ' env ( ) > ,
3031}
3132
@@ -88,12 +89,12 @@ impl ScopeData {
8889/// let mut x = 0;
8990///
9091/// thread::scope(|s| {
91- /// s.spawn(|_ | {
92+ /// s.spawn(|| {
9293/// println!("hello from the first scoped thread");
9394/// // We can borrow `a` here.
9495/// dbg!(&a);
9596/// });
96- /// s.spawn(|_ | {
97+ /// s.spawn(|| {
9798/// println!("hello from the second scoped thread");
9899/// // We can even mutably borrow `x` here,
99100/// // because no other threads are using it.
@@ -109,7 +110,7 @@ impl ScopeData {
109110#[ track_caller]
110111pub fn scope < ' env , F , T > ( f : F ) -> T
111112where
112- F : FnOnce ( & Scope < ' env > ) -> T ,
113+ F : for < ' scope > FnOnce ( & ' scope Scope < ' scope , ' env > ) -> T ,
113114{
114115 let scope = Scope {
115116 data : ScopeData {
@@ -118,6 +119,7 @@ where
118119 a_thread_panicked : AtomicBool :: new ( false ) ,
119120 } ,
120121 env : PhantomData ,
122+ scope : PhantomData ,
121123 } ;
122124
123125 // Run `f`, but catch panics so we can make sure to wait for all the threads to join.
@@ -138,7 +140,7 @@ where
138140 }
139141}
140142
141- impl < ' env > Scope < ' env > {
143+ impl < ' scope , ' env > Scope < ' scope , ' env > {
142144 /// Spawns a new thread within a scope, returning a [`ScopedJoinHandle`] for it.
143145 ///
144146 /// Unlike non-scoped threads, threads spawned with this function may
@@ -163,10 +165,10 @@ impl<'env> Scope<'env> {
163165 /// to recover from such errors.
164166 ///
165167 /// [`join`]: ScopedJoinHandle::join
166- pub fn spawn < ' scope , F , T > ( & ' scope self , f : F ) -> ScopedJoinHandle < ' scope , T >
168+ pub fn spawn < F , T > ( & ' scope self , f : F ) -> ScopedJoinHandle < ' scope , T >
167169 where
168- F : FnOnce ( & Scope < ' env > ) -> T + Send + ' env ,
169- T : Send + ' env ,
170+ F : FnOnce ( ) -> T + Send + ' scope ,
171+ T : Send + ' scope ,
170172 {
171173 Builder :: new ( ) . spawn_scoped ( self , f) . expect ( "failed to spawn thread" )
172174 }
@@ -196,7 +198,7 @@ impl Builder {
196198 /// thread::scope(|s| {
197199 /// thread::Builder::new()
198200 /// .name("first".to_string())
199- /// .spawn_scoped(s, |_ |
201+ /// .spawn_scoped(s, ||
200202 /// {
201203 /// println!("hello from the {:?} scoped thread", thread::current().name());
202204 /// // We can borrow `a` here.
@@ -205,7 +207,7 @@ impl Builder {
205207 /// .unwrap();
206208 /// thread::Builder::new()
207209 /// .name("second".to_string())
208- /// .spawn_scoped(s, |_ |
210+ /// .spawn_scoped(s, ||
209211 /// {
210212 /// println!("hello from the {:?} scoped thread", thread::current().name());
211213 /// // We can even mutably borrow `x` here,
@@ -222,14 +224,14 @@ impl Builder {
222224 /// ```
223225 pub fn spawn_scoped < ' scope , ' env , F , T > (
224226 self ,
225- scope : & ' scope Scope < ' env > ,
227+ scope : & ' scope Scope < ' scope , ' env > ,
226228 f : F ,
227229 ) -> io:: Result < ScopedJoinHandle < ' scope , T > >
228230 where
229- F : FnOnce ( & Scope < ' env > ) -> T + Send + ' env ,
230- T : Send + ' env ,
231+ F : FnOnce ( ) -> T + Send + ' scope ,
232+ T : Send + ' scope ,
231233 {
232- Ok ( ScopedJoinHandle ( unsafe { self . spawn_unchecked_ ( || f ( scope ) , Some ( & scope. data ) ) } ?) )
234+ Ok ( ScopedJoinHandle ( unsafe { self . spawn_unchecked_ ( || f ( ) , Some ( & scope. data ) ) } ?) )
233235 }
234236}
235237
@@ -245,7 +247,7 @@ impl<'scope, T> ScopedJoinHandle<'scope, T> {
245247 /// use std::thread;
246248 ///
247249 /// thread::scope(|s| {
248- /// let t = s.spawn(|_ | {
250+ /// let t = s.spawn(|| {
249251 /// println!("hello");
250252 /// });
251253 /// println!("thread id: {:?}", t.thread().id());
@@ -279,7 +281,7 @@ impl<'scope, T> ScopedJoinHandle<'scope, T> {
279281 /// use std::thread;
280282 ///
281283 /// thread::scope(|s| {
282- /// let t = s.spawn(|_ | {
284+ /// let t = s.spawn(|| {
283285 /// panic!("oh no");
284286 /// });
285287 /// assert!(t.join().is_err());
@@ -299,7 +301,7 @@ impl<'scope, T> ScopedJoinHandle<'scope, T> {
299301 }
300302}
301303
302- impl < ' env > fmt:: Debug for Scope < ' env > {
304+ impl < ' scope , ' env > fmt:: Debug for Scope < ' scope , ' env > {
303305 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
304306 f. debug_struct ( "Scope" )
305307 . field ( "num_running_threads" , & self . data . num_running_threads . load ( Ordering :: Relaxed ) )
0 commit comments