From 675a87ce939988e282358659ae657ebb24cb8859 Mon Sep 17 00:00:00 2001 From: Takafumi Arakaki Date: Sun, 5 Jan 2020 21:44:11 -0800 Subject: [PATCH 1/3] Define push! and popfirst! for AbstractChannel --- NEWS.md | 2 ++ base/channels.jl | 7 +++---- stdlib/Distributed/test/distributed_exec.jl | 2 ++ test/channels.jl | 5 +++++ 4 files changed, 12 insertions(+), 4 deletions(-) diff --git a/NEWS.md b/NEWS.md index 139c9238de7f1..2d1a10ad82da1 100644 --- a/NEWS.md +++ b/NEWS.md @@ -28,6 +28,8 @@ New library features Standard library changes ------------------------ +* `push!(c::Channel, v)` now returns channel `c`. Previously, it returned the pushed value + `v` ([#34202]). #### LinearAlgebra diff --git a/base/channels.jl b/base/channels.jl index f3b53188a791f..e2dc9cd86ae84 100644 --- a/base/channels.jl +++ b/base/channels.jl @@ -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) @@ -348,8 +351,6 @@ function put_unbuffered(c::Channel, v) return v end -push!(c::Channel, v) = put!(c, v) - """ fetch(c::Channel) @@ -396,8 +397,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) diff --git a/stdlib/Distributed/test/distributed_exec.jl b/stdlib/Distributed/test/distributed_exec.jl index f4c903e6b719b..ca9305fb907da 100644 --- a/stdlib/Distributed/test/distributed_exec.jl +++ b/stdlib/Distributed/test/distributed_exec.jl @@ -378,6 +378,7 @@ function test_channel(c) put!(c, 1) put!(c, "Hello") put!(c, 5.0) + @test push!(c, :World) === c @test isready(c) == true @test isopen(c) == true @@ -387,6 +388,7 @@ function test_channel(c) @test take!(c) == "Hello" @test fetch(c) == 5.0 @test take!(c) == 5.0 + @test popfirst!(c) == :World @test isready(c) == false @test isopen(c) == true close(c) diff --git a/test/channels.jl b/test/channels.jl index fc5852df62dbc..7401cedb2f0da 100644 --- a/test/channels.jl +++ b/test/channels.jl @@ -456,3 +456,8 @@ let t = @async nothing wait(t) @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 From 02a29847a3cf1b220711b70f797b490cc2f6b461 Mon Sep 17 00:00:00 2001 From: Takafumi Arakaki Date: Sun, 5 Jan 2020 22:16:35 -0800 Subject: [PATCH 2/3] Don't test with RemoteChannel as it is not a subtype of AbstractChannel --- stdlib/Distributed/test/distributed_exec.jl | 2 -- 1 file changed, 2 deletions(-) diff --git a/stdlib/Distributed/test/distributed_exec.jl b/stdlib/Distributed/test/distributed_exec.jl index ca9305fb907da..f4c903e6b719b 100644 --- a/stdlib/Distributed/test/distributed_exec.jl +++ b/stdlib/Distributed/test/distributed_exec.jl @@ -378,7 +378,6 @@ function test_channel(c) put!(c, 1) put!(c, "Hello") put!(c, 5.0) - @test push!(c, :World) === c @test isready(c) == true @test isopen(c) == true @@ -388,7 +387,6 @@ function test_channel(c) @test take!(c) == "Hello" @test fetch(c) == 5.0 @test take!(c) == 5.0 - @test popfirst!(c) == :World @test isready(c) == false @test isopen(c) == true close(c) From 30f530609b75f69c2e1607f44c989d74319d19f0 Mon Sep 17 00:00:00 2001 From: Takafumi Arakaki Date: Mon, 20 Jul 2020 11:29:45 -0700 Subject: [PATCH 3/3] Fix indentation --- test/channels.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/channels.jl b/test/channels.jl index 76704d8d11775..6a97889cea552 100644 --- a/test/channels.jl +++ b/test/channels.jl @@ -544,7 +544,7 @@ end @testset "push!(c, v) -> c" begin c = Channel(Inf) @test push!(c, nothing) === c - end +end # Channel `show` let c = Channel(3)