Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IO::FileDescriptor::Handle #14390

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/crystal/system/unix/file_descriptor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ require "termios"
module Crystal::System::FileDescriptor
include IO::Evented

@volatile_fd : Atomic(Int32)
# Platform-specific type to represent a file descriptor handle to the operating
# system.
alias Handle = Int32

private def unbuffered_read(slice : Bytes)
evented_read(slice, "Error reading file") do
Expand Down
5 changes: 4 additions & 1 deletion src/crystal/system/win32/file_descriptor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ require "io/overlapped"
module Crystal::System::FileDescriptor
include IO::Overlapped

@volatile_fd : Atomic(LibC::Int)
# Platform-specific type to represent a file descriptor handle to the operating
# system.
alias Handle = ::LibC::Int

@system_blocking = true

private def unbuffered_read(slice : Bytes)
Expand Down
7 changes: 4 additions & 3 deletions src/io/file_descriptor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ class IO::FileDescriptor < IO
include Crystal::System::FileDescriptor
include IO::Buffered

# The raw file-descriptor. It is defined to be an `Int`, but its size is
# platform-specific.
def fd : Int
@volatile_fd : Atomic(Handle)

# Returns the raw file-descriptor handle. Its type is platform-specific.
def fd : Handle
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If Handle becomes LibC::HANDLE, wouldn't this type restriction need to be reverted back to Int?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah if we want to go that way, we might need to split between the internal and external representation of the handle. Let's take this step by step, though. FileDescriptor:Handle is supposed to be a generic name used for public APIs.

The exact type is platform-specific, but it should be a concrete type, not Int.
We could consider using the same type on all platforms in the future (SizeT probably). But I fear that would be too much of a breaking change for Unix systems right now.

@volatile_fd.get
end

Expand Down