Przeglądaj źródła

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

This API will allow users to short circuit iteration and properly
propagate errors.
Idan Horowitz 3 lat temu
rodzic
commit
24be52b3f5
2 zmienionych plików z 10 dodań i 0 usunięć
  1. 9 0
      Kernel/Net/LocalSocket.cpp
  2. 1 0
      Kernel/Net/LocalSocket.h

+ 9 - 0
Kernel/Net/LocalSocket.cpp

@@ -34,6 +34,15 @@ void LocalSocket::for_each(Function<void(const LocalSocket&)> callback)
     });
 }
 
+ErrorOr<void> LocalSocket::try_for_each(Function<ErrorOr<void>(const LocalSocket&)> callback)
+{
+    return all_sockets().with_shared([&](const auto& sockets) -> ErrorOr<void> {
+        for (auto& socket : sockets)
+            TRY(callback(socket));
+        return {};
+    });
+}
+
 ErrorOr<NonnullRefPtr<LocalSocket>> LocalSocket::try_create(int type)
 {
     auto client_buffer = TRY(DoubleBuffer::try_create());

+ 1 - 0
Kernel/Net/LocalSocket.h

@@ -30,6 +30,7 @@ public:
     ErrorOr<NonnullRefPtr<OpenFileDescription>> recvfd(const OpenFileDescription& socket_description);
 
     static void for_each(Function<void(const LocalSocket&)>);
+    static ErrorOr<void> try_for_each(Function<ErrorOr<void>(const LocalSocket&)>);
 
     StringView socket_path() const;
     ErrorOr<NonnullOwnPtr<KString>> pseudo_path(const OpenFileDescription& description) const override;