Created
July 22, 2019 05:57
-
-
Save rust-play/9c5ec7153a788afd490aff1ad2a5ec80 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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