From 774024a41be93525941ca626a92b574bec033253 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Wed, 11 May 2022 21:07:47 +0200 Subject: [PATCH] net/SocketAddress: add std::span cast operator --- src/net/SocketAddress.hxx | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/net/SocketAddress.hxx b/src/net/SocketAddress.hxx index ccbb571fa..b143c5358 100644 --- a/src/net/SocketAddress.hxx +++ b/src/net/SocketAddress.hxx @@ -33,6 +33,7 @@ #include "Features.hxx" #include +#include #ifdef _WIN32 #include // IWYU pragma: export @@ -69,6 +70,10 @@ public: size_type _size) noexcept :address(_address), size(_size) {} + explicit SocketAddress(std::span src) noexcept + :address((const struct sockaddr *)(const void *)src.data()), + size(src.size()) {} + static constexpr SocketAddress Null() noexcept { return nullptr; } @@ -154,6 +159,14 @@ public: unsigned GetPort() const noexcept; #endif + operator std::span() const noexcept { + const void *q = reinterpret_cast(address); + return { + (const std::byte *)q, + (std::size_t)size, + }; + } + /** * Return a buffer pointing to the "steady" portion of the * address, i.e. without volatile parts like the port number.