From c26ebb448f9346f4154cae7ed66fea8bd7d4ef2c Mon Sep 17 00:00:00 2001 From: zephyr Date: Tue, 30 Apr 2024 03:12:04 +0900 Subject: [PATCH] (wip)realm-io: fix lifetime --- realm_io/src/linux/mmsg.rs | 54 ++++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 26 deletions(-) diff --git a/realm_io/src/linux/mmsg.rs b/realm_io/src/linux/mmsg.rs index 725fee23..9073d002 100644 --- a/realm_io/src/linux/mmsg.rs +++ b/realm_io/src/linux/mmsg.rs @@ -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(fd: RawFd, pkts: &mut [PacketStore<'_, '_, '_, M>]) -> i32 { +fn sendmpkts(fd: RawFd, pkts: &mut [PacketStore<'_, '_, '_, '_, M>]) -> i32 { unsafe { libc::sendmmsg(fd, pkts.as_ptr() as *mut _, pkts.len() as u32, 0) } } @@ -33,7 +33,7 @@ fn recvmpkts(fd: RawFd, pkts: &mut [PacketMut]) -> i32 { fn poll_sendmpkts( stream: &mut S, cx: &mut Context<'_>, - pkts: &mut [PacketStore<'_, '_, '_, M>], + pkts: &mut [PacketStore<'_, '_, '_, '_, M>], ) -> Poll> where S: AsyncRawIO + Unpin, @@ -49,7 +49,7 @@ where } /// Send multiple packets. -pub async fn send_mul_pkts(stream: &mut S, pkts: &mut [Packet<'_, '_, '_>]) -> Result +pub async fn send_mul_pkts(stream: &mut S, pkts: &mut [Packet<'_, '_, '_, '_>]) -> Result where S: AsyncRawIO + Unpin, { @@ -57,7 +57,7 @@ where } /// Recv multiple packets. -pub async fn recv_mul_pkts(stream: &mut S, pkts: &mut [PacketMut<'_, '_, '_>]) -> Result +pub async fn recv_mul_pkts(stream: &mut S, pkts: &mut [PacketMut<'_, '_, '_, '_>]) -> Result where S: AsyncRawIO + Unpin, { @@ -82,16 +82,16 @@ 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, - _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, @@ -99,9 +99,9 @@ mod store { } /// 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, @@ -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 { @@ -147,8 +147,9 @@ 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, @@ -156,7 +157,7 @@ mod store { } = 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, @@ -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 _; @@ -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 @@ -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 _; @@ -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 @@ -223,8 +224,9 @@ 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, @@ -232,7 +234,7 @@ mod store { } = 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,