Skip to content

Commit b5ba0bb

Browse files
mamcxgefjon
andcommitted
Apply suggestions from code review
Co-authored-by: Phoebe Goldman <phoebe@clockworklabs.io> Signed-off-by: Mario Montoya <mamcx@elmalabarista.com>
1 parent 99ce284 commit b5ba0bb

9 files changed

Lines changed: 33 additions & 33 deletions

File tree

crates/bindings-csharp/Codegen.Tests/fixtures/diag/snapshots/Module#FFI.verified.cs

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/bindings-csharp/Codegen.Tests/fixtures/server/snapshots/Module#FFI.verified.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public sealed record ReducerContext : DbContext<Local>, Internal.IReducerContext
2323
public readonly Timestamp Timestamp;
2424
public readonly AuthCtx SenderAuth;
2525

26-
// **Note:** must be 0..=i32::MAX
26+
// **Note:** must be 0..=u32::MAX
2727
internal int CounterUuid;
2828

2929
// We need this property to be non-static for parity with client SDK.

crates/bindings-csharp/Codegen/Module.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1738,7 +1738,7 @@ public sealed record ReducerContext : DbContext<Local>, Internal.IReducerContext
17381738
public readonly Random Rng;
17391739
public readonly Timestamp Timestamp;
17401740
public readonly AuthCtx SenderAuth;
1741-
// **Note:** must be 0..=i32::MAX
1741+
// **Note:** must be 0..=u32::MAX
17421742
internal int CounterUuid;
17431743
// We need this property to be non-static for parity with client SDK.
17441744
public Identity Identity => Internal.IReducerContext.GetIdentity();

crates/bindings-csharp/Runtime/ProcedureContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ Timestamp time
5252
public Timestamp Timestamp { get; private set; } = time;
5353
public AuthCtx SenderAuth { get; } = AuthCtx.BuildFromSystemTables(connectionId, sender);
5454

55-
// **Note:** must be 0..=i32::MAX
55+
// **Note:** must be 0..=u32::MAX
5656
protected int CounterUuid = 0;
5757
private Internal.TxContext? txContext;
5858
private ProcedureTxContextBase? cachedUserTxContext;

