Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extern crate libc;
- use std::marker::PhantomData;
- use libc::{malloc, free, c_void};
- trait Trace {}
- struct MyTrace<'b> {
- #[allow(dead_code)]
- buf: *mut c_void,
- _phantom: PhantomData<&'b c_void>,
- }
- impl<'b> MyTrace<'b> {
- fn new() -> Self {
- Self{buf: unsafe{malloc(128)}, _phantom: PhantomData}
- }
- }
- impl<'b> Trace for MyTrace<'b> {}
- impl<'b> Drop for MyTrace<'b> {
- fn drop(&mut self) {
- unsafe{free(self.buf)};
- }
- }
- trait Tracer {
- fn start(&mut self);
- fn stop(&mut self) -> Box<Trace>;
- }
- struct MyTracer<'b> {
- trace: Option<MyTrace<'b>>,
- }
- impl<'b> MyTracer<'b> {
- fn new() -> Self {
- Self{trace: None}
- }
- }
- impl<'b> Tracer for MyTracer<'b> {
- fn start(&mut self) {
- self.trace = Some(MyTrace::new());
- // Pretend the buffer is mutated in C here...
- }
- fn stop(&mut self) -> Box<Trace> {
- Box::new(self.trace.take().unwrap())
- }
- }
- fn main() {
- let mut tracer = MyTracer::new();
- tracer.start();
- let _trace = tracer.stop();
- println!("Hello, world!");
- }
Add Comment
Please, Sign In to add comment