Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created May 21, 2022 05:39
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/c929dd0bfcf378abb0ba1668589e02af to your computer and use it in GitHub Desktop.
Save rust-play/c929dd0bfcf378abb0ba1668589e02af to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
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