Skip to content

Instantly share code, notes, and snippets.

@Lytigas

Lytigas/file1.rs Secret

Created February 27, 2019 20:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lytigas/dc26f5c7a5f48676c46eca7d639b1690 to your computer and use it in GitHub Desktop.
Save Lytigas/dc26f5c7a5f48676c46eca7d639b1690 to your computer and use it in GitHub Desktop.
struct SharedState {
data: HashMap<KeyType, ValueType>,
msg_buf: Vec<Message<'static>>,
}
struct VecHolder<'a, 'b: 'a>(&'a mut Vec<Message<'b>>);
impl<'a, 'b: 'a> VecHolder<'a, 'b> {
pub fn new<'o>(other: &'a mut Vec<Message<'o>>) -> Self {
Self(unsafe { std::mem::transmute(other) })
}
pub fn inner<'d, 'e: 'd>(&'e mut self) -> &'d mut Vec<Message<'b>> {
self.0
}
}
impl<'a, 'b> Drop for VecHolder<'a, 'b> {
fn drop(&mut self) {
self.0.clear();
}
}
impl SharedState {
// other methods
pub fn dispatch_events<'a>(
&'a mut self,
) -> WsResult<UpdateTick> {
let mut msg_buf: VecHolder<'a, 'a> = VecHolder::new(&mut self.msg_buf);
// push a bunch of messages
msg_buf.inner().push(/* messages */);
handle_msgs(msg_buf.inner())?;
Ok(self.latest_update)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment