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

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.

Take a look at the "Shared State" section of the Book: Shared State - The Rust Programming Language

Let me know if you have any questions!

1 Like