-
-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from moka-rs/extra-bounds-on-future
Add `Send` and `'static` bounds to the init futures of `get_or_(try_)insert_with`
- Loading branch information
Showing
11 changed files
with
159 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,40 @@ | ||
{ | ||
"rust-analyzer.cargo.features": ["future"], | ||
"cSpell.words": [ | ||
"CLFU", | ||
"Deque", | ||
"Deques", | ||
"Hasher", | ||
"Kawano", | ||
"MSRV", | ||
"Moka", | ||
"RUSTFLAGS", | ||
"Ristretto", | ||
"Tatsuya", | ||
"Upsert", | ||
"actix", | ||
"ahash", | ||
"benmanes", | ||
"CLFU", | ||
"clippy", | ||
"cpus", | ||
"deqs", | ||
"Deque", | ||
"Deques", | ||
"else's", | ||
"getrandom", | ||
"Hasher", | ||
"Kawano", | ||
"Moka", | ||
"mpsc", | ||
"MSRV", | ||
"nanos", | ||
"nocapture", | ||
"peekable", | ||
"preds", | ||
"reqwest", | ||
"Ristretto", | ||
"runtimes", | ||
"rustdoc", | ||
"RUSTFLAGS", | ||
"rustfmt", | ||
"semver", | ||
"structs", | ||
"Tatsuya", | ||
"thiserror", | ||
"toolchain", | ||
"trybuild", | ||
"unsync", | ||
"Upsert", | ||
"usize" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// https://github.com/moka-rs/moka/issues/31 | ||
|
||
use moka::future::Cache; | ||
use std::rc::Rc; | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
let cache: Cache<_, String> = Cache::new(100); | ||
|
||
// Rc is !Send. | ||
let data = Rc::new("zero".to_string()); | ||
let data1 = Rc::clone(&data); | ||
|
||
cache | ||
.get_or_insert_with(0, async move { | ||
// A data race may occur. | ||
// The async block can be executed by a different thread | ||
// but Rc's internal reference counters are not thread safe. | ||
data1.to_string() | ||
}) | ||
.await; | ||
|
||
println!("{:?}", data); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
error: future cannot be sent between threads safely | ||
--> $DIR/get_with_non_send_future.rs:15:10 | ||
| | ||
15 | .get_or_insert_with(0, async move { | ||
| ^^^^^^^^^^^^^^^^^^ future created by async block is not `Send` | ||
| | ||
= help: within `impl Future`, the trait `Send` is not implemented for `Rc<String>` | ||
note: captured value is not `Send` | ||
--> $DIR/get_with_non_send_future.rs:19:13 | ||
| | ||
19 | data1.to_string() | ||
| ^^^^^ has type `Rc<String>` which is not `Send` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// https://github.com/moka-rs/moka/issues/31 | ||
|
||
use moka::future::Cache; | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
let cache: Cache<_, String> = Cache::new(100); | ||
|
||
let data = "zero".to_string(); | ||
{ | ||
// Not 'static. | ||
let data_ref = &data; | ||
|
||
cache | ||
.get_or_insert_with(0, async { | ||
// This may become a dangling pointer. | ||
// The async block can be executed by a different thread so | ||
// the captured reference `data_ref` may outlive its value. | ||
data_ref.to_string() | ||
}) | ||
.await; | ||
} | ||
|
||
println!("{:?}", data); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
error[E0597]: `data` does not live long enough | ||
--> $DIR/get_with_non_static_future.rs:12:24 | ||
| | ||
12 | let data_ref = &data; | ||
| ^^^^^ borrowed value does not live long enough | ||
... | ||
15 | .get_or_insert_with(0, async { | ||
| ____________________________________- | ||
16 | | // This may become a dangling pointer. | ||
17 | | // The async block can be executed by a different thread so | ||
18 | | // the captured reference `data_ref` may outlive its value. | ||
19 | | data_ref.to_string() | ||
20 | | }) | ||
| |_____________- argument requires that `data` is borrowed for `'static` | ||
... | ||
25 | } | ||
| - `data` dropped here while still borrowed | ||
|
||
error[E0373]: async block may outlive the current function, but it borrows `data_ref`, which is owned by the current function | ||
--> $DIR/get_with_non_static_future.rs:15:42 | ||
| | ||
15 | .get_or_insert_with(0, async { | ||
| __________________________________________^ | ||
16 | | // This may become a dangling pointer. | ||
17 | | // The async block can be executed by a different thread so | ||
18 | | // the captured reference `data_ref` may outlive its value. | ||
19 | | data_ref.to_string() | ||
| | -------- `data_ref` is borrowed here | ||
20 | | }) | ||
| |_____________^ may outlive borrowed value `data_ref` | ||
| | ||
= note: async blocks are not executed immediately and must either take a reference or ownership of outside variables they use | ||
help: to force the async block to take ownership of `data_ref` (and any other referenced variables), use the `move` keyword | ||
| | ||
15 | .get_or_insert_with(0, async move { | ||
16 | // This may become a dangling pointer. | ||
17 | // The async block can be executed by a different thread so | ||
18 | // the captured reference `data_ref` may outlive its value. | ||
19 | data_ref.to_string() | ||
20 | }) | ||
| |