racket / zuo

A tiny Racket for scripting
Other
271 stars 28 forks source link

Request procedure: `fd-peek` #23

Open jetjinser opened 2 months ago

jetjinser commented 2 months ago
(fd-peek handle amount) → (or/c string? eof)
  handle : handle?
  amount : (or/c integer? eof 'avail)

I hope that the handle can also be peeked. Unlike fd-read, which consumes data, it retains the data and can be read/peek next time.

I don't know if this makes sense, if I got anything wrong, please correct me. Thank you for your consideration!

mflatt commented 2 months ago

Unfortunately, this would be difficult to add, because file descriptors at the OS level generally do not support peeking. Peeking requires a layer that buffers input from a file descriptor, and adding that to Zuo would be a big change and have all sorts of complicated interactions with processes and pipes.

jetjinser commented 2 months ago

Ah, that's a pity 🥲. I still think this would be a very useful procedure, can we keep it open for the possibility of implementing it in the future?

Thank you very much for your efforts anyway :)

Matthew Flatt @.***> 于 2024年8月28日周三 05:47写道:

Unfortunately, this would be difficult to add, because file descriptors at the OS level generally do not support peeking. Peeking requires a layer that buffers input from a file descriptor, and adding that to Zuo would be a big change and have all sorts of complicated interactions with processes and pipes.

— Reply to this email directly, view it on GitHub https://github.com/racket/zuo/issues/23#issuecomment-2313636702, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALFG32CADYM6NC46LHESXFLZTTXWXAVCNFSM6AAAAABNGZGPVCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGMJTGYZTMNZQGI . You are receiving this because you authored the thread.Message ID: @.***>

lassik commented 2 months ago

The closest Unix system call is probably poll(). It does not tell how many bytes are available to read.

If amount is characters, it cannot be done with variable-length character encodings (e.g. UTF-8) without reading into a buffer.