66use std:: ptr:: null_mut;
77use std:: { ffi:: OsStr , io, os:: windows:: io:: FromRawHandle , ptr:: null} ;
88
9- use compio_buf:: { BufResult , IoBuf , IoBufMut } ;
9+ use compio_buf:: { BufResult , IntoInner , IoBuf , IoBufMut } ;
1010use compio_driver:: { AsRawFd , RawFd , ToSharedFd , impl_raw_fd, op:: ConnectNamedPipe , syscall} ;
1111use compio_io:: {
1212 AsyncRead , AsyncReadAt , AsyncReadManaged , AsyncReadManagedAt , AsyncWrite , AsyncWriteAt ,
@@ -29,7 +29,7 @@ use windows_sys::Win32::{
2929 } ,
3030} ;
3131
32- use crate :: { AsyncFd , File , OpenOptions } ;
32+ use crate :: { AsyncFd , File , OpenOptions , SyncFile } ;
3333
3434/// A [Windows named pipe] server.
3535///
@@ -180,6 +180,21 @@ impl NamedPipeServer {
180180 syscall ! ( BOOL , DisconnectNamedPipe ( self . as_raw_fd( ) as _) ) ?;
181181 Ok ( ( ) )
182182 }
183+
184+ /// Attempts to clone the named pipe server.
185+ pub fn try_clone ( & self ) -> io:: Result < SyncNamedPipeServer > {
186+ self . handle . try_clone ( ) . map ( SyncNamedPipeServer :: new)
187+ }
188+
189+ /// Try to convert the named pipe server into a thread safe handle.
190+ pub fn try_into_sync ( self ) -> Result < SyncNamedPipeServer , Self > {
191+ match self . handle . into_inner ( ) . try_unwrap ( ) {
192+ Ok ( file) => Ok ( SyncNamedPipeServer :: new ( file) ) ,
193+ Err ( fd) => Err ( Self {
194+ handle : unsafe { AsyncFd :: from_shared_fd_unchecked ( fd) } ,
195+ } ) ,
196+ }
197+ }
183198}
184199
185200impl AsyncRead for NamedPipeServer {
@@ -277,6 +292,33 @@ impl Splittable for &NamedPipeServer {
277292
278293impl_raw_fd ! ( NamedPipeServer , std:: fs:: File , handle, file) ;
279294
295+ /// A thread safe named pipe server.
296+ #[ derive( Debug ) ]
297+ pub struct SyncNamedPipeServer {
298+ inner : std:: fs:: File ,
299+ }
300+
301+ impl SyncNamedPipeServer {
302+ pub ( crate ) fn new ( inner : std:: fs:: File ) -> Self {
303+ Self { inner }
304+ }
305+
306+ /// Attempts to clone the named pipe server.
307+ pub fn try_clone ( & self ) -> io:: Result < Self > {
308+ self . inner . try_clone ( ) . map ( Self :: new)
309+ }
310+ }
311+
312+ impl IntoInner for SyncNamedPipeServer {
313+ type Inner = NamedPipeServer ;
314+
315+ fn into_inner ( self ) -> Self :: Inner {
316+ NamedPipeServer {
317+ handle : unsafe { AsyncFd :: new_unchecked ( self . inner ) } ,
318+ }
319+ }
320+ }
321+
280322/// A [Windows named pipe] client.
281323///
282324/// Constructed using [`ClientOptions::open`].
@@ -344,6 +386,19 @@ impl NamedPipeClient {
344386 // Safety: we're ensuring the lifetime of the named pipe.
345387 unsafe { named_pipe_info ( self . as_raw_fd ( ) ) }
346388 }
389+
390+ /// Attempts to clone the named pipe client.
391+ pub fn try_clone ( & self ) -> io:: Result < SyncNamedPipeClient > {
392+ self . handle . try_clone ( ) . map ( SyncNamedPipeClient :: new)
393+ }
394+
395+ /// Try to convert the named pipe client into a thread safe handle.
396+ pub fn try_into_sync ( self ) -> Result < SyncNamedPipeClient , Self > {
397+ match self . handle . try_into_sync ( ) {
398+ Ok ( file) => Ok ( SyncNamedPipeClient :: new ( file) ) ,
399+ Err ( handle) => Err ( Self { handle } ) ,
400+ }
401+ }
347402}
348403
349404impl AsyncRead for NamedPipeClient {
@@ -443,6 +498,33 @@ impl Splittable for &NamedPipeClient {
443498
444499impl_raw_fd ! ( NamedPipeClient , std:: fs:: File , handle, file) ;
445500
501+ /// A thread safe named pipe client.
502+ #[ derive( Debug ) ]
503+ pub struct SyncNamedPipeClient {
504+ inner : SyncFile ,
505+ }
506+
507+ impl SyncNamedPipeClient {
508+ pub ( crate ) fn new ( inner : SyncFile ) -> Self {
509+ Self { inner }
510+ }
511+
512+ /// Attempts to clone the named pipe client.
513+ pub fn try_clone ( & self ) -> io:: Result < Self > {
514+ self . inner . try_clone ( ) . map ( Self :: new)
515+ }
516+ }
517+
518+ impl IntoInner for SyncNamedPipeClient {
519+ type Inner = NamedPipeClient ;
520+
521+ fn into_inner ( self ) -> Self :: Inner {
522+ NamedPipeClient {
523+ handle : self . inner . into_inner ( ) ,
524+ }
525+ }
526+ }
527+
446528/// A builder structure for construct a named pipe with named pipe-specific
447529/// options. This is required to use for named pipe servers who wants to modify
448530/// pipe-related options.
0 commit comments