portability/libportability-gfx/src/handle.rs

48 lines
911 B
Rust
Raw Normal View History

use VK_NULL_HANDLE;
2017-09-07 11:34:41 +10:00
use std::{fmt, ops};
#[repr(C)]
2017-11-14 14:55:43 +11:00
pub struct Handle<T>(*mut T);
2017-09-07 11:34:41 +10:00
impl<T> Handle<T> {
pub fn new(value: T) -> Self {
2017-11-14 14:55:43 +11:00
let ptr = Box::into_raw(Box::new(value));
Handle(ptr)
2017-09-07 11:34:41 +10:00
}
pub fn unwrap(self) -> Box<T> {
2017-11-14 14:55:43 +11:00
unsafe { Box::from_raw(self.0) }
2017-09-07 11:34:41 +10:00
}
pub fn is_null(&self) -> bool {
self.0 == VK_NULL_HANDLE as *mut T
}
2017-09-07 11:34:41 +10:00
}
impl<T> Clone for Handle<T> {
fn clone(&self) -> Self {
2017-11-14 14:55:43 +11:00
Handle(self.0)
2017-09-07 11:34:41 +10:00
}
}
impl<T> Copy for Handle<T> {}
impl<T> ops::Deref for Handle<T> {
type Target = T;
fn deref(&self) -> &T {
unsafe { &*self.0 }
2017-09-07 11:34:41 +10:00
}
}
impl<T> ops::DerefMut for Handle<T> {
fn deref_mut(&mut self) -> &mut T {
2017-11-14 14:55:43 +11:00
unsafe { &mut *self.0 }
2017-09-07 11:34:41 +10:00
}
}
impl<T> fmt::Debug for Handle<T> {
2017-11-14 14:55:43 +11:00
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(formatter, "Handle({:p})", self.0)
2017-09-07 11:34:41 +10:00
}
}