Skip to content

Instantly share code, notes, and snippets.

@kornysietsma
Created April 26, 2019 07:51
Show Gist options
  • Save kornysietsma/2783b143360f06ab6faa96280b6ff20b to your computer and use it in GitHub Desktop.
Save kornysietsma/2783b143360f06ab6faa96280b6ff20b to your computer and use it in GitHub Desktop.
experimenting with rust traits and PartialEq
#![warn(clippy::all)]
#![allow(dead_code)]
use serde::ser::SerializeStruct;
use serde::ser::SerializeTuple;
use serde::{Serialize, Serializer};
use std::fmt;
use std::collections::HashMap;
use std::fmt::Debug;
trait DataPayload: erased_serde::Serialize {}
impl Serialize for DataPayload {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut state = serializer.serialize_tuple(1)?;
state.serialize_element(self)?;
state.end()
}
}
impl DataPayload {
fn slow_to_string(&self) -> String {
serde_json::to_string(&self).unwrap()
}
}
// ONLY FOR TESTING! Horribly slow...
impl<'a, 'b> PartialEq<DataPayload + 'b> for DataPayload + 'a {
fn eq(&self, other: &(DataPayload + 'b)) -> bool {
let self_str = &self.slow_to_string();
let other_str = &other.slow_to_string();
self_str == other_str
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment