-
-
Save rust-play/c929dd0bfcf378abb0ba1668589e02af 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 serde::Deserialize; | |
use serde_json; | |
use std::marker::PhantomData; | |
#[derive(Deserialize, Debug)] | |
struct Ping<'a> { | |
#[serde(borrow)] | |
ping: &'a str, | |
} | |
fn handle<'a: 'de, 'de, T: Deserialize<'de>, Callback: Fn(T)>(msg: &'a str, callback: Callback) { | |
let res: T = serde_json::from_str(&msg).unwrap(); | |
callback(res); | |
} | |
fn main() { | |
let callback = |p: Ping| println!("{:?}", p); | |
let msg = format!("{{\"ping\":\"{}\"}}", "abcdefg"); | |
handle(&msg, callback); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment