Skip to content

Fixes for latest rust #7

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

Merged
merged 1 commit into from
Mar 11, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl<T: Send + Sync> Thunk<T> {
/// });
/// assert_eq!(**reff, 7u);
/// ```
pub fn new<F>(producer: F) -> Thunk<T>
pub fn new<F: 'static>(producer: F) -> Thunk<T>
where F: Send + Sync + FnOnce() -> T {
Thunk {
inner: OnceMutex::new(Unevaluated(Producer::new(producer)))
Expand Down Expand Up @@ -113,7 +113,7 @@ struct Producer<T> {
}

impl<T> Producer<T> {
fn new<F: Send + Sync + FnOnce() -> T>(f: F) -> Producer<T> {
fn new<F: 'static + Send + Sync + FnOnce() -> T>(f: F) -> Producer<T> {
Producer {
inner: Box::new(move |()| {
f()
Expand All @@ -131,4 +131,3 @@ enum Inner<T> {
EvaluationInProgress,
Unevaluated(Producer<T>)
}

21 changes: 12 additions & 9 deletions tests/single.rs
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
#![feature(plugin, std_misc)]
#![feature(plugin)]
#![plugin(stainless)]

#[macro_use]
extern crate lazy;

#[plugin]
extern crate stainless;

pub use lazy::single::Thunk;
pub use std::sync::{Arc, Mutex};
pub use std::thread::{self, Thread};
pub use std::thread;

describe! thunk {
it "should evaluate when accessed" {
Expand All @@ -19,7 +17,10 @@ describe! thunk {
it "should evaluate just once" {
let counter = Arc::new(Mutex::new(0));
let counter_clone = counter.clone();
let val = lazy!(*counter.lock().unwrap() += 1);
let val = lazy!({
let mut data = counter.lock().unwrap();
*data += 1;
});
*val;
*val;
assert_eq!(*counter_clone.lock().unwrap(), 1);
Expand All @@ -28,7 +29,10 @@ describe! thunk {
it "should not evaluate if not accessed" {
let counter = Arc::new(Mutex::new(0));
let counter_clone = counter.clone();
let _val = lazy!(*counter.lock().unwrap() += 1);
let _val = lazy!({
let mut data = counter.lock().unwrap();
*data += 1;
});
assert_eq!(*counter_clone.lock().unwrap(), 0);
}

Expand All @@ -50,7 +54,7 @@ describe! thunk {
it "should drop internal data just once" {
let counter = Arc::new(Mutex::new(0));
let counter_clone = counter.clone();
let result = Thread::scoped(move || {
let result = thread::spawn(move || {
let value = Dropper(counter_clone);
let t = Thunk::<()>::new(move || {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's no longer possible to handle panics from scoped threads (PR #22435). I'm not entirely sure if the panic was relevant to the test logic.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the panic is actually quite important, since this is testing the behavior of a thunk under a panic.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, although I don't know how to fix it.

// Get a reference so value is captured.
Expand Down Expand Up @@ -79,4 +83,3 @@ impl Drop for Dropper {
*count.lock().unwrap() += 1;
}
}

21 changes: 12 additions & 9 deletions tests/sync.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
#![feature(plugin, std_misc, io)]
#![feature(plugin, std_misc, old_io)]
#![plugin(stainless)]

#[macro_use]
extern crate lazy;

#[plugin]
extern crate stainless;

pub use lazy::sync::Thunk;
pub use std::sync::{Arc, Barrier, Mutex};
pub use std::{old_io, time};
pub use std::thread::Thread;
pub use std::thread;

describe! sync {
it "should evaluate when accessed" {
Expand All @@ -20,7 +18,10 @@ describe! sync {
it "should evaluate just once" {
let counter = Arc::new(Mutex::new(0));
let counter_clone = counter.clone();
let val = sync_lazy!(*counter.lock().unwrap() += 1);
let val = sync_lazy!({
let mut data = counter.lock().unwrap();
*data += 1;
});
*val;
*val;
assert_eq!(*counter_clone.lock().unwrap(), 1);
Expand All @@ -29,7 +30,10 @@ describe! sync {
it "should not evaluate if not accessed" {
let counter = Arc::new(Mutex::new(0));
let counter_clone = counter.clone();
let _val = sync_lazy!(*counter.lock().unwrap() += 1);
let _val = sync_lazy!({
let mut data = counter.lock().unwrap();
*data += 1;
});
assert_eq!(*counter_clone.lock().unwrap(), 0);
}

Expand All @@ -46,7 +50,7 @@ describe! sync {
let data_worker = data.clone();

// Worker task.
Thread::spawn(move || {
thread::spawn(move || {
data_worker.force();
});

Expand All @@ -61,4 +65,3 @@ describe! sync {
}
}
}