Skip to content

Commit

Permalink
Use PhantomData in MappedAllocationSlab and fix merge (#167)
Browse files Browse the repository at this point in the history
* use PhantomData and fix merge

* fmt

* make clippy happy

* fmt 2
  • Loading branch information
fu5ha committed May 12, 2023
1 parent 006ade8 commit 614f915
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/vulkan/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub use visualizer::AllocatorVisualizer;
use super::allocator;
use super::allocator::AllocationType;
use ash::vk;
use core::marker::PhantomData;
use log::{debug, Level};
use std::fmt;

Expand Down Expand Up @@ -177,7 +178,10 @@ impl Allocation {
/// See the note about safety in [the documentation of Allocation][Allocation#safety]
///
/// [`Slab`]: presser::Slab
pub fn try_as_mapped_slab(&mut self) -> Option<MappedAllocationSlab<'_>> {
// best to be explicit where the lifetime is coming from since we're doing unsafe things
// and relying on an inferred liftime type in the PhantomData below
#[allow(clippy::needless_lifetimes)]
pub fn try_as_mapped_slab<'a>(&'a mut self) -> Option<MappedAllocationSlab<'a>> {
let mapped_ptr = self.mapped_ptr()?.cast().as_ptr();

if self.size > isize::MAX as _ {
Expand All @@ -188,7 +192,7 @@ impl Allocation {
let size = self.size as usize;

Some(MappedAllocationSlab {
_borrowed_alloc: self,
_borrowed_alloc: PhantomData,
mapped_ptr,
size,
})
Expand Down Expand Up @@ -273,7 +277,7 @@ impl Default for Allocation {
///
/// This type should be acquired by calling [`Allocation::try_as_mapped_slab`].
pub struct MappedAllocationSlab<'a> {
_borrowed_alloc: &'a mut Allocation,
_borrowed_alloc: PhantomData<&'a mut Allocation>,
mapped_ptr: *mut u8,
size: usize,
}
Expand All @@ -298,13 +302,15 @@ unsafe impl presser::Slab for Allocation {
fn base_ptr(&self) -> *const u8 {
self.mapped_ptr
.expect("tried to use a non-mapped Allocation as a Slab")
.0
.as_ptr()
.cast()
}

fn base_ptr_mut(&mut self) -> *mut u8 {
self.mapped_ptr
.expect("tried to use a non-mapped Allocation as a Slab")
.0
.as_ptr()
.cast()
}
Expand Down

0 comments on commit 614f915

Please sign in to comment.