vorner / arc-swap

Support atomic operations on Arc itself

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

cannot get the `rcu` API work

lnshi opened this issue · comments

Thanks a lot for the earlier support.

I am still learning rust, and cannot get the rcu API work, pls help, my setup like below:

#[derive(Default, Debug)]
struct Config {
	a: HashTrieMap<String, HashTrieMap<String, String>>,
	b: HashTrieMap<String, String>,
	c: usize,
}

let cfg = Config{};
let aswap_cfg = arc_swap::ArcSwap::from(Arc::new(cfg));

and i try to implement an update API by using rcu for the aswap_cfg, like below:

// self is the instance of `struct ASwapConfig(ArcSwap<Config>)`
fn update_cfg(&self, item: String) bool {
  let snapshot = self.0.load()

  self.0.rcu(|snapshot| {
    // update the snapshot with the `item`

    snapshot
  });
}

but got the error tells that:

error: the trait bound `Arc<Config>: From<&Arc<Config>>` is not satisfied

the trait `From<&Arc<Config>>` is not implemented for `Arc<Config>`

Is this the correct approach?

If yes how can i implement the From trait for Arc<Config> when the Arc is defined in another crate?

No, you don't implement the From trait.

You just need to return an owned instance ‒ clone it. It's even in the example: https://docs.rs/arc-swap/latest/arc_swap/struct.ArcSwapAny.html#method.rcu

i see, thanks for the help, will close.