Skip to content

Commit 605720e

Browse files
committed
std/os: add File.SetReadDeadline, File.SetWriteDeadline
1 parent f74ce04 commit 605720e

2 files changed

Lines changed: 35 additions & 0 deletions

File tree

std/internal/poll/fd.jule

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ use "std/sys"
1010
// Throw when a file descriptor is used after it has been closed.
1111
let mut ErrFileClosing = errors::New("use of closed file")
1212

13+
// Throw when a request is made to set a deadline
14+
// on a file type that does not use the poller.
15+
let mut ErrNoDeadline = errors::New("file type does not support deadline")
16+
1317
// FD flags. Kind flags should not be combined like File|Console|Socket.
1418
const (
1519
File = 1 << iota // Standard file descriptor.

std/os/file.jule

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use "std/internal/poll"
66
use "std/io"
77
use "std/sys"
8+
use "std/time"
89
use "std/unsafe"
910

1011
// The name of the operating system's “null device.”
@@ -213,6 +214,36 @@ impl File {
213214
ret self.fd.ShouldAsync()
214215
}
215216

217+
// Sets or updates a deadline for the future read operations.
218+
// If the deadline is given as 0, the deadline is cleared.
219+
// The deadline is evaluated against an absolute point in time.
220+
// In practice, the given deadline is equivalent to `time::Now().Add(deadline)`.
221+
// It does not apply per operation, but remains valid until this absolute time.
222+
// Any operation initiated after this deadline has passed will fail.
223+
//
224+
// If file does not supports async I/O, throws error.
225+
fn SetReadDeadline(mut *self, deadline: time::Duration)! {
226+
if !self.ShouldAsync() {
227+
throw poll::ErrNoDeadline
228+
}
229+
self.fd.SetReadDeadline(deadline)
230+
}
231+
232+
// Sets or updates a deadline for the future write operations.
233+
// If the deadline is given as 0, the deadline is cleared.
234+
// The deadline is evaluated against an absolute point in time.
235+
// In practice, the given deadline is equivalent to `time::Now().Add(deadline)`.
236+
// It does not apply per operation, but remains valid until this absolute time.
237+
// Any operation initiated after this deadline has passed will fail.
238+
//
239+
// If file does not supports async I/O, throws error.
240+
fn SetWriteDeadline(mut *self, deadline: time::Duration)! {
241+
if !self.ShouldAsync() {
242+
throw poll::ErrNoDeadline
243+
}
244+
self.fd.SetWriteDeadline(deadline)
245+
}
246+
216247
// Writes bytes to handle and returns written byte count.
217248
// The number of bytes written can never exceed the length of the buf.
218249
async fn Write(mut *self, buf: []byte)!: (n: int) {

0 commit comments

Comments
 (0)