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

Return the first argument in put! #34202

Closed
wants to merge 2 commits into from
Closed
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
7 changes: 6 additions & 1 deletion base/channels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 9 additions & 3 deletions test/channels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -456,3 +457,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