Skip to content

Commit 90fc054

Browse files
committed
Merge branch 'piever-pv/inet'
2 parents 6f5cefb + 8453aeb commit 90fc054

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

NEWS.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ Standard library changes
6868

6969
* `mean` now accepts both a function argument and a `dims` keyword ([#31576]).
7070

71+
#### Sockets
72+
73+
* Added `InetAddr` constructor from `AbstractString`, representing IP address, and `Integer`,
74+
representing port number ([#31459]).
75+
7176
#### Miscellaneous
7277

7378
* `foldr` and `mapfoldr` now work on any iterator that supports `Iterators.reverse`, not just arrays ([#31781]).

stdlib/Sockets/src/IPAddr.jl

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,36 @@ struct InetAddr{T<:IPAddr}
274274
port::UInt16
275275
end
276276

277+
"""
278+
InetAddr(ip::IPAddr, port) -> InetAddr
279+
280+
Return an `InetAddr` object from ip address `ip` and port number `port`.
281+
282+
# Examples
283+
```jldoctest
284+
julia> Sockets.InetAddr(ip"127.0.0.1", 8000)
285+
Sockets.InetAddr{IPv4}(ip"127.0.0.1", 8000)
286+
```
287+
"""
277288
InetAddr(ip::IPAddr, port) = InetAddr{typeof(ip)}(ip, port)
289+
290+
"""
291+
InetAddr(str::AbstractString, port) -> InetAddr
292+
293+
Return an `InetAddr` object from ip address `str` formatted as [`AbstractString`](@ref)
294+
and port number `port`.
295+
296+
!!! compat "Julia 1.3"
297+
This constructor requires at least Julia 1.3.
298+
299+
# Examples
300+
```jldoctest
301+
julia> Sockets.InetAddr("127.0.0.1", 8000)
302+
Sockets.InetAddr{IPv4}(ip"127.0.0.1", 8000)
303+
```
304+
"""
305+
InetAddr(str::AbstractString, port) = InetAddr(parse(IPAddr, str), port)
306+
278307
function show(io::IO, addr::InetAddr)
279308
show(io, typeof(addr))
280309
print(io, "(")

stdlib/Sockets/test/runtests.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ end
6666
@test inet.port == 1024
6767
str = "Sockets.InetAddr{$(isdefined(Main, :IPv4) ? "" : "Sockets.")IPv4}(ip\"127.0.0.1\", 1024)"
6868
@test sprint(show, inet) == str
69+
inet = Sockets.InetAddr("127.0.0.1", 1024)
70+
@test inet.host == ip"127.0.0.1"
71+
@test inet.port == 1024
6972
end
7073
@testset "InetAddr invalid port" begin
7174
@test_throws InexactError Sockets.InetAddr(IPv4(127,0,0,1), -1)

0 commit comments

Comments
 (0)