Skip to content

Commit

Permalink
Impl unsafe deref_mut on IpcSharedMemory
Browse files Browse the repository at this point in the history
Signed-off-by: sagudev <[email protected]>
  • Loading branch information
sagudev committed Aug 20, 2024
1 parent ccf2aad commit d20de40
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/ipc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,24 @@ impl Deref for IpcSharedMemory {
}
}

impl IpcSharedMemory {
/// Returns a mutable reference to the deref of this [`IpcSharedMemory`].
///
/// # Safety
///
/// This is safe if there is only one reader/writer on the data.
/// User can achieve this by not cloning [`IpcSharedMemory`]
/// and serializing/deserializing only once.
#[inline]
pub unsafe fn deref_mut(&mut self) -> &mut [u8] {
if let Some(os_shared_memory) = &mut self.os_shared_memory {
os_shared_memory.deref_mut()
} else {
&mut []
}
}
}

impl<'de> Deserialize<'de> for IpcSharedMemory {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
Expand Down
10 changes: 10 additions & 0 deletions src/platform/inprocess/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,16 @@ impl Deref for OsIpcSharedMemory {
}
}

impl OsIpcSharedMemory {
#[inline]
pub unsafe fn deref_mut(&mut self) -> &mut [u8] {
if self.ptr.is_null() {
panic!("attempted to access a consumed `OsIpcSharedMemory`")
}
unsafe { slice::from_raw_parts_mut(self.ptr, self.length) }
}
}

impl OsIpcSharedMemory {
pub fn from_byte(byte: u8, length: usize) -> OsIpcSharedMemory {
let mut v = Arc::new(vec![byte; length]);
Expand Down
10 changes: 10 additions & 0 deletions src/platform/macos/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,16 @@ impl Deref for OsIpcSharedMemory {
}
}

impl OsIpcSharedMemory {
#[inline]
pub unsafe fn deref_mut(&mut self) -> &mut [u8] {
if self.ptr.is_null() && self.length > 0 {
panic!("attempted to access a consumed `OsIpcSharedMemory`")
}
unsafe { slice::from_raw_parts_mut(self.ptr, self.length) }
}
}

impl OsIpcSharedMemory {
unsafe fn from_raw_parts(ptr: *mut u8, length: usize) -> OsIpcSharedMemory {
OsIpcSharedMemory {
Expand Down
9 changes: 8 additions & 1 deletion src/platform/unix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use std::hash::BuildHasherDefault;
use std::io;
use std::marker::PhantomData;
use std::mem;
use std::ops::{Deref, RangeFrom};
use std::ops::{Deref, DerefMut, RangeFrom};
use std::os::fd::RawFd;
use std::ptr;
use std::slice;
Expand Down Expand Up @@ -866,6 +866,13 @@ impl Deref for OsIpcSharedMemory {
}
}

impl OsIpcSharedMemory {
#[inline]
pub unsafe fn deref_mut(&mut self) -> &mut [u8] {
unsafe { slice::from_raw_parts_mut(self.ptr, self.length) }
}
}

impl OsIpcSharedMemory {
unsafe fn from_raw_parts(
ptr: *mut u8,
Expand Down
8 changes: 8 additions & 0 deletions src/platform/windows/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1834,6 +1834,14 @@ impl Deref for OsIpcSharedMemory {
}
}

impl OsIpcSharedMemory {
#[inline]
pub unsafe fn deref_mut(&mut self) -> &mut [u8] {
assert!(!self.view_handle.Value.is_null() && self.handle.is_valid());
unsafe { slice::from_raw_parts_mut(self.view_handle.Value as _, self.length) }
}
}

impl OsIpcSharedMemory {
fn new(length: usize) -> Result<OsIpcSharedMemory, WinError> {
unsafe {
Expand Down

0 comments on commit d20de40

Please sign in to comment.