@@ -27,7 +27,7 @@ use libc::{ftruncate as ftruncate64, off_t as off64_t};
2727) ) ]
2828use libc:: { ftruncate64, off64_t} ;
2929use pin_project_lite:: pin_project;
30- use socket2:: { SockAddr , SockAddrStorage , socklen_t} ;
30+ use socket2:: { SockAddr , SockAddrStorage , Socket as Socket2 , socklen_t} ;
3131
3232use crate :: { op:: * , sys:: aio:: * , sys_slice:: * , syscall} ;
3333
@@ -47,12 +47,15 @@ impl AsFd for CurrentDir {
4747 }
4848}
4949
50- /// Open or create a file with flags and mode.
51- pub struct OpenFile < S : AsFd > {
52- pub ( crate ) dirfd : S ,
53- pub ( crate ) path : CString ,
54- pub ( crate ) flags : i32 ,
55- pub ( crate ) mode : libc:: mode_t ,
50+ pin_project ! {
51+ /// Open or create a file with flags and mode.
52+ pub struct OpenFile <S : AsFd > {
53+ pub ( crate ) dirfd: S ,
54+ pub ( crate ) path: CString ,
55+ pub ( crate ) flags: i32 ,
56+ pub ( crate ) mode: libc:: mode_t,
57+ pub ( crate ) opened_fd: Option <OwnedFd >,
58+ }
5659}
5760
5861impl < S : AsFd > OpenFile < S > {
@@ -63,6 +66,7 @@ impl<S: AsFd> OpenFile<S> {
6366 path,
6467 flags,
6568 mode,
69+ opened_fd : None ,
6670 }
6771 }
6872
@@ -76,6 +80,14 @@ impl<S: AsFd> OpenFile<S> {
7680 }
7781}
7882
83+ impl < S : AsFd > IntoInner for OpenFile < S > {
84+ type Inner = OwnedFd ;
85+
86+ fn into_inner ( self ) -> Self :: Inner {
87+ self . opened_fd . expect ( "file not opened" )
88+ }
89+ }
90+
7991impl CloseFile {
8092 pub ( crate ) fn call ( self : Pin < & mut Self > ) -> io:: Result < usize > {
8193 Ok ( syscall ! ( libc:: close( self . fd. as_fd( ) . as_raw_fd( ) ) ) ? as _ )
@@ -462,11 +474,14 @@ impl<S1: AsFd, S2: AsFd> HardLink<S1, S2> {
462474 }
463475}
464476
465- /// Create a socket.
466- pub struct CreateSocket {
467- pub ( crate ) domain : i32 ,
468- pub ( crate ) socket_type : i32 ,
469- pub ( crate ) protocol : i32 ,
477+ pin_project ! {
478+ /// Create a socket.
479+ pub struct CreateSocket {
480+ pub ( crate ) domain: i32 ,
481+ pub ( crate ) socket_type: i32 ,
482+ pub ( crate ) protocol: i32 ,
483+ pub ( crate ) opened_fd: Option <Socket2 >,
484+ }
470485}
471486
472487impl CreateSocket {
@@ -476,10 +491,19 @@ impl CreateSocket {
476491 domain,
477492 socket_type,
478493 protocol,
494+ opened_fd : None ,
479495 }
480496 }
481497}
482498
499+ impl IntoInner for CreateSocket {
500+ type Inner = Socket2 ;
501+
502+ fn into_inner ( self ) -> Self :: Inner {
503+ self . opened_fd . expect ( "socket not created" )
504+ }
505+ }
506+
483507impl < S : AsFd > ShutdownSocket < S > {
484508 pub ( crate ) fn how ( & self ) -> i32 {
485509 match self . how {
@@ -506,7 +530,7 @@ pin_project! {
506530 pub ( crate ) fd: S ,
507531 pub ( crate ) buffer: SockAddrStorage ,
508532 pub ( crate ) addr_len: socklen_t,
509- pub ( crate ) accepted_fd: Option <OwnedFd >,
533+ pub ( crate ) accepted_fd: Option <Socket2 >,
510534 _p: PhantomPinned ,
511535 }
512536}
@@ -526,9 +550,9 @@ impl<S> Accept<S> {
526550 }
527551
528552 /// Get the remote address from the inner buffer.
529- pub fn into_addr ( mut self ) -> SockAddr {
530- std :: mem :: forget ( self . accepted_fd . take ( ) ) ;
531- unsafe { SockAddr :: new ( self . buffer , self . addr_len ) }
553+ pub fn into_addr ( mut self ) -> ( Socket2 , SockAddr ) {
554+ let socket = self . accepted_fd . take ( ) . expect ( "socket not accepted" ) ;
555+ ( socket , unsafe { SockAddr :: new ( self . buffer , self . addr_len ) } )
532556 }
533557}
534558
0 commit comments