From add17d90d145ff95877b16988896aafce04f134e Mon Sep 17 00:00:00 2001 From: Dan Callahan Date: Fri, 3 Apr 2015 14:29:33 -0500 Subject: [PATCH 1/5] Remove old_io from trpl/concurrency.md --- src/doc/trpl/concurrency.md | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/src/doc/trpl/concurrency.md b/src/doc/trpl/concurrency.md index 6b814a685424e..ca3745cfa0522 100644 --- a/src/doc/trpl/concurrency.md +++ b/src/doc/trpl/concurrency.md @@ -88,9 +88,8 @@ When `guard` goes out of scope, it will block execution until the thread is finished. If we didn't want this behaviour, we could use `thread::spawn()`: ``` -# #![feature(old_io, std_misc)] +# #![feature(std_misc)] use std::thread; -use std::old_io::timer; use std::time::Duration; fn main() { @@ -98,7 +97,7 @@ fn main() { println!("Hello from a thread!"); }); - timer::sleep(Duration::milliseconds(50)); + thread::sleep_ms(50); } ``` @@ -147,9 +146,8 @@ As an example, here is a Rust program that would have a data race in many languages. It will not compile: ```ignore -# #![feature(old_io, std_misc)] +# #![feature(std_misc)] use std::thread; -use std::old_io::timer; use std::time::Duration; fn main() { @@ -161,7 +159,7 @@ fn main() { }); } - timer::sleep(Duration::milliseconds(50)); + thread::sleep_ms(50); } ``` @@ -187,9 +185,8 @@ only one person at a time can mutate what's inside. For that, we can use the but for a different reason: ```ignore -# #![feature(old_io, std_misc)] +# #![feature(std_misc)] use std::thread; -use std::old_io::timer; use std::time::Duration; use std::sync::Mutex; @@ -203,7 +200,7 @@ fn main() { }); } - timer::sleep(Duration::milliseconds(50)); + thread::sleep_ms(50); } ``` @@ -232,10 +229,9 @@ guard across thread boundaries, which gives us our error. We can use `Arc` to fix this. Here's the working version: ``` -# #![feature(old_io, std_misc)] +# #![feature(std_misc)] use std::sync::{Arc, Mutex}; use std::thread; -use std::old_io::timer; use std::time::Duration; fn main() { @@ -249,7 +245,7 @@ fn main() { }); } - timer::sleep(Duration::milliseconds(50)); + thread::sleep_ms(50); } ``` @@ -258,10 +254,9 @@ handle is then moved into the new thread. Let's examine the body of the thread more closely: ``` -# #![feature(old_io, std_misc)] +# #![feature(std_misc)] # use std::sync::{Arc, Mutex}; # use std::thread; -# use std::old_io::timer; # use std::time::Duration; # fn main() { # let data = Arc::new(Mutex::new(vec![1u32, 2, 3])); From 11449c7fd791d66b28e84c22c3d6fde7ee8cd76d Mon Sep 17 00:00:00 2001 From: Dan Callahan Date: Fri, 3 Apr 2015 14:48:13 -0500 Subject: [PATCH 2/5] Duration is unused; remove it --- src/doc/trpl/concurrency.md | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/doc/trpl/concurrency.md b/src/doc/trpl/concurrency.md index ca3745cfa0522..7a7cf15966004 100644 --- a/src/doc/trpl/concurrency.md +++ b/src/doc/trpl/concurrency.md @@ -90,7 +90,6 @@ finished. If we didn't want this behaviour, we could use `thread::spawn()`: ``` # #![feature(std_misc)] use std::thread; -use std::time::Duration; fn main() { thread::spawn(|| { @@ -148,7 +147,6 @@ languages. It will not compile: ```ignore # #![feature(std_misc)] use std::thread; -use std::time::Duration; fn main() { let mut data = vec![1u32, 2, 3]; @@ -187,7 +185,6 @@ but for a different reason: ```ignore # #![feature(std_misc)] use std::thread; -use std::time::Duration; use std::sync::Mutex; fn main() { @@ -232,7 +229,6 @@ We can use `Arc` to fix this. Here's the working version: # #![feature(std_misc)] use std::sync::{Arc, Mutex}; use std::thread; -use std::time::Duration; fn main() { let data = Arc::new(Mutex::new(vec![1u32, 2, 3])); @@ -257,7 +253,6 @@ thread more closely: # #![feature(std_misc)] # use std::sync::{Arc, Mutex}; # use std::thread; -# use std::time::Duration; # fn main() { # let data = Arc::new(Mutex::new(vec![1u32, 2, 3])); # for i in 0..2 { From 733e0ee469fb3bc2f98b4457021680ee77c7ee8d Mon Sep 17 00:00:00 2001 From: Dan Callahan Date: Fri, 3 Apr 2015 15:01:34 -0500 Subject: [PATCH 3/5] std_misc not required on 1.0.0-beta --- src/doc/trpl/concurrency.md | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/doc/trpl/concurrency.md b/src/doc/trpl/concurrency.md index 7a7cf15966004..3a8d8fb20a415 100644 --- a/src/doc/trpl/concurrency.md +++ b/src/doc/trpl/concurrency.md @@ -88,7 +88,6 @@ When `guard` goes out of scope, it will block execution until the thread is finished. If we didn't want this behaviour, we could use `thread::spawn()`: ``` -# #![feature(std_misc)] use std::thread; fn main() { @@ -145,7 +144,6 @@ As an example, here is a Rust program that would have a data race in many languages. It will not compile: ```ignore -# #![feature(std_misc)] use std::thread; fn main() { @@ -183,7 +181,6 @@ only one person at a time can mutate what's inside. For that, we can use the but for a different reason: ```ignore -# #![feature(std_misc)] use std::thread; use std::sync::Mutex; @@ -226,7 +223,6 @@ guard across thread boundaries, which gives us our error. We can use `Arc` to fix this. Here's the working version: ``` -# #![feature(std_misc)] use std::sync::{Arc, Mutex}; use std::thread; @@ -250,7 +246,6 @@ handle is then moved into the new thread. Let's examine the body of the thread more closely: ``` -# #![feature(std_misc)] # use std::sync::{Arc, Mutex}; # use std::thread; # fn main() { From adf8e1137485676d74f2026593b0ea1d085e1710 Mon Sep 17 00:00:00 2001 From: Dan Callahan Date: Fri, 3 Apr 2015 15:04:51 -0500 Subject: [PATCH 4/5] Fixup code sample This block is supposed to be a mirror of the block above it, but with most lines commented out to focus on the inner function body. However, a few lines were missing in the commented region. --- src/doc/trpl/concurrency.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/doc/trpl/concurrency.md b/src/doc/trpl/concurrency.md index 3a8d8fb20a415..09bf5028c19fe 100644 --- a/src/doc/trpl/concurrency.md +++ b/src/doc/trpl/concurrency.md @@ -248,6 +248,7 @@ thread more closely: ``` # use std::sync::{Arc, Mutex}; # use std::thread; +# # fn main() { # let data = Arc::new(Mutex::new(vec![1u32, 2, 3])); # for i in 0..2 { @@ -257,6 +258,8 @@ thread::spawn(move || { data[i] += 1; }); # } +# +# thread::sleep_ms(50); # } ``` From 0e38b88d992a7174f374ae57c95d3f18f2860292 Mon Sep 17 00:00:00 2001 From: Dan Callahan Date: Fri, 3 Apr 2015 15:33:40 -0500 Subject: [PATCH 5/5] Update line numbers referenced by compiler errors Hat tip to @tshepang in #23871 --- src/doc/trpl/concurrency.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/doc/trpl/concurrency.md b/src/doc/trpl/concurrency.md index 09bf5028c19fe..bc0a76bc2b6e7 100644 --- a/src/doc/trpl/concurrency.md +++ b/src/doc/trpl/concurrency.md @@ -162,7 +162,7 @@ fn main() { This gives us an error: ```text -12:17 error: capture of moved value: `data` +8:17 error: capture of moved value: `data` data[i] += 1; ^~~~ ``` @@ -201,10 +201,10 @@ fn main() { Here's the error: ```text -:11:9: 11:22 error: the trait `core::marker::Send` is not implemented for the type `std::sync::mutex::MutexGuard<'_, collections::vec::Vec>` [E0277] +:9:9: 9:22 error: the trait `core::marker::Send` is not implemented for the type `std::sync::mutex::MutexGuard<'_, collections::vec::Vec>` [E0277] :11 thread::spawn(move || { ^~~~~~~~~~~~~ -:11:9: 11:22 note: `std::sync::mutex::MutexGuard<'_, collections::vec::Vec>` cannot be sent between threads safely +:9:9: 9:22 note: `std::sync::mutex::MutexGuard<'_, collections::vec::Vec>` cannot be sent between threads safely :11 thread::spawn(move || { ^~~~~~~~~~~~~ ```