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::Stapled #6017

Merged
merged 3 commits into from Apr 29, 2018
Merged

Conversation

straight-shoota
Copy link
Member

@straight-shoota straight-shoota commented Apr 26, 2018

Adds IO::Stapled class which staples together two unidirectional IOs to form a single bidirectional IO. It basically delegates all read methods to reader and all write methods to writer.

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.

Copy link
Contributor

@RX14 RX14 left a 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

end
end

{% unless flag?(:win32) %}
Copy link
Contributor

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

@straight-shoota
Copy link
Member Author

Added gets delegator and removed win32 check 👍

end
end

# Creates a pair of bidirectional pipe endpints connected with each other
Copy link
Member

Choose a reason for hiding this comment

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

"endpoints"

# This class staples together two unidirectional `IO`s to form a single,
# bidirectional `IO`.
#
# Example (loopback):
Copy link
Member

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?".

@ysbaddaden
Copy link
Contributor

Is there any use case other than "mocking sockets in specs"?

@RX14
Copy link
Contributor

RX14 commented Apr 27, 2018

Is there any use to IO::Delimited other than HTTP::Multipart :)

I'd be interested in other usages, but I think this is worth it just for the speccing.

@straight-shoota
Copy link
Member Author

@ysbaddaden You could for example wrap the standard streams with HTTP or any other protocol that expects read and write on the same IO ;)

@ysbaddaden
Copy link
Contributor

At least IO::Delimited has a use case in the stdlib. I question here the actual need for IO::Stapled aside from specs, that could just use sockets.

I can appreciate the idea of IO::Stapled, but I fail to fathom actual use cases :(

@asterite
Copy link
Member

@ysbaddaden I think the idea is to use it for specs, to stub a socket. But if we are going to do that, maybe IO::Stapled should just have two IO::Memory internally, and maybe be called IO::Mock or something like that.

@straight-shoota
Copy link
Member Author

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 IO::Memory won't do because you can't really communicate through it. This would limit the usefulness as replacement for socket connections. For many use cases you'll need a pipe (hence there is also a class method to set that up).

@asterite
Copy link
Member

Hm, maybe wrapping two pipes is a good use case...

@RX14 RX14 added this to the Next milestone Apr 29, 2018
@RX14 RX14 merged commit 981c343 into crystal-lang:master Apr 29, 2018
@straight-shoota straight-shoota deleted the jm/feature/io-stapled branch April 29, 2018 13:32
chris-huxtable pushed a commit to chris-huxtable/crystal that referenced this pull request Jun 6, 2018
* Add IO::Stapled

* fixup! Add IO::Stapled

* fixup! Add IO::Stapled
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants