Skip to content

Instantly share code, notes, and snippets.

@y21
Created March 19, 2021 07:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save y21/d6cba7f2977d41946d0e623cc15c7754 to your computer and use it in GitHub Desktop.
Save y21/d6cba7f2977d41946d0e623cc15c7754 to your computer and use it in GitHub Desktop.
ManualDrop
use std::ops::{Drop, Deref};
#[derive(Debug)]
struct ManualDrop<T>(*mut T);
impl<T> ManualDrop<T> {
pub fn new(data: T) -> ManualDrop<T> {
Self(Box::into_raw(Box::new(data)))
}
}
impl<T> Deref for ManualDrop<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
unsafe { &*self.0 }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment