Skip to content

Instantly share code, notes, and snippets.

@jpastuszek
Forked from anonymous/playground.rs
Last active August 16, 2016 09:22
Show Gist options
  • Save jpastuszek/6760c902630b021e209dc09b26ba7c85 to your computer and use it in GitHub Desktop.
Save jpastuszek/6760c902630b021e209dc09b26ba7c85 to your computer and use it in GitHub Desktop.
Shared via Rust Playground
use std::ops::Deref;
#[derive(Debug)]
struct S {
i: [u8]
}
impl S {
fn from_bytes(bytes: &[u8]) -> &S {
unsafe { &*((bytes as *const [u8]) as *const S)}
}
fn as_bytes(&self) -> &[u8] {
//unsafe { &*((self as *const S) as *const [u8])}
self
}
}
impl Deref for S {
type Target = [u8];
fn deref(&self) -> &[u8] {
&self.i
}
}
fn main() {
let b = "Host: ".as_bytes();
let s = S::from_bytes(b);
let c: &[u8] = s;
println!("{:?}", s);
println!("{:?}", s.as_bytes());
println!("{:?}", c);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment