Skip to content

Commit 83d6b79

Browse files
BerrysoftCopilot
andauthored
docs(fs,net): comments on close (#821)
* feat(fs,win): close for named pipe * docs(fs): comments on `close` * docs(net): comments on `close` * fix(net): typo Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent a261ae5 commit 83d6b79

6 files changed

Lines changed: 58 additions & 6 deletions

File tree

compio-fs/src/file.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,14 @@ impl File {
8383

8484
/// Close the file. If the returned future is dropped before polling, the
8585
/// file won't be closed.
86+
///
87+
/// As [`File`] is clonable, users can call `close` on a clone, but the
88+
/// future will never complete until all clones are dropped. Some
89+
/// operations may keep a strong reference to the file, so the future
90+
/// may never complete if there are pending operations.
91+
///
92+
/// It's OK to drop the [`File`] directly without calling `close`, but the
93+
/// file may not be closed immediately.
8694
pub fn close(self) -> impl Future<Output = io::Result<()>> {
8795
// Make sure that fd won't be dropped after `close` called.
8896
// Users may call this method and drop the future immediately. In that way

compio-fs/src/named_pipe.rs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use compio_io::{
1212
AsyncRead, AsyncReadAt, AsyncReadManaged, AsyncReadManagedAt, AsyncWrite, AsyncWriteAt,
1313
util::Splittable,
1414
};
15-
use compio_runtime::{BorrowedBuffer, BufferPool, fd::AsyncFd};
15+
use compio_runtime::{BorrowedBuffer, BufferPool};
1616
use widestring::U16CString;
1717
use windows_sys::Win32::{
1818
Storage::FileSystem::{
@@ -91,7 +91,7 @@ use crate::{File, OpenOptions};
9191
/// [Windows named pipe]: https://docs.microsoft.com/en-us/windows/win32/ipc/named-pipes
9292
#[derive(Debug, Clone)]
9393
pub struct NamedPipeServer {
94-
handle: AsyncFd<std::fs::File>,
94+
handle: File,
9595
}
9696

9797
impl NamedPipeServer {
@@ -180,6 +180,14 @@ impl NamedPipeServer {
180180
syscall!(BOOL, DisconnectNamedPipe(self.as_raw_fd() as _))?;
181181
Ok(())
182182
}
183+
184+
/// Close the server. If the returned future is dropped before polling, the
185+
/// server won't be closed.
186+
///
187+
/// See [`File::close`] for more details.
188+
pub fn close(self) -> impl Future<Output = io::Result<()>> {
189+
self.handle.close()
190+
}
183191
}
184192

185193
impl AsyncRead for NamedPipeServer {
@@ -192,7 +200,7 @@ impl AsyncRead for NamedPipeServer {
192200
impl AsyncRead for &NamedPipeServer {
193201
#[inline]
194202
async fn read<B: IoBufMut>(&mut self, buffer: B) -> BufResult<usize, B> {
195-
(&self.handle).read(buffer).await
203+
self.handle.read_at(buffer, 0).await
196204
}
197205
}
198206

@@ -219,7 +227,7 @@ impl AsyncReadManaged for &NamedPipeServer {
219227
len: usize,
220228
) -> io::Result<Self::Buffer<'a>> {
221229
// The position is ignored.
222-
(&self.handle).read_managed(buffer_pool, len).await
230+
self.handle.read_managed_at(buffer_pool, len, 0).await
223231
}
224232
}
225233

@@ -243,7 +251,7 @@ impl AsyncWrite for NamedPipeServer {
243251
impl AsyncWrite for &NamedPipeServer {
244252
#[inline]
245253
async fn write<T: IoBuf>(&mut self, buffer: T) -> BufResult<usize, T> {
246-
(&self.handle).write(buffer).await
254+
(&self.handle).write_at(buffer, 0).await
247255
}
248256

249257
#[inline]
@@ -344,6 +352,14 @@ impl NamedPipeClient {
344352
// SAFETY: we're ensuring the lifetime of the named pipe.
345353
unsafe { named_pipe_info(self.as_raw_fd()) }
346354
}
355+
356+
/// Close the client. If the returned future is dropped before polling, the
357+
/// client won't be closed.
358+
///
359+
/// See [`File::close`] for more details.
360+
pub fn close(self) -> impl Future<Output = io::Result<()>> {
361+
self.handle.close()
362+
}
347363
}
348364

349365
impl AsyncRead for NamedPipeClient {
@@ -1065,7 +1081,7 @@ impl ServerOptions {
10651081
)?;
10661082

10671083
Ok(NamedPipeServer {
1068-
handle: AsyncFd::new(unsafe { std::fs::File::from_raw_handle(h as _) })?,
1084+
handle: File::from_std(unsafe { std::fs::File::from_raw_handle(h as _) })?,
10691085
})
10701086
}
10711087
}

compio-fs/src/pipe/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,8 @@ impl Sender {
342342

343343
/// Close the pipe. If the returned future is dropped before polling, the
344344
/// pipe won't be closed.
345+
///
346+
/// See [`File::close`] for more details.
345347
pub fn close(self) -> impl Future<Output = io::Result<()>> {
346348
self.file.close()
347349
}
@@ -477,6 +479,8 @@ impl Receiver {
477479

478480
/// Close the pipe. If the returned future is dropped before polling, the
479481
/// pipe won't be closed.
482+
///
483+
/// See [`File::close`] for more details.
480484
pub fn close(self) -> impl Future<Output = io::Result<()>> {
481485
self.file.close()
482486
}

compio-net/src/tcp.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ impl TcpListener {
104104

105105
/// Close the socket. If the returned future is dropped before polling, the
106106
/// socket won't be closed.
107+
///
108+
/// See [`TcpStream::close`] for more details.
109+
///
110+
/// [`TcpStream::close`]: crate::tcp::TcpStream::close
107111
pub fn close(self) -> impl Future<Output = io::Result<()>> {
108112
self.inner.close()
109113
}
@@ -303,6 +307,14 @@ impl TcpStream {
303307

304308
/// Close the socket. If the returned future is dropped before polling, the
305309
/// socket won't be closed.
310+
///
311+
/// As the socket is clonable, users can call `close` on a clone, but the
312+
/// future will never complete until all clones are dropped. Some
313+
/// operations may keep a strong reference to the socket, so the future
314+
/// may never complete if there are pending operations.
315+
///
316+
/// It's OK to drop the socket directly without calling `close`, but the
317+
/// socket may not be closed immediately.
306318
pub fn close(self) -> impl Future<Output = io::Result<()>> {
307319
self.inner.close()
308320
}

compio-net/src/udp.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@ impl UdpSocket {
136136

137137
/// Close the socket. If the returned future is dropped before polling, the
138138
/// socket won't be closed.
139+
///
140+
/// See [`TcpStream::close`] for more details.
141+
///
142+
/// [`TcpStream::close`]: crate::tcp::TcpStream::close
139143
pub fn close(self) -> impl Future<Output = io::Result<()>> {
140144
self.inner.close()
141145
}

compio-net/src/unix.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ impl UnixListener {
9797

9898
/// Close the socket. If the returned future is dropped before polling, the
9999
/// socket won't be closed.
100+
///
101+
/// See [`TcpStream::close`] for more details.
102+
///
103+
/// [`TcpStream::close`]: crate::tcp::TcpStream::close
100104
pub fn close(self) -> impl Future<Output = io::Result<()>> {
101105
self.inner.close()
102106
}
@@ -248,6 +252,10 @@ impl UnixStream {
248252

249253
/// Close the socket. If the returned future is dropped before polling, the
250254
/// socket won't be closed.
255+
///
256+
/// See [`TcpStream::close`] for more details.
257+
///
258+
/// [`TcpStream::close`]: crate::tcp::TcpStream::close
251259
pub fn close(self) -> impl Future<Output = io::Result<()>> {
252260
self.inner.close()
253261
}

0 commit comments

Comments
 (0)