scatto 0.2.0

scatto - low-latency messaging primitives
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
//! pre-allocated ring buffer storage.
//!
//! the ring buffer is the core data structure for storing events in the channel.
//! it uses a fixed-size circular array with power-of-2 capacity for efficient
//! index calculations using bitwise AND instead of modulo.
//!
//! # design
//!
//! - pre-allocated slots to avoid allocation in hot path
//! - power-of-2 capacity for O(1) index calculation
//! - uses `UnsafeCell<MaybeUninit<T>>` for interior mutability and uninitialized storage
//! - zero-copy access via raw pointer operations
//!
//! # safety
//!
//! the ring buffer provides raw access methods that are unsafe. users must ensure:
//! - only one writer accesses a slot at a time
//! - readers only access slots that have been initialized
//! - proper synchronization between producers and consumers
//!
//! # example
//!
//! ```
//! use scatto::ringbuffer::RingBuffer;
//!
//! let buffer: RingBuffer<u64> = RingBuffer::new(1024);
//! assert_eq!(buffer.capacity(), 1024);
//!
//! // write at sequence 0
//! unsafe {
//!     buffer.write(0, 42);
//!     assert_eq!(*buffer.get(0), 42);
//! }
//! ```

use core::cell::UnsafeCell;
use core::mem::MaybeUninit;

/// check if a number is a power of 2.
#[inline]
const fn is_power_of_two(n: usize) -> bool {
    n != 0 && (n & (n - 1)) == 0
}

/// pre-allocated ring buffer with runtime-configurable capacity.
///
/// the buffer stores events in a circular array. capacity must be a power of 2,
/// which allows using bitwise AND instead of modulo for index calculations.
///
/// # type parameters
///
/// * `T` - the event type to store
///
/// # example
///
/// ```
/// use scatto::ringbuffer::RingBuffer;
///
/// // create a buffer for 1024 events
/// let buffer: RingBuffer<String> = RingBuffer::new(1024);
///
/// // with factory for pre-initialization
/// let buffer = RingBuffer::with_factory(1024, || String::with_capacity(256));
/// ```
pub struct RingBuffer<T> {
    /// pre-allocated event slots.
    /// UnsafeCell allows interior mutability for zero-copy writes.
    /// MaybeUninit allows uninitialized storage.
    slots: Box<[UnsafeCell<MaybeUninit<T>>]>,

    /// bitmask for efficient modulo operation: `sequence & mask` == `sequence % capacity`
    mask: usize,

    /// number of slots in the buffer (always power of 2)
    capacity: usize,
}

impl<T> RingBuffer<T> {
    /// create a new ring buffer with the given capacity.
    ///
    /// # arguments
    ///
    /// * `capacity` - number of slots (must be power of 2)
    ///
    /// # panics
    ///
    /// panics if `capacity` is not a power of 2 or is zero.
    ///
    /// # example
    ///
    /// ```
    /// use scatto::ringbuffer::RingBuffer;
    ///
    /// let buffer: RingBuffer<u64> = RingBuffer::new(1024);
    /// assert_eq!(buffer.capacity(), 1024);
    /// ```
    ///
    /// ```should_panic
    /// use scatto::ringbuffer::RingBuffer;
    ///
    /// // this will panic - 100 is not a power of 2
    /// let buffer: RingBuffer<u64> = RingBuffer::new(100);
    /// ```
    pub fn new(capacity: usize) -> Self {
        assert!(
            is_power_of_two(capacity),
            "Ring buffer capacity must be a power of 2, got {}",
            capacity
        );

        let mut slots = Vec::with_capacity(capacity);
        for _ in 0..capacity {
            slots.push(UnsafeCell::new(MaybeUninit::uninit()));
        }

        Self {
            slots: slots.into_boxed_slice(),
            mask: capacity - 1,
            capacity,
        }
    }

