1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 04:38:11 +00:00

Kernel: Add UDPSocket::try_for_each() for fallible iteration

This API will allow users to short circuit iteration and properly
propagate errors.
This commit is contained in:
Idan Horowitz 2022-02-24 20:05:49 +02:00 committed by Andreas Kling
parent aabc8d348b
commit 6682afb5d4
2 changed files with 10 additions and 0 deletions

View file

@ -22,6 +22,15 @@ void UDPSocket::for_each(Function<void(const UDPSocket&)> callback)
});
}
ErrorOr<void> UDPSocket::try_for_each(Function<ErrorOr<void>(const UDPSocket&)> callback)
{
return sockets_by_port().with_shared([&](const auto& sockets) -> ErrorOr<void> {
for (auto& socket : sockets)
TRY(callback(*socket.value));
return {};
});
}
static Singleton<MutexProtected<HashMap<u16, UDPSocket*>>> s_map;
MutexProtected<HashMap<u16, UDPSocket*>>& UDPSocket::sockets_by_port()