thread-scoped-1.0.2/.gitignore01006440001750000175000000000045131210116170014435 0ustar0000000000000000/Cargo.lock /target/ /.gitattributes thread-scoped-1.0.2/.travis.yml01006440001750000175000000003572131210116170014566 0ustar0000000000000000language: rust rust: - nightly - beta - stable sudo: false os: - linux cache: directories: - "$HOME/.cargo" - target env: global: - secure: "JKijLISIcscFxBB+W4B8lGve3TG5AhZUAojnEJu5p7FelK0vzW+hNBfOXo7OO35MC24w0t+tfEWoiy56NxSr5zJld9rB5owFiPVz47wmhXlKqI7Lu78BsiSnQqhXr8exRiJhOJZM8umD0wOeCw3+cVB1FMmHPxE5pgPM33hJWX/xvlIYeWGcUK/5Z09n/ebg2+gR9wOUyFOHnZu/xqPo2QldtLQ8QTNM2cdWJQnb2qDaPH3tfLgjVq4sTM/4LwMHyXcjLl73KwW4X0skroRBG0Ehkeickmz9ZcX6ol2BXvT6hLngZ4Vc8DOXzuWDcPM+tCYBD0s+8NLy0YIFL/uR4+2ICB0Q1FwPdnHWPD7Z8D1UXFgdQR4Y1tXk6RgqE7KUW2yUmdFqzddzw9PQDBKbgHiB7EDx2ujGnKbty6JiNY8Ja1n7MZ4jFgr8vgyCXRwrV9IW+Wr4gtO3kBPOO1ULYBq7+HLPc4qGN4ENa5wwKPmfqqXgpEZuWu7CNldXDl3IWtZw5YEJBOVBQXStnXkmqa2tIdHUlhDvUdmzxDYS8boavIOgxVlFefzDVIoPp+vfmtxGt5WyjmNV4JyBnTEUVdY1oGQij1ozjcHB50JS3Sl2/RLMpLRzMuiSIwDefTjcCr2kWMO7sBN2WqgH99F01EzM6SPN2rzRxFnvape+eTs=" - secure: "sotr3t7smgtkxSn8hN0fwKT9mGa1G0dGUBCPaVpDABKShp2P1DPmt9D0Tlbl+Co6XT9hC+4yJdUuBdSztYPPGpfL0rk7Zjns/gxdusxLVRvnW7h7g51vGdaTUXyirZhtP7eC9oYXXMj4/8jP8EaDs1h4945dwDo+GMuL3sxY2YKR769trls9+nZs0nrzopznrnXe4t8DPAAoPzMRD2B+0cWZF6xy0AYkPZL0tyFFy67BAA/Oo4X9xUPD5sILHL1y7Ob0+dcQltj+9VR2vUsbNp6ka+6GFrdKduqtRTnBh/ZO3cQZQtHghY8GIJbuvAjw8ehmnXDxGNoD3VhmYUIqmYtGFHgr6ygj3veZig6sJg085ikXp8cTByI3qWl5M9HYEFbEfNHGJBAXzHK1JDylL7w0yX4aIax8qAS4kjuYXGUoeWsr6XkAsE/yMYahnWmqcIHJTJZIoFB4egH9x9+L/oJAHQXq9259FRTqQpgBeEwgpCKAe8Ll+v+McsRbSyf83B2UxOhjgep1BK+mc3R1GcZR84bE1W2Kk5n48OJxrF6NV1kByqyMOMn++/PSzydrKaNsEu8HmS3cx762JRQ4fJxgFDKkhDqhPOAZ/TAo1PhmSYIx8Dye/Bzt5S3mzNFj89R8vdhwU4xUeCn1ra3dwyJOCGfWHSZgOfIWsl1nlvM=" before_install: - curl -L https://github.com/arcnmx/ci/archive/0.1.tar.gz | tar -xzC $HOME && . $HOME/ci-0.1/src script: - cargo test - cargo build deploy: provider: script script: 'true' on: tags: true all_branches: true condition: "$TRAVIS_RUST_VERSION = stable" before_deploy: - cargo doc - cargo package after_deploy: - cargo pages-publish - cargo publish thread-scoped-1.0.2/COPYING01006440001750000175000000002032131210116170013476 0ustar0000000000000000Copyright (c) 2015 arcnmx Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. thread-scoped-1.0.2/Cargo.toml01006440001750000175000000001612131210116170014376 0ustar0000000000000000# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g. crates.io) dependencies # # If you believe there's an error in this file please file an # issue against the rust-lang/cargo repository. If you're # editing this file be aware that the upstream Cargo.toml # will likely look very different (and much more reasonable) [package] name = "thread-scoped" version = "1.0.2" authors = ["arcnmx", "The Rust Project Developers"] description = "Unsafe and deprecated std::thread::scoped" documentation = "http://arcnmx.github.io/thread-scoped-rs/thread_scoped/" readme = "README.md" keywords = ["thread", "scoped", "spawn"] license = "MIT/Apache-2.0" repository = "https://github.com/arcnmx/thread-scoped-rs" thread-scoped-1.0.2/Cargo.toml.orig01006440001750000175000000000577131210116170015346 0ustar0000000000000000[package] name = "thread-scoped" version = "1.0.2" authors = ["arcnmx", "The Rust Project Developers"] description = "Unsafe and deprecated std::thread::scoped" documentation = "http://arcnmx.github.io/thread-scoped-rs/thread_scoped/" repository = "https://github.com/arcnmx/thread-scoped-rs" readme = "README.md" keywords = ["thread", "scoped", "spawn"] license = "MIT/Apache-2.0" thread-scoped-1.0.2/README.md01006440001750000175000000002420131210116170013723 0ustar0000000000000000# thread-scoped [![travis-badge][]][travis] [![release-badge][]][cargo] [![docs-badge][]][docs] [![license-badge][]][license] A `std::thread::spawn()` that can access its current scope. Stable fork of the deprecated `std::thread::scoped()` ## Memory Unsafety This interface is inherently unsafe if the `JoinGuard` is allowed to leak without being dropped. See [rust-lang/rust#24292](https://github.com/rust-lang/rust/issues/24292) for more details. ## Alternatives This crate is only provided as a fallback mirror for legacy dependency on the deprecated `libstd` interface. Using a modern and safe API instead is recommended: - [crossbeam::scope](https://github.com/crossbeam-rs/crossbeam) [travis-badge]: https://img.shields.io/travis/arcnmx/thread-scoped-rs/master.svg?style=flat-square [travis]: https://travis-ci.org/arcnmx/thread-scoped-rs [release-badge]: https://img.shields.io/crates/v/thread_scoped.svg?style=flat-square [cargo]: https://crates.io/crates/thread-scoped [docs-badge]: https://img.shields.io/badge/API-docs-blue.svg?style=flat-square [docs]: http://arcnmx.github.io/thread-scoped-rs/thread_scoped/ [license-badge]: https://img.shields.io/badge/license-MIT-lightgray.svg?style=flat-square [license]: https://github.com/arcnmx/thread-scoped-rs/blob/master/COPYING thread-scoped-1.0.2/src/lib.rs01006440001750000175000000007015131210116170014354 0ustar0000000000000000#![deny(missing_docs)] //! A stable version of `std::thread::scoped` //! //! ## Warning //! //! This is inherently unsafe if the `JoinGuard` is allowed to leak without being dropped. //! See [rust-lang/rust#24292](https://github.com/rust-lang/rust/issues/24292) for more details. use std::marker::PhantomData; use std::thread::{spawn, JoinHandle, Thread}; use std::mem::{transmute, forget}; /// A RAII guard for that joins a scoped thread upon drop /// /// # Panics /// /// `JoinGuard` will panic on join or drop if its owned thread panics #[must_use = "thread will be immediately joined if `JoinGuard` is not used"] pub struct JoinGuard<'a, T: Send + 'a> { inner: Option>, _marker: PhantomData<&'a T>, } unsafe impl<'a, T: Send + 'a> Sync for JoinGuard<'a, T> {} impl<'a, T: Send + 'a> JoinGuard<'a, T> { /// Provides the backing `Thread` object pub fn thread(&self) -> &Thread { &self.inner.as_ref().unwrap().thread() } /// Joins the guarded thread and returns its result /// /// # Panics /// /// `join()` will panic if the owned thread panics pub fn join(mut self) -> T { match self.inner.take().unwrap().join() { Ok(res) => unsafe { *res.into_inner() }, Err(_) => panic!("child thread {:?} panicked", self.thread()), } } } /// Detaches a child thread from its guard pub trait ScopedDetach { /// Detaches a child thread from its guard /// /// Note: Only valid for the 'static lifetime fn detach(self); } impl ScopedDetach for JoinGuard<'static, T> { fn detach(mut self) { let _ = self.inner.take(); } } impl<'a, T: Send + 'a> Drop for JoinGuard<'a, T> { fn drop(&mut self) { self.inner.take().map(|v| if v.join().is_err() { panic!("child thread {:?} panicked", self.thread()); }); } } /// Spawns a new scoped thread pub unsafe fn scoped<'a, T, F>(f: F) -> JoinGuard<'a, T> where T: Send + 'a, F: FnOnce() -> T, F: Send + 'a { let f = BoxedThing::new(f); JoinGuard { inner: Some(spawn(move || BoxedThing::new(f.into_inner::()()) )), _marker: PhantomData, } } struct BoxedThing(usize); impl BoxedThing { fn new(v: T) -> Self { let mut b = Box::new(v); let b_ptr = &mut *b as *mut _ as usize; forget(b); BoxedThing(b_ptr) } unsafe fn into_inner(self) -> Box { transmute(self.0 as *mut T) } } #[cfg(test)] mod tests { use std::thread::sleep; use std::time::Duration; use super::scoped; #[test] fn test_scoped_stack() { unsafe { let mut a = 5; scoped(|| { sleep(Duration::from_millis(100)); a = 2; }).join(); assert_eq!(a, 2); } } #[test] fn test_join_success() { unsafe { assert!(scoped(move|| -> String { "Success!".to_string() }).join() == "Success!"); } } #[test] fn test_scoped_success() { unsafe { let res = scoped(move|| -> String { "Success!".to_string() }).join(); assert!(res == "Success!"); } } #[test] #[should_panic] fn test_scoped_panic() { unsafe { scoped(|| panic!()).join(); } } #[test] #[should_panic] fn test_scoped_implicit_panic() { unsafe { let _ = scoped(|| panic!()); } } }