-
-
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 documentation for Channel#send and Channel#close (crystal-lang#8346) #8356
Merged
straight-shoota
merged 6 commits into
crystal-lang:master
from
lbarasti:document-channel-send-and-close-behaviour
Nov 26, 2019
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
917ae72
Add documentation for Channel#send and Channel#close (crystal-lang#8346)
lbarasti 62c796b
Clarify docs for #close
lbarasti fd53245
Clarify docs for #send
lbarasti 287c8b8
Document and test the behaviour of Channel when #close is called more…
lbarasti 2150b52
Clarify docs for #close and make #close test more comprehensive
lbarasti afe17aa
Merge branch 'master' into document-channel-send-and-close-behaviour
lbarasti File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -167,6 +167,13 @@ class Channel(T) | |
end | ||
end | ||
|
||
# Closes the channel. | ||
# The method prevents any new value from being sent to / received from the channel. | ||
# All fibers blocked in `send` or `receive` will be awakened with `Channel::ClosedError` | ||
# | ||
# Both awaiting and subsequent calls to `#send` will consider the channel closed. | ||
# All items successfully sent to the channel can be received, before `#receive` considers the channel closed. | ||
# Calling `#close` on a closed channel does not have any effect. | ||
def close : Nil | ||
sender_list = Crystal::PointerLinkedList(Sender(T)).new | ||
receiver_list = Crystal::PointerLinkedList(Receiver(T)).new | ||
|
@@ -186,6 +193,11 @@ class Channel(T) | |
@closed | ||
end | ||
|
||
# Sends a value to the channel. | ||
# If the channel has spare capacity, then the method returns immediately. | ||
# Otherwise, this method blocks the calling fiber until another fiber calls `#receive` on the channel. | ||
# | ||
# Raises `ClosedError` if the channel is closed or closes while waiting on a full channel. | ||
def send(value : T) | ||
sender = Sender(T).new | ||
|
||
|
@@ -237,13 +249,15 @@ class Channel(T) | |
end | ||
|
||
# Receives a value from the channel. | ||
# If there is a value waiting, it is returned immediately. Otherwise, this method blocks until a value is sent to the channel. | ||
# If there is a value waiting, then it is returned immediately. Otherwise, this method blocks until a value is sent to the channel. | ||
# | ||
# Raises `ClosedError` if the channel is closed or closes while waiting for receive. | ||
# | ||
# ``` | ||
# channel = Channel(Int32).new | ||
# channel.send(1) | ||
# spawn do | ||
# channel.send(1) | ||
# end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. An alternative way to fix the code snippet would be to go buffered channel = Channel(Int32).new(1)
channel.send(42)
channel.receive # => 42 |
||
# channel.receive # => 1 | ||
# ``` | ||
def receive | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Added
then
for consistency with the docs above - both are valid, I believe