Created
May 30, 2017 02:17
-
-
Save anonymous/61d033eba0806f2785ecfa655cfa9286 to your computer and use it in GitHub Desktop.
Rust code shared from the 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
extern crate serde; | |
#[macro_use] | |
extern crate serde_derive; | |
extern crate serde_json; | |
use std::collections::HashMap; | |
use serde::{Serialize, Serializer}; | |
fn main() { | |
let mut map: HashMap<String, String> = HashMap::new(); | |
map.insert("hello".into(), "world".into()); | |
let data = ApiResult::Success(map.keys()); | |
println!("{}", serde_json::to_string(&data).unwrap()); | |
} | |
#[derive(Serialize)] | |
#[serde(bound(serialize = "T: Clone + Iterator, T::Item: Serialize"))] | |
pub enum ApiResult<'a, T> { | |
Failure { code: u32, errors: Vec<&'a str> }, | |
Success(#[serde(serialize_with = "serialize_iter")] T), | |
} | |
fn serialize_iter<T, S>(iter: &T, serializer: S) -> Result<S::Ok, S::Error> | |
where T: Clone + Iterator, | |
T::Item: Serialize, | |
S: Serializer | |
{ | |
serializer.collect_seq(iter.clone()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment