// Copyright 2018 Jacob Lifshay
use api;
use api_impl::{Device, Instance, PhysicalDevice, Queue};
+use buffer::Buffer;
+use device_memory::DeviceMemory;
+use sampler::Sampler;
+use sampler::SamplerYcbcrConversion;
+use shader_module::ShaderModule;
use std::fmt;
use std::marker::PhantomData;
use std::mem;
}
}
-#[repr(transparent)]
-pub struct OwnedHandle<T: HandleAllocFree>(T);
+pub struct OwnedHandle<T: HandleAllocFree>(NonNull<T::Value>);
impl<T: HandleAllocFree> OwnedHandle<T> {
pub fn new<I: Into<T::Value>>(v: I) -> Self {
- unsafe { OwnedHandle(T::allocate(v)) }
+ unsafe { OwnedHandle(T::allocate(v).get().unwrap()) }
}
- pub unsafe fn from(v: T) -> Self {
- OwnedHandle(v)
+ pub unsafe fn from(v: T) -> Option<Self> {
+ v.get().map(OwnedHandle)
}
- pub unsafe fn take(mut self) -> T {
- self.0.take()
+ pub unsafe fn take(self) -> T {
+ let retval = self.0;
+ mem::forget(self);
+ T::new(Some(retval))
}
- pub unsafe fn get_handle(&self) -> &T {
- &self.0
+ pub unsafe fn get_handle(&self) -> T {
+ T::new(Some(self.0))
}
}
impl<T: HandleAllocFree> Deref for OwnedHandle<T> {
type Target = T::Value;
fn deref(&self) -> &T::Value {
- unsafe { &*self.0.get().unwrap().as_ptr() }
+ unsafe { &*self.0.as_ptr() }
}
}
impl<T: HandleAllocFree> DerefMut for OwnedHandle<T> {
fn deref_mut(&mut self) -> &mut T::Value {
- unsafe { &mut *self.0.get().unwrap().as_ptr() }
+ unsafe { &mut *self.0.as_ptr() }
}
}
impl<T: HandleAllocFree> Drop for OwnedHandle<T> {
fn drop(&mut self) {
- if !self.0.is_null() {
- unsafe {
- self.0.take().free();
- }
+ unsafe {
+ T::new(Some(self.0)).free();
}
}
}
}
}
+#[derive(Copy, Clone)]
#[repr(transparent)]
-pub struct SharedHandle<T: Handle>(T);
+pub struct SharedHandle<T: Handle>(NonNull<T::Value>);
impl<T: Handle> SharedHandle<T> {
- pub unsafe fn from(v: T) -> Self {
- SharedHandle(v)
+ pub unsafe fn from(v: T) -> Option<Self> {
+ v.get().map(SharedHandle)
}
- pub unsafe fn take(mut self) -> T {
- self.0.take()
+ pub unsafe fn take(self) -> T {
+ T::new(Some(self.0))
}
- pub unsafe fn get_handle(&self) -> &T {
- &self.0
+ pub unsafe fn get_handle(&self) -> T {
+ T::new(Some(self.0))
}
}
impl<T: Handle> Deref for SharedHandle<T> {
type Target = T::Value;
fn deref(&self) -> &T::Value {
- unsafe { &*self.0.get().unwrap().as_ptr() }
+ unsafe { &*self.0.as_ptr() }
}
}
impl HandleAllocFree for VkFence {}
-pub struct DeviceMemory {}
-
pub type VkDeviceMemory = NondispatchableHandle<DeviceMemory>;
impl HandleAllocFree for VkDeviceMemory {}
-pub struct Buffer {}
-
pub type VkBuffer = NondispatchableHandle<Buffer>;
impl HandleAllocFree for VkBuffer {}
impl HandleAllocFree for VkImageView {}
-pub struct ShaderModule {}
-
pub type VkShaderModule = NondispatchableHandle<ShaderModule>;
impl HandleAllocFree for VkShaderModule {}
impl HandleAllocFree for VkDescriptorSetLayout {}
-pub struct Sampler {}
-
pub type VkSampler = NondispatchableHandle<Sampler>;
impl HandleAllocFree for VkSampler {}
impl HandleAllocFree for VkCommandPool {}
-pub struct SamplerYcbcrConversion {}
-
pub type VkSamplerYcbcrConversion = NondispatchableHandle<SamplerYcbcrConversion>;
impl HandleAllocFree for VkSamplerYcbcrConversion {}