-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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::Stapled #6017
Add IO::Stapled #6017
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also perhaps delegate gets too
src/io/stapled.cr
Outdated
end | ||
end | ||
|
||
{% unless flag?(:win32) %} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IO.pipe
will work on windows after we merge #5623, so might as well not have this check
Added |
src/io/stapled.cr
Outdated
end | ||
end | ||
|
||
# Creates a pair of bidirectional pipe endpints connected with each other |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"endpoints"
b472703
to
c3a96dd
Compare
# This class staples together two unidirectional `IO`s to form a single, | ||
# bidirectional `IO`. | ||
# | ||
# Example (loopback): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would use another example, maybe using two IO::Memory
. When I first saw this I thought "why not use IO.pipe
directly?".
Is there any use case other than "mocking sockets in specs"? |
Is there any use to I'd be interested in other usages, but I think this is worth it just for the speccing. |
@ysbaddaden You could for example wrap the standard streams with HTTP or any other protocol that expects read and write on the same IO ;) |
At least I can appreciate the idea of |
@ysbaddaden I think the idea is to use it for specs, to stub a socket. But if we are going to do that, maybe |
Yes, the main purpose of this is obviously mocking socket connections, but it can also be generally useful for multiplexing two unidirectional read/write streams in one IO. Limiting to |
Hm, maybe wrapping two pipes is a good use case... |
* Add IO::Stapled * fixup! Add IO::Stapled * fixup! Add IO::Stapled
Adds
IO::Stapled
class which staples together two unidirectionalIO
s to form a single bidirectionalIO
. It basically delegates all read methods toreader
and all write methods towriter
.This class is an extension to unidirectional IOs (like
IO::Memory
,IO.pipe
) when you need true bidirectional IOs, for example as a replacement for socket streams.This is particularly useful for speccing code that would normally work with a socket. You don't need to create an actual socket but can just staple together two
IO::Memory
.