You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In Moka v0.5.0 and v0.5.1, the following code fragments for future::Cache will compile. However Moka should reject them as they can lead undefined behavior (UB) in safe Rust.
Example 1
use moka::future::Cache;use std::rc::Rc;#[tokio::main]asyncfnmain(){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,asyncmove{// 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);}
Example 2
use moka::future::Cache;#[tokio::main]asyncfnmain(){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);}
So future::Cache's get_or_insert_with() and get_or_try_insert_with() need extra bounds Send and 'static on the init future.
The text was updated successfully, but these errors were encountered:
In Moka v0.5.0 and v0.5.1, the following code fragments for
future::Cache
will compile. However Moka should reject them as they can lead undefined behavior (UB) in safe Rust.Example 1
Example 2
So
future::Cache
'sget_or_insert_with()
andget_or_try_insert_with()
need extra boundsSend
and'static
on theinit
future.The text was updated successfully, but these errors were encountered: