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

Define push! and popfirst! for AbstractChannel #34274

Merged
merged 8 commits into from
Aug 29, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ New library features

Standard library changes
------------------------

* The `nextprod` function now accepts tuples and other array types for its first argument ([#35791]).
* The function `isapprox(x,y)` now accepts the `norm` keyword argument also for numeric (i.e., non-array) arguments `x` and `y` ([#35883]).
* `view`, `@view`, and `@views` now work on `AbstractString`s, returning a `SubString` when appropriate ([#35879]).
Expand All @@ -82,6 +81,7 @@ Standard library changes
* `first` and `last` functions now accept an integer as second argument to get that many
leading or trailing elements of any iterable ([#34868]).
* `intersect` on `CartesianIndices` now returns `CartesianIndices` instead of `Vector{<:CartesianIndex}` ([#36643]).
* `push!(c::Channel, v)` now returns channel `c`. Previously, it returned the pushed value `v` ([#34202]).
* `RegexMatch` objects can now be probed for whether a named capture group exists within it through `haskey()` ([#36717]).
* A new standard library `TOML` has been added for parsing and printing [TOML files](https://toml.io) ([#37034]).

Expand Down
7 changes: 3 additions & 4 deletions base/channels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ Representation of a channel passing objects of type `T`.
"""
abstract type AbstractChannel{T} end

push!(c::AbstractChannel, v) = (put!(c, v); c)
popfirst!(c::AbstractChannel) = take!(c)

"""
Channel{T=Any}(size::Int=0)

Expand Down Expand Up @@ -347,8 +350,6 @@ function put_unbuffered(c::Channel, v)
return v
end

push!(c::Channel, v) = put!(c, v)

"""
fetch(c::Channel)

Expand Down Expand Up @@ -395,8 +396,6 @@ function take_buffered(c::Channel)
end
end

popfirst!(c::Channel) = take!(c)

# 0-size channel
function take_unbuffered(c::Channel{T}) where T
lock(c)
Expand Down
5 changes: 5 additions & 0 deletions test/channels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,11 @@ let t = @async nothing
@test_throws ErrorException("schedule: Task not runnable") schedule(t, nothing)
end

@testset "push!(c, v) -> c" begin
c = Channel(Inf)
@test push!(c, nothing) === c
end

# Channel `show`
let c = Channel(3)
@test repr(c) == "Channel{Any}(3)"
Expand Down