    /// create a new ring buffer with pre-initialized slots.
    ///
    /// the factory function is called once for each slot to initialize it.
    /// this is useful for pre-allocating resources (e.g. `Vec` with capacity).
    ///
    /// # arguments
    ///
    /// * `capacity` - number of slots (must be power of 2)
    /// * `factory` - function to create initial values
    ///
    /// # panics
    ///
    /// panics if `capacity` is not a power of 2 or is zero.
    ///
    /// # example
    ///
    /// ```
    /// use scatto::ringbuffer::RingBuffer;
    ///
    /// // Pre-allocate strings with capacity
    /// let buffer = RingBuffer::with_factory(1024, || String::with_capacity(256));
    /// ```
    pub fn with_factory<F>(capacity: usize, factory: F) -> Self
    where
        F: Fn() -> T,
    {
        assert!(
            is_power_of_two(capacity),
            "Ring buffer capacity must be a power of 2, got {}",
            capacity
        );

        let mut slots = Vec::with_capacity(capacity);
        for _ in 0..capacity {
            slots.push(UnsafeCell::new(MaybeUninit::new(factory())));
        }

        Self {
            slots: slots.into_boxed_slice(),
            mask: capacity - 1,
            capacity,
        }
    }

    /// get the buffer capacity (number of slots).
    #[inline]
    pub fn capacity(&self) -> usize {
        self.capacity
    }

    /// get the index mask for modulo operations.
    #[inline]
    pub fn mask(&self) -> usize {
        self.mask
    }

    /// convert a sequence number to a slot index.
    ///
    /// uses bitwise AND which is faster than modulo for power-of-2 sizes.
    ///
    /// # example
    ///
    /// ```
    /// use scatto::ringbuffer::RingBuffer;
    ///
    /// let buffer: RingBuffer<u64> = RingBuffer::new(8);
    /// assert_eq!(buffer.index(0), 0);
    /// assert_eq!(buffer.index(7), 7);
    /// assert_eq!(buffer.index(8), 0);  // wraps around
    /// assert_eq!(buffer.index(9), 1);
    /// ```
    #[inline(always)]
    pub fn index(&self, sequence: i64) -> usize {
        (sequence as usize) & self.mask
    }

    /// get a raw mutable pointer to the slot at the given sequence.
    ///
    /// # safety
    ///
    /// the caller must ensure:
    /// - exclusive access to this slot (no concurrent reads or writes)
    /// - the slot index is valid
    ///
    /// # example
    ///
    /// ```
    /// use scatto::ringbuffer::RingBuffer;
    ///
    /// let buffer: RingBuffer<u64> = RingBuffer::new(8);
    /// unsafe {
    ///     let ptr = buffer.get_ptr_mut(0);
    ///     ptr.write(42);
    /// }
    /// ```
    #[inline(always)]
    pub unsafe fn get_ptr_mut(&self, sequence: i64) -> *mut T {
        let idx = self.index(sequence);
        // safety: idx is always < capacity due to mask
        unsafe { (*self.slots.get_unchecked(idx).get()).as_mut_ptr() }
    }

    /// get a raw const pointer to the slot at the given sequence.
    ///
    /// # safety
    ///
    /// the caller must ensure:
    /// - the slot has been initialized (written to)
    /// - no concurrent writes to this slot
    ///
    /// # example
    ///
    /// ```
    /// use scatto::ringbuffer::RingBuffer;
    ///
    /// let buffer: RingBuffer<u64> = RingBuffer::new(8);
    /// unsafe {
    ///     buffer.write(0, 42);
    ///     let ptr = buffer.get_ptr(0);
    ///     assert_eq!(*ptr, 42);
    /// }
    /// ```
    #[inline(always)]
    pub unsafe fn get_ptr(&self, sequence: i64) -> *const T {
        let idx = self.index(sequence);
        // safety: idx is always < capacity due to mask
        unsafe { (*self.slots.get_unchecked(idx).get()).as_ptr() }
    }

    /// write an event to the slot at the given sequence.
    ///
    /// this overwrites any existing value without dropping it.
    /// for types that implement `Drop`, use [`write_and_drop`](Self::write_and_drop).
    ///
    /// # safety
    ///
    /// the caller must ensure:
    /// - exclusive access to this slot
    /// - no concurrent reads while writing
    ///
    /// # example
    ///
    /// ```
    /// use scatto::ringbuffer::RingBuffer;
    ///
    /// let buffer: RingBuffer<u64> = RingBuffer::new(8);
    /// unsafe {
    ///     buffer.write(0, 100);
    ///     buffer.write(1, 200);
    /// }
    /// ```
    #[inline(always)]
    pub unsafe fn write(&self, sequence: i64, event: T) {
        let ptr = unsafe { self.get_ptr_mut(sequence) };
        unsafe { ptr.write(event) };
    }

