From 24be52b3f53a66f625d8f19fb3465ebf799d2c78 Mon Sep 17 00:00:00 2001 From: Idan Horowitz Date: Thu, 24 Feb 2022 20:05:10 +0200 Subject: [PATCH] Kernel: Add LocalSocket::try_for_each() for fallible iteration This API will allow users to short circuit iteration and properly propagate errors. --- Kernel/Net/LocalSocket.cpp | 9 +++++++++ Kernel/Net/LocalSocket.h | 1 + 2 files changed, 10 insertions(+) diff --git a/Kernel/Net/LocalSocket.cpp b/Kernel/Net/LocalSocket.cpp index de46196624..57b811637a 100644 --- a/Kernel/Net/LocalSocket.cpp +++ b/Kernel/Net/LocalSocket.cpp @@ -34,6 +34,15 @@ void LocalSocket::for_each(Function callback) }); } +ErrorOr LocalSocket::try_for_each(Function(const LocalSocket&)> callback) +{ + return all_sockets().with_shared([&](const auto& sockets) -> ErrorOr { + for (auto& socket : sockets) + TRY(callback(socket)); + return {}; + }); +} + ErrorOr> LocalSocket::try_create(int type) { auto client_buffer = TRY(DoubleBuffer::try_create()); diff --git a/Kernel/Net/LocalSocket.h b/Kernel/Net/LocalSocket.h index c9643f7310..4bc5c3e4a3 100644 --- a/Kernel/Net/LocalSocket.h +++ b/Kernel/Net/LocalSocket.h @@ -30,6 +30,7 @@ public: ErrorOr> recvfd(const OpenFileDescription& socket_description); static void for_each(Function); + static ErrorOr try_for_each(Function(const LocalSocket&)>); StringView socket_path() const; ErrorOr> pseudo_path(const OpenFileDescription& description) const override;