Skip to content

Commit

Permalink
(wip)realm-io: fix lifetime
Browse files Browse the repository at this point in the history
  • Loading branch information
zephyrchien committed Apr 29, 2024
1 parent 6b5f03e commit c26ebb4
Showing 1 changed file with 28 additions and 26 deletions.
54 changes: 28 additions & 26 deletions realm_io/src/linux/mmsg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ use crate::AsyncRawIO;
pub use store::{PacketStore, Const, Mutable};
pub use store::{PacketRef, PacketMutRef};
pub use store::{SockAddrStore, SOCK_STORE_LEN};
pub type Packet<'a, 'iov, 'ctrl> = PacketStore<'a, 'iov, 'ctrl, Const>;
pub type PacketMut<'a, 'iov, 'ctrl> = PacketStore<'a, 'iov, 'ctrl, Mutable>;
pub type Packet<'a, 'buf, 'iov, 'ctrl> = PacketStore<'a, 'buf, 'iov, 'ctrl, Const>;
pub type PacketMut<'a, 'buf, 'iov, 'ctrl> = PacketStore<'a, 'buf, 'iov, 'ctrl, Mutable>;

#[inline]
fn sendmpkts<M>(fd: RawFd, pkts: &mut [PacketStore<'_, '_, '_, M>]) -> i32 {
fn sendmpkts<M>(fd: RawFd, pkts: &mut [PacketStore<'_, '_, '_, '_, M>]) -> i32 {
unsafe { libc::sendmmsg(fd, pkts.as_ptr() as *mut _, pkts.len() as u32, 0) }
}

Expand All @@ -33,7 +33,7 @@ fn recvmpkts(fd: RawFd, pkts: &mut [PacketMut]) -> i32 {
fn poll_sendmpkts<S, M>(
stream: &mut S,
cx: &mut Context<'_>,
pkts: &mut [PacketStore<'_, '_, '_, M>],
pkts: &mut [PacketStore<'_, '_, '_, '_, M>],
) -> Poll<Result<usize>>
where
S: AsyncRawIO + Unpin,
Expand All @@ -49,15 +49,15 @@ where
}

/// Send multiple packets.
pub async fn send_mul_pkts<S>(stream: &mut S, pkts: &mut [Packet<'_, '_, '_>]) -> Result<usize>
pub async fn send_mul_pkts<S>(stream: &mut S, pkts: &mut [Packet<'_, '_, '_, '_>]) -> Result<usize>
where
S: AsyncRawIO + Unpin,
{
std::future::poll_fn(move |cx| poll_sendmpkts(stream, cx, pkts)).await
}

/// Recv multiple packets.
pub async fn recv_mul_pkts<S>(stream: &mut S, pkts: &mut [PacketMut<'_, '_, '_>]) -> Result<usize>
pub async fn recv_mul_pkts<S>(stream: &mut S, pkts: &mut [PacketMut<'_, '_, '_, '_>]) -> Result<usize>
where
S: AsyncRawIO + Unpin,
{
Expand All @@ -82,26 +82,26 @@ mod store {
/// Represent [`libc::mmsghdr`].
#[derive(Clone, Copy)]
#[repr(transparent)]
pub struct PacketStore<'a, 'iov, 'ctrl, M> {
pub struct PacketStore<'a, 'buf, 'iov, 'ctrl, M> {
pub(crate) store: mmsghdr,
_type: PhantomData<M>,
_lifetime: PhantomData<(&'a (), &'iov (), &'ctrl ())>,
_lifetime: PhantomData<(&'a (), &'buf (), &'iov (), &'ctrl ())>,
}

/// Constant field accessor for [`PacketStore`].
pub struct PacketRef<'a, 'iov, 'ctrl, 'this> {
pub struct PacketRef<'a, 'buf, 'iov, 'ctrl, 'this> {
addr: &'a SockAddrStore,
iovec: &'iov [IoSlice<'iov>],
iovec: &'iov [IoSlice<'buf>],
control: &'ctrl [u8],
flags: i32,
nbytes: u32,
_lifetime: PhantomData<&'this ()>,
}

/// Mutable field accessor for [`PacketStore`].
pub struct PacketMutRef<'a, 'iov, 'ctrl, 'this> {
pub struct PacketMutRef<'a, 'buf, 'iov, 'ctrl, 'this> {
addr: &'a mut SockAddrStore,
iovec: &'iov mut [IoSlice<'iov>],
iovec: &'iov mut [IoSlice<'buf>],
control: &'ctrl mut [u8],
flags: i32,
nbytes: u32,
Expand All @@ -121,23 +121,23 @@ mod store {
};
}

impl<'a, 'iov, 'ctrl, 'this> PacketRef<'a, 'iov, 'ctrl, 'this> {
impl<'a, 'buf, 'iov, 'ctrl, 'this> PacketRef<'a, 'buf, 'iov, 'ctrl, 'this> {
access_fn!(!ref, addr, &&'a SockAddrStore);
access_fn!(!ref, iovec, &&'iov [IoSlice<'iov>]);
access_fn!(!ref, iovec, &&'iov [IoSlice<'buf>]);
access_fn!(!ref, control, &&'ctrl [u8]);
access_fn!(!val, flags, i32);
access_fn!(!val, nbytes, u32);
}

impl<'a, 'iov, 'ctrl, 'this> PacketMutRef<'a, 'iov, 'ctrl, 'this> {
impl<'a, 'buf, 'iov, 'ctrl, 'this> PacketMutRef<'a, 'buf, 'iov, 'ctrl, 'this> {
access_fn!(!mut, addr, &mut &'a mut SockAddrStore);
access_fn!(!mut, iovec, &mut &'iov mut [IoSlice<'iov>]);
access_fn!(!mut, iovec, &mut &'iov mut [IoSlice<'buf>]);
access_fn!(!mut, control, &mut &'ctrl mut [u8]);
access_fn!(!val, flags, i32);
access_fn!(!val, nbytes, u32);
}

impl<'a, 'iov, 'ctrl, M> PacketStore<'a, 'iov, 'ctrl, M> {
impl<'a, 'buf, 'iov, 'ctrl, M> PacketStore<'a, 'buf, 'iov, 'ctrl, M> {
/// New zeroed storage.
pub const fn new() -> Self {
Self {
Expand All @@ -147,16 +147,17 @@ mod store {
}
}

/// Get constant accessor.
#[rustfmt::skip]
pub fn get_ref<'this>(&'this self) -> PacketRef<'this, 'a, 'iov, 'ctrl> {
pub fn get_ref<'this>(&'this self) -> PacketRef<'this, 'a, 'buf, 'iov, 'ctrl> {
let msghdr {
msg_name, msg_namelen,
msg_iov, msg_iovlen,
msg_control, msg_controllen, msg_flags,
} = self.store.msg_hdr;
let msg_len = self.store.msg_len;
unsafe { PacketRef {
addr: &*msg_name.cast(),
addr: &*msg_name.cast(), // todo!
iovec: slice::from_raw_parts(msg_iov as *const _, msg_iovlen),
control: slice::from_raw_parts(msg_control as *const _, msg_controllen),
flags: msg_flags,
Expand All @@ -166,13 +167,13 @@ mod store {
}
}

impl<'a, 'iov, 'ctrl, M> Default for PacketStore<'a, 'iov, 'ctrl, M> {
impl<'a, 'buf, 'iov, 'ctrl, M> Default for PacketStore<'a, 'buf, 'iov, 'ctrl, M> {
fn default() -> Self {
Self::new()
}
}

impl<'a, 'iov, 'ctrl> PacketStore<'a, 'iov, 'ctrl, Const> {
impl<'a, 'buf, 'iov, 'ctrl> PacketStore<'a, 'buf, 'iov, 'ctrl, Const> {
/// Set target address.
pub const fn with_addr(mut self, addr: &'a SockAddrStore) -> Self {
self.store.msg_hdr.msg_name = addr.0.as_ptr() as *mut _;
Expand All @@ -181,7 +182,7 @@ mod store {
}

/// Set data to send.
pub const fn with_iovec(mut self, iov: &'iov [IoSlice]) -> Self {
pub const fn with_iovec(mut self, iov: &'iov [IoSlice<'buf>]) -> Self {
self.store.msg_hdr.msg_iov = ptr::from_ref(iov) as *mut _;
self.store.msg_hdr.msg_iovlen = iov.len();
self
Expand All @@ -201,7 +202,7 @@ mod store {
}
}

impl<'a, 'iov, 'ctrl> PacketStore<'a, 'iov, 'ctrl, Mutable> {
impl<'a, 'buf, 'iov, 'ctrl> PacketStore<'a, 'buf, 'iov, 'ctrl, Mutable> {
/// Set storage to accommodate peer address.
pub fn with_addr(mut self, addr: &'a mut SockAddrStore) -> Self {
self.store.msg_hdr.msg_name = addr.0.as_ptr() as *mut _;
Expand All @@ -210,7 +211,7 @@ mod store {
}

/// Set storage to receive data.
pub fn with_iovec(mut self, iov: &'iov mut [IoSliceMut]) -> Self {
pub fn with_iovec(mut self, iov: &'iov mut [IoSliceMut<'buf>]) -> Self {
self.store.msg_hdr.msg_iov = ptr::from_mut(iov) as *mut _;
self.store.msg_hdr.msg_iovlen = iov.len();
self
Expand All @@ -223,16 +224,17 @@ mod store {
self
}

/// Get mutable accessor.
#[rustfmt::skip]
pub fn get_mut<'this>(&'this mut self) -> PacketMutRef<'this, 'a, 'iov, 'ctrl> {
pub fn get_mut<'this>(&'this mut self) -> PacketMutRef<'this, 'a, 'buf, 'iov, 'ctrl> {
let msghdr {
msg_name, msg_namelen,
msg_iov, msg_iovlen,
msg_control, msg_controllen, msg_flags,
} = self.store.msg_hdr;
let msg_len = self.store.msg_len;
unsafe { PacketMutRef {
addr: &mut *msg_name.cast(),
addr: &mut *msg_name.cast(), // todo!
iovec: slice::from_raw_parts_mut(msg_iov as *mut _, msg_iovlen),
control: slice::from_raw_parts_mut(msg_control as *mut _, msg_controllen),
flags: msg_flags,
Expand Down

0 comments on commit c26ebb4

Please sign in to comment.