Skip to content

Commit 3751623

Browse files
committed
Add asIpUnsafe to GenSocketAddress
1 parent a8624ad commit 3751623

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

shared/src/main/scala/com/comcast/ip4s/GenSocketAddress.scala

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,14 @@ package com.comcast.ip4s
2525
* This trait is left open for extension, allowing other address types to be defined.
2626
* When using this trait, pattern match on the supported subtypes.
2727
*/
28-
abstract class GenSocketAddress private[ip4s] ()
28+
abstract class GenSocketAddress private[ip4s] () {
29+
30+
/** Downcasts this address to a `SocketAddress[IpAddress]`.
31+
*
32+
* @throws UnsupportedOperationException if this address is not a `SocketAddress[IpAddress]`
33+
*/
34+
def asIpUnsafe: SocketAddress[IpAddress] = this match {
35+
case addr @ SocketAddress(_: IpAddress, _) => addr.asInstanceOf[SocketAddress[IpAddress]]
36+
case other => throw new UnsupportedOperationException(s"asIpUnsafe only supported on SocketAddress[IpAddress]: $other")
37+
}
38+
}

0 commit comments

Comments
 (0)