Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use PhantomData in MappedAllocationSlab and fix merge #167

Merged
merged 4 commits into from
May 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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