Skip to content

Commit

Permalink
Set IPAddr subtypes as scalars for broadcasting (#32133)
Browse files Browse the repository at this point in the history
* Set IPAddr subtypes as scalars for broadcasting

* Add news
  • Loading branch information
omus authored May 27, 2019
1 parent 3243b94 commit 6ccd5c4
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 0 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Standard library changes
* Cmd interpolation (`` `$(x::Cmd) a b c` `` where) now propagates `x`'s process flags
(environment, flags, working directory, etc) if `x` is the first interpolant and errors
otherwise ([#24353]).
* `IPAddr` subtypes now behave like scalars when used in broadcasting ([#32133]).

#### LinearAlgebra

Expand Down
3 changes: 3 additions & 0 deletions stdlib/Sockets/src/IPAddr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ abstract type IPAddr end
Base.isless(a::T, b::T) where {T<:IPAddr} = isless(a.host, b.host)
(dt::Type{<:Integer})(ip::IPAddr) = dt(ip.host)::dt

# Allow IP addresses to broadcast as unwrapped scalars
Base.Broadcast.broadcastable(ip::IPAddr) = Ref(ip)

struct IPv4 <: IPAddr
host::UInt32
IPv4(host::UInt32) = new(host)
Expand Down
5 changes: 5 additions & 0 deletions stdlib/Sockets/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ end
@test_throws MethodError sort([ip"2.3.4.5", ip"1.2.3.4", ip"2001:1:2::1"])
end

@testset "broadcastable" begin
@test size(ip"127.0.0.1" .== ip"127.0.0.1") == ()
@test size(ip"::1" .== ip"::1") == ()
end

@testset "RFC 5952 Compliance" begin
@test repr(ip"2001:db8:0:0:0:0:2:1") == "ip\"2001:db8::2:1\""
@test repr(ip"2001:0db8::0001") == "ip\"2001:db8::1\""
Expand Down

1 comment on commit 6ccd5c4

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Executing the daily benchmark build, I will reply here when finished:

@nanosoldier runbenchmarks(ALL, isdaily = true)

Please sign in to comment.