summaryrefslogtreecommitdiff
path: root/src/collection.rs
diff options
context:
space:
mode:
authorBotahamec <botahamec@outlook.com>2024-05-27 00:33:16 -0400
committerBotahamec <botahamec@outlook.com>2024-05-27 00:33:16 -0400
commit329bb3a2230fd2c26edde116c7dad2f9a37c199d (patch)
tree312a8e9fb8769fce2d8c53e169f09f199f43d5c1 /src/collection.rs
parentd5496c35c1c62492516baca30043c3cbec1a718c (diff)
Fixed UB pertaining to Box
Diffstat (limited to 'src/collection.rs')
-rw-r--r--src/collection.rs16
1 files changed, 12 insertions, 4 deletions
diff --git a/src/collection.rs b/src/collection.rs
index 27ec1c4..8227362 100644
--- a/src/collection.rs
+++ b/src/collection.rs
@@ -1,4 +1,5 @@
use std::marker::PhantomData;
+use std::ptr::NonNull;
use crate::{key::Keyable, lockable::RawLock};
@@ -85,10 +86,17 @@ pub struct RefLockCollection<'a, L> {
// This type caches the sorting order of the locks and the fact that it doesn't
// contain any duplicates.
pub struct BoxedLockCollection<L> {
- data: Box<L>,
- locks: Vec<&'static dyn RawLock>, // As far as you know, it's static.
- // Believe it or not, saying the lifetime
- // is static when it's not isn't UB
+ // Box isn't used directly because it requires that the data not be
+ // aliased. To resolve this, we'll have to ensure that only one of the
+ // following is true at any given time:
+ //
+ // 1. We have a mutable reference to the data
+ // 2. We have immutable references to the data and locks
+ //
+ // This is enforced by having #1 be true for a mutable or owned reference
+ // to the value, and #2 is true for an immutable reference.
+ data: NonNull<L>,
+ locks: Vec<NonNull<dyn RawLock>>,
}
/// Locks a collection of locks using a retrying algorithm.