This repository has been archived by the owner on Feb 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PrimitiveArray -> MutableArray
(#794)
- Loading branch information
Showing
11 changed files
with
252 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// this code is in its own module so that inner types are not accessible | ||
// as that might break invariants assumptions | ||
use crate::types::NativeType; | ||
use std::mem::ManuallyDrop; | ||
use std::ops::{Deref, DerefMut}; | ||
|
||
/// Holds a `Vec` that may hold a pointer that is not allocated by `Vec`. It is therefore not | ||
/// safe to deallocate the inner type naively | ||
/// | ||
/// This struct exists to avoid holding an `enum` of a `Vec` or a foreign pointer, whose `deref` | ||
/// is known to be least 50% more expensive than the deref of a `Vec`. | ||
/// | ||
/// # Safety | ||
/// | ||
/// it is unsafe to take and drop the inner value of `MaybeForeign` | ||
/// Doing so is only allowed if the `Vec` was created from the native `Vec` allocator | ||
pub(super) struct MaybeForeign<T: NativeType> { | ||
inner: ManuallyDrop<Vec<T>>, | ||
} | ||
|
||
impl<T: NativeType> MaybeForeign<T> { | ||
#[inline] | ||
pub(super) fn new(data: Vec<T>) -> Self { | ||
Self { | ||
inner: ManuallyDrop::new(data), | ||
} | ||
} | ||
|
||
/// # Safety | ||
/// This function may only be called if the inner `Vec<T>` was allocated | ||
/// by `Vec<T, A>` allocator `A`. | ||
#[inline] | ||
pub(super) unsafe fn drop_local(&mut self) { | ||
let data = std::mem::take(&mut self.inner); | ||
let _data = ManuallyDrop::into_inner(data); | ||
} | ||
|
||
/// # Safety | ||
/// This function may only be called if the inner `Vec<T>` was allocated | ||
/// in Rust and the default `Vec<T, A>` allocator `A`. | ||
/// Otherwise, users may reallocate `Vec`, which is unsound | ||
#[inline] | ||
pub(super) unsafe fn mut_vec(&mut self) -> &mut Vec<T> { | ||
self.inner.deref_mut() | ||
} | ||
} | ||
|
||
impl<T: NativeType> Deref for MaybeForeign<T> { | ||
type Target = [T]; | ||
|
||
#[inline] | ||
fn deref(&self) -> &Self::Target { | ||
&self.inner | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,5 +4,6 @@ | |
mod immutable; | ||
|
||
pub(crate) mod bytes; | ||
mod foreign; | ||
|
||
pub use immutable::Buffer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.