    /// write an event, dropping any existing value first.
    ///
    /// use this for types that implement `Drop` when reusing slots.
    ///
    /// # safety
    ///
    /// the caller must ensure:
    /// - exclusive access to this slot
    /// - the slot was previously initialized (has a valid value to drop)
    #[inline(always)]
    pub unsafe fn write_and_drop(&self, sequence: i64, event: T) {
        let ptr = unsafe { self.get_ptr_mut(sequence) };
        // drop the old value
        unsafe { core::ptr::drop_in_place(ptr) };
        // write the new value
        unsafe { ptr.write(event) };
    }

    /// read a reference to the event at the given sequence.
    ///
    /// # safety
    ///
    /// the caller must ensure:
    /// - the slot has been initialized
    /// - no concurrent writes to this slot
    /// - the returned reference does not outlive concurrent access constraints
    ///
    /// # example
    ///
    /// ```
    /// use scatto::ringbuffer::RingBuffer;
    ///
    /// let buffer: RingBuffer<u64> = RingBuffer::new(8);
    /// unsafe {
    ///     buffer.write(0, 42);
    ///     let value = buffer.get(0);
    ///     assert_eq!(*value, 42);
    /// }
    /// ```
    #[inline(always)]
    pub unsafe fn get(&self, sequence: i64) -> &T {
        unsafe { &*self.get_ptr(sequence) }
    }

    /// get a mutable reference to the event at the given sequence.
    ///
    /// this enables zero-copy in-place mutation.
    ///
    /// # safety
    ///
    /// the caller must ensure:
    /// - exclusive access to this slot
    /// - the slot has been initialized
    /// - no concurrent reads or writes
    ///
    /// # example
    ///
    /// ```
    /// use scatto::ringbuffer::RingBuffer;
    ///
    /// let buffer = RingBuffer::with_factory(8, || String::new());
    /// unsafe {
    ///     // zero-copy: modify the event in place
    ///     let event = buffer.get_mut(0);
    ///     event.push_str("hello");
    ///     assert_eq!(buffer.get(0), "hello");
    /// }
    /// ```
    #[inline(always)]
    pub unsafe fn get_mut(&self, sequence: i64) -> &mut T {
        unsafe { &mut *self.get_ptr_mut(sequence) }
    }

    /// read and move the event out of the slot.
    ///
    /// this performs a `ptr::read` which moves ownership to the caller.
    /// the slot is left in an uninitialized state.
    ///
    /// # safety
    ///
    /// the caller must ensure:
    /// - she slot has been initialized
    /// - no concurrent access to this slot
    /// - the slot wont be read again without being rewritten
    #[inline(always)]
    pub unsafe fn read(&self, sequence: i64) -> T {
        unsafe { core::ptr::read(self.get_ptr(sequence)) }
    }

    /// calculate free slots available for writing.
    ///
    /// # arguments
    ///
    /// * `producer_seq` - current producer sequence (last written)
    /// * `consumer_seq` - current consumer sequence (last consumed)
    ///
    /// # returns
    ///
    /// number of slots available for the producer to write.
    #[inline]
    pub fn free_slots(&self, producer_seq: i64, consumer_seq: i64) -> usize {
        let pending = producer_seq - consumer_seq;
        if pending < 0 {
            self.capacity
        } else {
            self.capacity - pending as usize
        }
    }
}

// safety: RingBuffer is Send if T is Send - can be transferred between threads
unsafe impl<T: Send> Send for RingBuffer<T> {}

// safety: RingBuffer is Sync if T is Send - can be shared between threads
// (actual synchronization is done by the channel using the buffer)
unsafe impl<T: Send> Sync for RingBuffer<T> {}

impl<T> Drop for RingBuffer<T> {
    fn drop(&mut self) {
        // we dont know which slots were initialized, so we can not safely drop them.
        // for types that do not need dropping (primitives), this is fine.
        // for types with drop, the channel must ensure all written values are
        // either consumed or explicitly dropped before the buffer is dropped.
        //
        // if T needs drop, the channel implementation should track which
        // sequences have been written but not consumed, and drop them.
    }
}