crates/bindings-typescript/src/lib/reducers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ export type ReducerCtx<SchemaDef extends UntypedSchemaDef> = Readonly<{
119119
identity: Identity;
120120
timestamp: Timestamp;
121121
connectionId: ConnectionId | null;
122-
// **Note:** must be 0..=i32::MAX
122+
// **Note:** must be 0..=u32::MAX
123123
counter_uuid: { value: number };
124124
db: DbView<SchemaDef>;
125125
senderAuth: AuthCtx;

crates/bindings-typescript/src/server/procedures.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export function callProcedure(
3535
timestamp,
3636
connectionId,
3737
http: httpClient,
38-
// **Note:** must be 0..=i32::MAX
38+
// **Note:** must be 0..=u32::MAX
3939
counter_uuid: { value: Number(0) },
4040
get identity() {
4141
return new Identity(sys.identity().__identity__);

crates/bindings/src/lib.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![doc = include_str!("../README.md")]
22
// ^ if you are working on docs, go read the top comment of README.md please.
33

4-
use core::cell::{LazyCell, OnceCell, RefCell};
4+
use core::cell::{Cell, LazyCell, OnceCell, RefCell};
55
use core::ops::Deref;
66
use spacetimedb_lib::bsatn;
77
use std::rc::Rc;
@@ -978,8 +978,9 @@ pub struct ReducerContext {
978978
#[cfg(feature = "rand08")]
979979
rng: std::cell::OnceCell<StdbRng>,
980980
/// A counter used for generating UUIDv7 values.
981-
/// **Note:** must be 0..=i32::MAX
982-
counter_uuid: RefCell<i32>,
981+
/// **Note:** must be 0..=u32::MAX
982+
#[cfg(feature = "rand")]
983+
counter_uuid: Cell<u32>,
983984
}
984985

985986
impl ReducerContext {
@@ -993,7 +994,8 @@ impl ReducerContext {
993994
sender_auth: AuthCtx::internal(),
994995
#[cfg(feature = "rand08")]
995996
rng: std::cell::OnceCell::new(),
996-
counter_uuid: RefCell::new(0),
997+
#[cfg(feature = "rand")]
998+
counter_uuid: Cell::new(0),
997999
}
9981000
}
9991001

@@ -1007,7 +1009,8 @@ impl ReducerContext {
10071009
sender_auth: AuthCtx::from_connection_id_opt(connection_id),
10081010
#[cfg(feature = "rand08")]
10091011
rng: std::cell::OnceCell::new(),
1010-
counter_uuid: RefCell::new(0),
1012+
#[cfg(feature = "rand")]
1013+
counter_uuid: Cell::new(0),
10111014
}
10121015
}
10131016

@@ -1073,6 +1076,7 @@ impl ReducerContext {
10731076
/// }
10741077
/// # }
10751078
/// ```
1079+
#[cfg(feature = "rand")]
10761080
pub fn new_uuid_v7(&self) -> anyhow::Result<Uuid> {
10771081
let mut random_bytes = [0u8; 4];
10781082
self.rng().try_fill_bytes(&mut random_bytes)?;
@@ -1138,8 +1142,10 @@ pub struct ProcedureContext {
11381142
#[cfg(feature = "rand08")]
11391143
rng: std::cell::OnceCell<StdbRng>,
11401144
/// A counter used for generating UUIDv7 values.
1141-
/// **Note:** must be 0..=i32::MAX
1142-
counter_uuid: RefCell<i32>,
1145+
/// **Note:** must be 0..=u32::MAX
1146+
// Disabled when compiling without `rand`, as both v4 and v7 UUIDs have random components.
1147+
#[cfg(feature = "rand")]
1148+
counter_uuid: Cell<u32>,
11431149
}
11441150

11451151
#[cfg(feature = "unstable")]
@@ -1152,7 +1158,8 @@ impl ProcedureContext {
11521158
http: http::HttpClient {},
11531159
#[cfg(feature = "rand08")]
11541160
rng: std::cell::OnceCell::new(),
1155-
counter_uuid: RefCell::new(0),
1161+
#[cfg(feature = "rand")]
1162+
counter_uuid: Cell::new(0),
11561163
}
11571164
}
11581165
/// Read the current module's [`Identity`].
@@ -1355,7 +1362,7 @@ impl ProcedureContext {
13551362
/// }
13561363
/// # }
13571364
/// ```
1358-
#[cfg(feature = "unstable")]
1365+
#[cfg(all(feature = "unstable", feature = "rand"))]
13591366
pub fn new_uuid_v7(&self) -> anyhow::Result<Uuid> {
13601367
let mut random_bytes = [0u8; 4];
13611368
self.rng().try_fill_bytes(&mut random_bytes)?;

crates/client-api/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ bytestring = "1"
4747
tokio-tungstenite.workspace = true
4848
itoa.workspace = true
4949
derive_more = "0.99.17"
50-
uuid = { workspace = true, features = ["v4"] }
50+
uuid.workspace = true
5151
jsonwebtoken.workspace = true
5252
scopeguard.workspace = true
5353
serde_with.workspace = true

crates/sats/src/uuid.rs

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::cell::RefCell;
1+
use std::cell::Cell;
22
use std::fmt;
33

44
use crate::timestamp::Timestamp;
@@ -123,17 +123,10 @@ impl Uuid {
123123
/// uuid.to_string(),
124124
/// );
125125
/// ```
126-
pub fn from_counter_v7(counter: &RefCell<i32>, now: Timestamp, random_bytes: &[u8; 4]) -> anyhow::Result<Self> {
126+
pub fn from_counter_v7(counter: &Cell<u32>, now: Timestamp, random_bytes: &[u8; 4]) -> anyhow::Result<Self> {
127127
// Monotonic counter value (31 bits)
128-
let mut current = counter.borrow_mut();
129-
// The counter is constructed from Timestamp::UNIX_EPOCH by callers
130-
assert!(*current >= 0, "uuid counter must be non-negative");
131-
132-
let counter_val = {
133-
let next = *current;
134-
*current = next.wrapping_add(1) & 0x7FFF_FFFF;
135-
next
136-
};
128+
let counter_val = counter.get();
129+
counter.set(counter_val.wrapping_add(1) & 0x7FFF_FFFF);
137130

138131
let ts_ms = now
139132
.to_duration_since_unix_epoch()
@@ -312,7 +305,7 @@ mod test {
312305
let u_v4 = Uuid::from_random_bytes_v4([0u8; 16]);
313306
assert_eq!(u_v4.get_version(), Some(Version::V4));
314307

315-
let counter = RefCell::new(0);
308+
let counter = Cell::new(0);
316309
let ts = Timestamp::from_micros_since_unix_epoch(1_686_000_000_000);
317310
let u_v7 = Uuid::from_counter_v7(&counter, ts, &[0u8; 4]).unwrap();
318311
assert_eq!(u_v7.get_version(), Some(Version::V7));
@@ -321,16 +314,16 @@ mod test {
321314
#[test]
322315
fn wrap_around() {
323316
// Check wraparound behavior
324-
let counter = RefCell::new(i32::MAX);
317+
let counter = Cell::new(u32::MAX);
325318
let ts = Timestamp::now();
326319
let _u1 = Uuid::from_counter_v7(&counter, ts, &[0u8; 4]).unwrap();
327-
assert_eq!(0, counter.borrow().to_owned());
320+
assert_eq!(0, counter.get());
328321
}
329322

330323
#[test]
331324
#[should_panic(expected = "timestamp before unix epoch")]
332325
fn negative_timestamp_panics() {
333-
let counter = RefCell::new(0);
326+
let counter = Cell::new(0);
334327
let ts = Timestamp::from_micros_since_unix_epoch(-1);
335328
let _u = Uuid::from_counter_v7(&counter, ts, &[0u8; 4]).unwrap();
336329
}
@@ -344,13 +337,13 @@ mod test {
344337
assert_eq!(u1, u1);
345338
assert_ne!(u1, u2);
346339
// Check we start from zero
347-
let counter = RefCell::new(0);
340+
let counter = Cell::new(0);
348341
let ts = Timestamp::now();
349342
let u_start = Uuid::from_counter_v7(&counter, ts, &[0u8; 4]).unwrap();
350343
assert_eq!(u_start.get_counter(), 0);
351344
// Check ordering over many UUIDs up to the max counter value
352345
let total = 10_000_000;
353-
let counter = RefCell::new(i32::MAX - total);
346+
let counter = Cell::new(u32::MAX - total);
354347
let ts = Timestamp::now();
355348
let uuids = (0..total)
356349
.map(|_| {

0 commit comments

Comments
 (0)