admin管理员组文章数量:1124655
I'm trying to write a simple (I think!) synchronous, single-threaded publish-subscribe system in Rust. I want a centralized event router to which I can attach and remove callbacks dynamically. I'm not sure if I'm just approaching this wrong or if it's actually not possible, so I'm looking for advice.
Here's a trimmed-down sketch:
struct EventListener {}
impl EventListener {
fn on_event(&self, event: &str) {
println!("{}", event);
}
}
struct EventRouter {
// TODO: How can I express the necessary lifetime relationship between the EventRouter and listeners?
listeners: Vec<&EventListener>
}
impl EventRouter {
fn add_listener(&mut self, listener: &EventListener) {
self.listeners.push(listener);
}
// A stand-in for a way to remove listeners dynamically
fn clear(&mut self) {
self.listeners.clear();
}
fn send(&self, event: &str) {
for listener in &self.listeners {
listener.on_event(event);
}
}
}
fn main() {
let mut router = EventRouter { listeners: Vec::new() };
{
let listener = EventListener {};
router.add_listener(&listener);
router.send("Hello, world!");
router.clear();
}
router.send("Hello, world!");
}
How can I express to Rust that all of the EventListener
references held by the EventRouter
have lifetimes that are a) within the EventRouter
lifetime b) potentially all different from one another? Is this simply not possible to express? I'm open to solutions that use things like channels or tokio
, as long as I can get the qualities I described initially.
(Also, I recognize that I might ultimately be better served by abandoning the single-threaded/synchronous constraint, but I'm currently exploring translating an existing system with these qualities to Rust and I'm curious about how/if this can work.)
I'm trying to write a simple (I think!) synchronous, single-threaded publish-subscribe system in Rust. I want a centralized event router to which I can attach and remove callbacks dynamically. I'm not sure if I'm just approaching this wrong or if it's actually not possible, so I'm looking for advice.
Here's a trimmed-down sketch:
struct EventListener {}
impl EventListener {
fn on_event(&self, event: &str) {
println!("{}", event);
}
}
struct EventRouter {
// TODO: How can I express the necessary lifetime relationship between the EventRouter and listeners?
listeners: Vec<&EventListener>
}
impl EventRouter {
fn add_listener(&mut self, listener: &EventListener) {
self.listeners.push(listener);
}
// A stand-in for a way to remove listeners dynamically
fn clear(&mut self) {
self.listeners.clear();
}
fn send(&self, event: &str) {
for listener in &self.listeners {
listener.on_event(event);
}
}
}
fn main() {
let mut router = EventRouter { listeners: Vec::new() };
{
let listener = EventListener {};
router.add_listener(&listener);
router.send("Hello, world!");
router.clear();
}
router.send("Hello, world!");
}
How can I express to Rust that all of the EventListener
references held by the EventRouter
have lifetimes that are a) within the EventRouter
lifetime b) potentially all different from one another? Is this simply not possible to express? I'm open to solutions that use things like channels or tokio
, as long as I can get the qualities I described initially.
(Also, I recognize that I might ultimately be better served by abandoning the single-threaded/synchronous constraint, but I'm currently exploring translating an existing system with these qualities to Rust and I'm curious about how/if this can work.)
Share Improve this question asked 2 days ago abinghamabingham 1,3361 gold badge11 silver badges18 bronze badges 7 | Show 2 more comments1 Answer
Reset to default 0Based on comments, it seems clear that what I’m trying to do simply isn’t possible with Rust. The lifetimes of the references I’m trying to use can’t be proven safe, so Rust won’t allow it. I need to switch to some design involving heap allocations and moves/copies of some data.
本文标签:
版权声明:本文标题:How can I write a synchronous pub-sub system where subscribers can be added and removed dynamically in rust? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736646014a1946105.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
Box<EventListener>
orRc<EventListener>
. – Jmb Commented 2 days agoBox
or shared ownership withRc
). – Jmb Commented 2 days ago