@@ -699,3 +699,202 @@ impl<'name, 'bufs, 'control> fmt::Debug for MsgHdrMut<'name, 'bufs, 'control> {
699699 "MsgHdrMut" . fmt ( fmt)
700700 }
701701}
702+
703+ /// Configuration of a `sendmmsg(2)` system call.
704+ ///
705+ /// This wraps `mmsghdr` on Unix. Also see [`MMsgHdrMut`] for the variant used by `recvmmsg(2)`.
706+ /// This API is not available on Windows.
707+ #[ cfg( any(
708+ target_os = "aix" ,
709+ target_os = "android" ,
710+ target_os = "freebsd" ,
711+ target_os = "linux" ,
712+ target_os = "netbsd" ,
713+ target_os = "openbsd" ,
714+ ) ) ]
715+ pub struct MMsgHdr < ' addr , ' bufs , ' control > {
716+ inner : sys:: mmsghdr ,
717+ #[ allow( clippy:: type_complexity) ]
718+ _lifetimes : PhantomData < ( & ' addr SockAddr , & ' bufs IoSlice < ' bufs > , & ' control [ u8 ] ) > ,
719+ }
720+
721+ #[ cfg( any(
722+ target_os = "aix" ,
723+ target_os = "android" ,
724+ target_os = "freebsd" ,
725+ target_os = "linux" ,
726+ target_os = "netbsd" ,
727+ target_os = "openbsd" ,
728+ ) ) ]
729+ impl < ' addr , ' bufs , ' control > MMsgHdr < ' addr , ' bufs , ' control > {
730+ /// Create a new `MMsgHdr` with all empty/zero fields.
731+ #[ allow( clippy:: new_without_default) ]
732+ pub fn new ( ) -> MMsgHdr < ' addr , ' bufs , ' control > {
733+ // SAFETY: all zero is valid for `mmsghdr`.
734+ MMsgHdr {
735+ inner : unsafe { mem:: zeroed ( ) } ,
736+ _lifetimes : PhantomData ,
737+ }
738+ }
739+
740+ /// Set the address (name) of the message.
741+ ///
742+ /// Corresponds to setting `msg_name` and `msg_namelen` on Unix.
743+ pub fn with_addr ( mut self , addr : & ' addr SockAddr ) -> Self {
744+ sys:: set_msghdr_name ( & mut self . inner . msg_hdr , addr) ;
745+ self
746+ }
747+
748+ /// Set the buffer(s) of the message.
749+ ///
750+ /// Corresponds to setting `msg_iov` and `msg_iovlen` on Unix.
751+ pub fn with_buffers ( mut self , bufs : & ' bufs [ IoSlice < ' _ > ] ) -> Self {
752+ let ptr = bufs. as_ptr ( ) as * mut _ ;
753+ sys:: set_msghdr_iov ( & mut self . inner . msg_hdr , ptr, bufs. len ( ) ) ;
754+ self
755+ }
756+
757+ /// Set the control buffer of the message.
758+ ///
759+ /// Corresponds to setting `msg_control` and `msg_controllen` on Unix.
760+ pub fn with_control ( mut self , buf : & ' control [ u8 ] ) -> Self {
761+ let ptr = buf. as_ptr ( ) as * mut _ ;
762+ sys:: set_msghdr_control ( & mut self . inner . msg_hdr , ptr, buf. len ( ) ) ;
763+ self
764+ }
765+
766+ /// Set the flags of the message.
767+ ///
768+ /// Corresponds to setting `msg_flags` on Unix.
769+ pub fn with_flags ( mut self , flags : sys:: c_int ) -> Self {
770+ sys:: set_msghdr_flags ( & mut self . inner . msg_hdr , flags) ;
771+ self
772+ }
773+
774+ /// Gets the number of sent bytes.
775+ ///
776+ /// Corresponds to `msg_len` on Unix.
777+ pub fn data_len ( & self ) -> usize {
778+ self . inner . msg_len as usize
779+ }
780+ }
781+
782+ #[ cfg( any(
783+ target_os = "aix" ,
784+ target_os = "android" ,
785+ target_os = "freebsd" ,
786+ target_os = "linux" ,
787+ target_os = "netbsd" ,
788+ target_os = "openbsd" ,
789+ ) ) ]
790+ impl < ' name , ' bufs , ' control > fmt:: Debug for MMsgHdr < ' name , ' bufs , ' control > {
791+ fn fmt ( & self , fmt : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
792+ "MMsgHdr" . fmt ( fmt)
793+ }
794+ }
795+
796+ /// Configuration of a `recvmmsg(2)` system call.
797+ ///
798+ /// This wraps `mmsghdr` on Unix. Also see [`MMsgHdr`] for the variant used by `sendmmsg(2)`.
799+ /// This API is not available on Windows.
800+ #[ cfg( any(
801+ target_os = "aix" ,
802+ target_os = "android" ,
803+ target_os = "freebsd" ,
804+ target_os = "linux" ,
805+ target_os = "netbsd" ,
806+ target_os = "openbsd" ,
807+ ) ) ]
808+ pub struct MMsgHdrMut < ' addr , ' bufs , ' control > {
809+ inner : sys:: mmsghdr ,
810+ #[ allow( clippy:: type_complexity) ]
811+ _lifetimes : PhantomData < (
812+ & ' addr mut SockAddr ,
813+ & ' bufs mut MaybeUninitSlice < ' bufs > ,
814+ & ' control mut [ u8 ] ,
815+ ) > ,
816+ }
817+
818+ #[ cfg( any(
819+ target_os = "aix" ,
820+ target_os = "android" ,
821+ target_os = "freebsd" ,
822+ target_os = "linux" ,
823+ target_os = "netbsd" ,
824+ target_os = "openbsd" ,
825+ ) ) ]
826+ impl < ' addr , ' bufs , ' control > MMsgHdrMut < ' addr , ' bufs , ' control > {
827+ /// Create a new `MMsgHdrMut` with all empty/zero fields.
828+ #[ allow( clippy:: new_without_default) ]
829+ pub fn new ( ) -> MMsgHdrMut < ' addr , ' bufs , ' control > {
830+ // SAFETY: all zero is valid for `mmsghdr`.
831+ MMsgHdrMut {
832+ inner : unsafe { mem:: zeroed ( ) } ,
833+ _lifetimes : PhantomData ,
834+ }
835+ }
836+
837+ /// Set the mutable address (name) of the message.
838+ ///
839+ /// Corresponds to setting `msg_name` and `msg_namelen` on Unix.
840+ #[ allow( clippy:: needless_pass_by_ref_mut) ]
841+ pub fn with_addr ( mut self , addr : & ' addr mut SockAddr ) -> Self {
842+ sys:: set_msghdr_name ( & mut self . inner . msg_hdr , addr) ;
843+ self
844+ }
845+
846+ /// Set the mutable buffer(s) of the message.
847+ ///
848+ /// Corresponds to setting `msg_iov` and `msg_iovlen` on Unix.
849+ pub fn with_buffers ( mut self , bufs : & ' bufs mut [ MaybeUninitSlice < ' _ > ] ) -> Self {
850+ sys:: set_msghdr_iov (
851+ & mut self . inner . msg_hdr ,
852+ bufs. as_mut_ptr ( ) . cast ( ) ,
853+ bufs. len ( ) ,
854+ ) ;
855+ self
856+ }
857+
858+ /// Set the mutable control buffer of the message.
859+ ///
860+ /// Corresponds to setting `msg_control` and `msg_controllen` on Unix.
861+ pub fn with_control ( mut self , buf : & ' control mut [ MaybeUninit < u8 > ] ) -> Self {
862+ sys:: set_msghdr_control ( & mut self . inner . msg_hdr , buf. as_mut_ptr ( ) . cast ( ) , buf. len ( ) ) ;
863+ self
864+ }
865+
866+ /// Returns the flags of the message.
867+ pub fn flags ( & self ) -> RecvFlags {
868+ sys:: msghdr_flags ( & self . inner . msg_hdr )
869+ }
870+
871+ /// Gets the length of the control buffer.
872+ ///
873+ /// Can be used to determine how much, if any, of the control buffer was filled by `recvmsg`.
874+ ///
875+ /// Corresponds to `msg_controllen` on Unix.
876+ pub fn control_len ( & self ) -> usize {
877+ sys:: msghdr_control_len ( & self . inner . msg_hdr )
878+ }
879+
880+ /// Gets the number of received bytes.
881+ ///
882+ /// Corresponds to `msg_len` on Unix.
883+ pub fn data_len ( & self ) -> usize {
884+ self . inner . msg_len as usize
885+ }
886+ }
887+
888+ #[ cfg( any(
889+ target_os = "aix" ,
890+ target_os = "android" ,
891+ target_os = "freebsd" ,
892+ target_os = "linux" ,
893+ target_os = "netbsd" ,
894+ target_os = "openbsd" ,
895+ ) ) ]
896+ impl < ' name , ' bufs , ' control > fmt:: Debug for MMsgHdrMut < ' name , ' bufs , ' control > {
897+ fn fmt ( & self , fmt : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
898+ "MMsgHdrMut" . fmt ( fmt)
899+ }
900+ }
0 commit comments