Skip to content

Conversation

@plemarquand
Copy link
Contributor

Change the underlying storage type from Value? to storing Value, with optional-specific operations moved to constrained extensions.

This improves type safety by eliminating unnecessary optionality for non-optional types.

This stronger type constraint makes it more difficult for the struct to be misused. For instance in the current implementation the extension method increment() will fail to increment if the user forgot to initialize the ThreadSafeBox with a value. Now a value must be set initially before increment is called.

Also this patch prevents a race condition in the memoize methods where the call to the memoized body was not guarded by a lock which allowed multiple threads to call memoize at once and produce inconsistent results.

Finally, fix the subscript setter so it works properly with structs, as the if var value and then value set will set the value on a copy, not on the self.underlying struct. Now that the type is no longer optional this unwrapping isn't necessary and we can set directly on the value.

@plemarquand
Copy link
Contributor Author

@swift-ci test

Change the underlying storage type from `Value?` to storing `Value`, with
optional-specific operations moved to constrained extensions.

This improves type safety by eliminating unnecessary optionality for
non-optional types.

This stronger type constraint makes it more difficult for the struct to
be misused. For instance in the current implementation the extension
method `increment()` will fail to increment if the user forgot to
initialize the `ThreadSafeBox` with a value. Now a value must be set
initially before increment is called.

Also this patch prevents a race condition in the `memoize` methods where
the call to the memoized `body` was not guarded by a lock which allowed
multiple threads to call memoize at once and produce inconsistent
results.

Finally, fix the subscript setter so it works properly with `structs`,
as the `if var value` and then value set will set the value on a copy,
not on the `self.underlying` struct. Now that the type is no longer
optional this unwrapping isn't necessary and we can set directly on the
value.
@plemarquand plemarquand force-pushed the thread-safe-box-refinements branch from 71c68f2 to 516b83a Compare December 9, 2025 14:28
@plemarquand
Copy link
Contributor Author

@swift-ci test

@plemarquand
Copy link
Contributor Author

@swift-ci test macOS Platform

@plemarquand
Copy link
Contributor Author

@swift-ci test

@plemarquand
Copy link
Contributor Author

@swift-ci test windows

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants