Skip to content

Instantly share code, notes, and snippets.

@crmaxx
Last active March 12, 2018 12:52
Show Gist options
  • Save crmaxx/89080996aa542b93085b74ff3f4d9f39 to your computer and use it in GitHub Desktop.
Save crmaxx/89080996aa542b93085b74ff3f4d9f39 to your computer and use it in GitHub Desktop.
example for trait from Telegram
use std::ffi::CString;
trait ReadByteString {
fn read_string_zero(&self) -> Option<CString> ;
}
impl ReadByteString for [u8] {
fn read_string_zero(&self) -> Option<CString> {
let pos = self.iter().position(|&x| x == 0 )?;
CString::new(&self[..pos]).ok()
}
}
fn main() {
let res = b"hello\0hehe"[..].read_string_zero();
println!("{:?}", res);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment