Skip to content

Instantly share code, notes, and snippets.

Created July 28, 2016 10:07
Show Gist options
  • Save anonymous/428ce8460547729c42fa11d9c76b02e1 to your computer and use it in GitHub Desktop.
Save anonymous/428ce8460547729c42fa11d9c76b02e1 to your computer and use it in GitHub Desktop.
Shared via Rust Playground
/*
struct Foo {
data: Vec<u8>
}
impl Foo {
fn p(&mut self, c: usize) {
self.data.insert(0, (c + 42) as u8);
}
fn n<F>(&self, f: F) -> usize where
F: Fn(&[u8]) -> &[u8] {
f(self.data.as_ref()).len()
}
fn a<F>(&self, f: F) -> &[u8] where
F: Fn(&[u8]) -> &[u8]
{
f(self.data.as_ref())
}
fn fa<F>(&mut self, f: &F) -> &[u8] where
F: Fn(&[u8]) -> &[u8]
{
let s = self.n(f);
self.p(s);
self.a(f)
}
}
*/
/*
struct Foo<O> {
data: Vec<O>
}
impl<O,> Foo<O> {
fn d(&self) -> &[O] {
self.data.as_ref()
}
fn p(&mut self, c: usize) {
self.data.truncate(c);
}
fn n<'b, F, CO>(&'b self, f: F) -> usize where
F: Fn(&'b [O]) -> (&'b [O], CO) {
f(self.d()).0.len()
}
fn a<'b, F, CO>(&'b self, f: F) -> CO where
F: Fn(&'b [O]) -> (&'b [O], CO) {
f(self.d()).1
}
fn fa<'b, F, CO>(&'b mut self, f: &F) -> CO where
F: Fn(&'b [O]) -> (&'b [O], CO) {
let s = self.n(f);
self.p(s);
self.a(f)
}
}
*/
struct Foo<O> {
data: Vec<O>
}
impl<O,> Foo<O> {
fn d(&self) -> &[O] {
self.data.as_ref()
}
fn p(&mut self, c: usize) {
self.data.truncate(c);
}
fn n<'b, F, CO>(&'b self, f: F) -> usize where
F: Fn(&[O]) -> (&[O], CO) {
f(self.d()).0.len()
}
fn a<'b, F, CO>(&'b self, f: F) -> CO where
F: Fn(&[O]) -> (&[O], CO) {
f(self.d()).1
}
fn fa<'b, F, CO>(&'b mut self, f: &F) -> CO where
F: Fn(&[O]) -> (&[O], CO) {
let s = self.n(f);
self.p(s);
self.a(f)
}
}
fn x<F>(d: &[u8], f: F) -> &[u8] where
F: Fn(&[u8]) -> (&[u8], &[u8]) {
f(d).1
}
fn main() {
fn z(i: &[u8]) -> (&[u8], &[u8]) {
(&i[0..5], &i[2..4])
}
fn y(i: &[u8]) -> (&[u8], u32) {
(&i[0..5], 42)
}
let mut f = Foo { data: vec![0, 1, 2, 3, 4] };
//println!("x: {:?}", x(&[1, 2, 3, 4, 5], z));
//println!("x: {:?}", x(&[3, 4, 5, 6, 7], z));
println!("bar: {:?}", f.fa(&z));
println!("bar: {:?}", f.fa(&y));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment