From fbc5415096678e7364b78a20e99ba777542ab0fe Mon Sep 17 00:00:00 2001 From: Takafumi Arakaki Date: Thu, 26 Dec 2019 14:05:27 -0800 Subject: [PATCH 1/2] Return the first argument from `put!` --- base/channels.jl | 7 ++++++- test/channels.jl | 6 ++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/base/channels.jl b/base/channels.jl index f3b53188a791f..a9e060663e801 100644 --- a/base/channels.jl +++ b/base/channels.jl @@ -304,14 +304,19 @@ Append an item `v` to the channel `c`. Blocks if the channel is full. For unbuffered channels, blocks until a [`take!`](@ref) is performed by a different task. +Return the first argument. !!! compat "Julia 1.1" `v` now gets converted to the channel's type with [`convert`](@ref) as `put!` is called. + +!!! compat "Julia 1.4" + `put!` now returns the channel `c`; older versions of Julia returned `v`. """ function put!(c::Channel{T}, v) where T check_channel_state(c) v = convert(T, v) - return isbuffered(c) ? put_buffered(c, v) : put_unbuffered(c, v) + isbuffered(c) ? put_buffered(c, v) : put_unbuffered(c, v) + return c end function put_buffered(c::Channel, v) diff --git a/test/channels.jl b/test/channels.jl index fc5852df62dbc..a1f8d23cedbfa 100644 --- a/test/channels.jl +++ b/test/channels.jl @@ -456,3 +456,9 @@ let t = @async nothing wait(t) @test_throws ErrorException("schedule: Task not runnable") schedule(t, nothing) end + +@testset "put!(c, v) -> c" begin + c = Channel(Inf) + @test put!(c, nothing) === c + @test push!(c, nothing) === c +end From 7b3dcd5b3f15a4d8ae662ab8c35e613d4c867e20 Mon Sep 17 00:00:00 2001 From: Takafumi Arakaki Date: Thu, 26 Dec 2019 15:38:02 -0800 Subject: [PATCH 2/2] Fix tests --- test/channels.jl | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/channels.jl b/test/channels.jl index a1f8d23cedbfa..e172725a7a445 100644 --- a/test/channels.jl +++ b/test/channels.jl @@ -19,7 +19,7 @@ end c = Channel(1) @test eltype(c) == Any - @test put!(c, 1) == 1 + @test put!(c, 1) === c @test isready(c) == true @test take!(c) == 1 @test isready(c) == false @@ -35,7 +35,8 @@ end c = Channel{Int}(Inf) @test eltype(c) == Int - pvals = map(i->put!(c,i), 1:10^6) + pvals = 1:10^6 + foldl(put!, pvals; init=c) tvals = Int[take!(c) for i in 1:10^6] @test pvals == tvals @@ -162,7 +163,7 @@ using Distributed if N > 0 for i in 1:5 - @test put!(cs[i], 2) === 2 + @test put!(cs[i], 2) === cs[i] end end for i in 1:5 @@ -457,8 +458,7 @@ let t = @async nothing @test_throws ErrorException("schedule: Task not runnable") schedule(t, nothing) end -@testset "put!(c, v) -> c" begin +@testset "push!(c, v) -> c" begin c = Channel(Inf) - @test put!(c, nothing) === c @test push!(c, nothing) === c end