Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created September 20, 2019 20:55
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/ff0592552f65e8fa205d4a4af0ba3bc0 to your computer and use it in GitHub Desktop.
Save rust-play/ff0592552f65e8fa205d4a4af0ba3bc0 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
fn main() {
foo("abc");
}
struct Value();
struct StrDeserializer<'a>(&'a str);
impl<'a> StrDeserializer<'a> {
fn new(s: &'a str) -> Self {
StrDeserializer(s)
}
}
trait Deserializer { }
impl Deserializer for Value { }
impl Deserializer for &Value { }
impl<'a> Deserializer for &mut StrDeserializer<'a> { }
// I want this function to remain unchanged
fn gets_called<D: Deserializer>(_d: D) { }
// I want to change function this into something that can take both &str and Value (or &Value)
fn foo(s: &str) {
let mut de = StrDeserializer::new(s);
gets_called(&mut de);
}
fn foo2(v: &Value) {
gets_called(v);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment