NIOLoopBound and NIOLoopBoundBox are important tools for making mutable types Sendable.
The power of NIOLoopBoundBox is that it allows correct mutation, whilst remaining Sendable without locks and without having to use @unchecked if the mutable class makes sure to have all accesses (reading & writing) run on one particular EventLoop. This is safe as EventLoops guarantee sequentially consistent memory order across executions of different work items/events. Typically EventLoops achieve this by just being thread-bound.
These types are well used already but there's a small and common pattern which is safe but unsupported as of yet:
Initialise a NIOLoopBoundBox with a Sendable value off the loop. All further accesses (reads & writes) however happen on the loop. That's safe because of Swift's Definitive Initialisation (DI) but NIOLoopBoundBox didn't support this pattern (apart from makeEmptyBox which always initialises with nil).
Modifications:
Allow Sendable values to be provided whilst creating the type off loop.
Result:
Even more types can be safely & correctly made Sendable without using `@unchecked.
Motivation:
NIOLoopBound
andNIOLoopBoundBox
are important tools for making mutable typesSendable
. The power ofNIOLoopBoundBox
is that it allows correct mutation, whilst remainingSendable
without locks and without having to use@unchecked
if the mutable class makes sure to have all accesses (reading & writing) run on one particularEventLoop
. This is safe asEventLoop
s guarantee sequentially consistent memory order across executions of different work items/events. TypicallyEventLoop
s achieve this by just being thread-bound.These types are well used already but there's a small and common pattern which is safe but unsupported as of yet:
Initialise a
NIOLoopBoundBox
with aSendable
value off the loop. All further accesses (reads & writes) however happen on the loop. That's safe because of Swift's Definitive Initialisation (DI) butNIOLoopBoundBox
didn't support this pattern (apart frommakeEmptyBox
which always initialises withnil
).Modifications:
Sendable
values to be provided whilst creating the type off loop.Result:
Sendable
without using `@unchecked.