impl<T: core::fmt::Debug> core::fmt::Debug for RingBuffer<T> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("RingBuffer")
            .field("capacity", &self.capacity)
            .field("mask", &self.mask)
            .finish_non_exhaustive()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_new() {
        let buffer: RingBuffer<u64> = RingBuffer::new(1024);
        assert_eq!(buffer.capacity(), 1024);
        assert_eq!(buffer.mask(), 1023);
    }

    #[test]
    #[should_panic(expected = "power of 2")]
    fn test_new_non_power_of_2() {
        let _: RingBuffer<u64> = RingBuffer::new(100);
    }

    #[test]
    #[should_panic(expected = "power of 2")]
    fn test_new_zero() {
        let _: RingBuffer<u64> = RingBuffer::new(0);
    }

    #[test]
    fn test_with_factory() {
        let buffer = RingBuffer::with_factory(8, || String::with_capacity(100));
        assert_eq!(buffer.capacity(), 8);

        // verify factory was called
        unsafe {
            let s = buffer.get_mut(0);
            assert!(s.capacity() >= 100);
        }
    }

    #[test]
    fn test_index() {
        let buffer: RingBuffer<u64> = RingBuffer::new(8);

        assert_eq!(buffer.index(0), 0);
        assert_eq!(buffer.index(1), 1);
        assert_eq!(buffer.index(7), 7);
        assert_eq!(buffer.index(8), 0); // wrap
        assert_eq!(buffer.index(9), 1);
        assert_eq!(buffer.index(15), 7);
        assert_eq!(buffer.index(16), 0);
    }

    #[test]
    fn test_write_and_read() {
        let buffer: RingBuffer<u64> = RingBuffer::new(8);

        unsafe {
            buffer.write(0, 100);
            buffer.write(1, 200);
            buffer.write(7, 700);

            assert_eq!(*buffer.get(0), 100);
            assert_eq!(*buffer.get(1), 200);
            assert_eq!(*buffer.get(7), 700);
        }
    }

    #[test]
    fn test_wrap_around() {
        let buffer: RingBuffer<u64> = RingBuffer::new(4);

        unsafe {
            // fill buffer
            for i in 0..4 {
                buffer.write(i, i as u64 * 10);
            }

            // verify
            for i in 0..4 {
                assert_eq!(*buffer.get(i), i as u64 * 10);
            }

            // wrap around - write at sequence 4 overwrites sequence 0
            buffer.write(4, 40);
            assert_eq!(*buffer.get(4), 40); // same slot as 0
            assert_eq!(*buffer.get(0), 40); // same content
        }
    }

    #[test]
    fn test_get_mut_zero_copy() {
        let buffer = RingBuffer::with_factory(8, String::new);

        unsafe {
            // modify in place
            let s = buffer.get_mut(0);
            s.push_str("hello");
            s.push_str(" world");

            assert_eq!(buffer.get(0), "hello world");
        }
    }

    #[test]
    fn test_read_moves_ownership() {
        let buffer: RingBuffer<String> = RingBuffer::new(4);

        unsafe {
            buffer.write(0, String::from("test"));
            let owned: String = buffer.read(0);
            assert_eq!(owned, "test");
            // slot is now uninitialized
        }
    }

    #[test]
    fn test_free_slots() {
        let buffer: RingBuffer<u64> = RingBuffer::new(8);

        // empty buffer
        assert_eq!(buffer.free_slots(-1, -1), 8);

        // producer ahead of consumer
        assert_eq!(buffer.free_slots(3, -1), 4);
        assert_eq!(buffer.free_slots(7, -1), 0); // Full

        // consumer catching up
        assert_eq!(buffer.free_slots(7, 3), 4);
        assert_eq!(buffer.free_slots(7, 6), 7);
    }

    #[test]
    fn test_large_sequences() {
        let buffer: RingBuffer<u64> = RingBuffer::new(8);

        // test with large sequence numbers
        let large_seq: i64 = 1_000_000_000;

        unsafe {
            buffer.write(large_seq, 42);
            assert_eq!(*buffer.get(large_seq), 42);
        }

        // verify index calculation
        let expected_idx = (large_seq as usize) & 7;
        assert_eq!(buffer.index(large_seq), expected_idx);
    }

    #[test]
    fn test_debug() {
        let buffer: RingBuffer<u64> = RingBuffer::new(8);
        let debug = format!("{:?}", buffer);
        assert!(debug.contains("RingBuffer"));
        assert!(debug.contains("capacity: 8"));
    }
}