Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created December 20, 2022 11:56
Show Gist options
  • Save rust-play/504213373e511d375ab47419f0f1815d to your computer and use it in GitHub Desktop.
Save rust-play/504213373e511d375ab47419f0f1815d to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
use std::io::Read;
use std::io::Cursor;
struct B(String);
struct RefPairA<'b>(&'b B, String);
trait GetPair {
fn pair(&self) -> (&str, &str);
}
impl<'b> GetPair for RefPairA<'b> {
fn pair(&self) -> (&'b str, &str) {
(&self.0.0, &self.1)
}
}
fn get_data_a(p: &RefPairA) -> impl Read {
let (b, s) = p.pair();
Cursor::new(format!("{}/{}", b, s).into_bytes())
}
fn get_data(p: &impl GetPair) -> impl Read {
let (b, s) = p.pair();
Cursor::new(format!("{}/{}", b, s).into_bytes())
}
fn data_a() -> impl Read {
let b = B("foo".to_owned());
let a = RefPairA(&b, "bar".to_owned());
get_data_a(&a)
}
fn data() -> impl Read {
let b = B("foo".to_owned());
let a = RefPairA(&b, "bar".to_owned());
get_data(&a)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment