Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created July 22, 2019 05:57
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 rust-play/9c5ec7153a788afd490aff1ad2a5ec80 to your computer and use it in GitHub Desktop.
Save rust-play/9c5ec7153a788afd490aff1ad2a5ec80 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
use std::net::{SocketAddr, UdpSocket};
use std::sync::{Arc, Mutex};
use serde::Serialize;
use serde_json::Value;
#[derive(Serialize)]
pub struct Peer {
ip: SocketAddr,
}
pub fn foo(_socket: &UdpSocket, _packet: Value, target_addr: SocketAddr, peers: Arc<Mutex<Vec<Peer>>>) {
let lock = peers.lock().expect("cannot lock the peers");
if !lock.iter().find(|x| x.ip == target_addr).is_some() {
let _packet = write_packet(&*lock);
}
}
fn write_packet<T: Serialize>(_data: &T) -> String { serde_json::to_string(_data).unwrap() }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment