[SOLVED] Using a single instance of a struct between multiple other structs?


#1

Example:

extern crate ws;

struct Test {
	a: u32
}

struct Daemon {
	socket: ws::Sender,
	test: Test
}

impl ws::Handler for Daemon {

	fn on_message(&mut self, msg: ws::Message) -> ws::Result<()> {
		self.test.a += 1;
		self.socket.send(self.test.a.to_string())
	}
}

fn main() {
	ws::listen("localhost:1234", |out| Daemon {
		socket: out,
		test: Test {a: 0}
	}).unwrap();
}

This causes each client connected to the websocket server to have a different instance of Test. However, I’m needing every client to have the same instance. I seem to be unable to just pass it in.


#2

Take a look at the “Shared State” section of the Book: https://doc.rust-lang.org/book/second-edition/ch16-03-shared-state.html

Let me know if you have any questions!