arcmut 0.1.0

Introduce ArcMut, utility for FFI.
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
#![doc = include_str!("../README.md")]
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![cfg_attr(docsrs, allow(unused_attributes))]
#![deny(missing_docs, warnings)]

extern crate alloc;

#[cfg(feature = "std")]
extern crate std;

use alloc::boxed::Box;

#[cfg(not(loom))]
use core::sync::atomic::{AtomicPtr, AtomicUsize, Ordering};
use core::{
  hash::Hasher,
  ops::{Deref, DerefMut},
  ptr,
};

#[cfg(loom)]
use loom::sync::atomic::{AtomicPtr, AtomicUsize, Ordering};

#[cfg(loom)]
trait AtomicMut<T> {}

#[cfg(not(loom))]
trait AtomicMut<T> {
  fn with_mut<F, R>(&mut self, f: F) -> R
  where
    F: FnOnce(&mut *mut T) -> R;
}

#[cfg(not(loom))]
impl<T> AtomicMut<T> for AtomicPtr<T> {
  fn with_mut<F, R>(&mut self, f: F) -> R
  where
    F: FnOnce(&mut *mut T) -> R,
  {
    f(self.get_mut())
  }
}

struct Data<T> {
  refs: AtomicUsize,
  data: T,
}

/// A reference-counted pointer to a value of type `T`, which can be mutated.
///
/// > **Note: This struct is not thread-safe!!!**
///
/// `ArcMut<T>` provides shared ownership of a value of type `T`, allocated
/// in the heap. Invoking `clone` on `ArcMut` produces another pointer to the
/// same allocation in the heap. When the last `ArcMut` pointer to a given
/// allocation is destroyed, the value stored in that allocation (often
/// referred to as "inner value") is also dropped.
///
/// This is similar to `std::sync::Arc`, but it allows interior mutability.
///
/// In normal circumstances, you are not expected to use this type, but when you
/// are writing FFI code, you may need to use this type to share a value between
/// Rust and other languages, and again, if the code in other languages is concurrent,
/// you need to use a `Arc<Mutex<T>>` instead.
pub struct ArcMut<T> {
  ptr: AtomicPtr<()>,
  _marker: core::marker::PhantomData<T>,
}

impl<T> From<T> for ArcMut<T> {
  /// Converts a `T` into an `Arc<T>`
  ///
  /// The conversion moves the value into a
  /// newly allocated `Arc`. It is equivalent to
  /// calling `Arc::new(t)`.
  ///
  /// # Example
  /// ```rust
  /// # use arcmut::ArcMut;
  /// let x = 5;
  /// let arc = ArcMut::new(5);
  ///
  /// assert_eq!(ArcMut::from(x), arc);
  /// ```
  fn from(t: T) -> Self {
    Self::new(t)
  }
}

impl<T: Default> Default for ArcMut<T> {
  fn default() -> Self {
    Self::new(Default::default())
  }
}

impl<T> Clone for ArcMut<T> {
  fn clone(&self) -> Self {
    unsafe {
      let shared: *mut Data<T> = self.ptr.load(Ordering::Relaxed).cast();

      let old_size = (*shared).refs.fetch_add(1, Ordering::Release);
      if old_size > usize::MAX >> 1 {
        abort();
      }

      // Safety:
      // The ptr is always non-null, and the data is only deallocated when the
      // last ArcMut is dropped.
      Self {
        ptr: AtomicPtr::new(shared as *mut ()),
        _marker: core::marker::PhantomData,
      }
    }
  }
}

impl<T> Deref for ArcMut<T> {
  type Target = T;

  fn deref(&self) -> &Self::Target {
    unsafe {
      let shared: *mut Data<T> = self.ptr.load(Ordering::Relaxed).cast();
      &(*shared).data
    }
  }
}

impl<T> DerefMut for ArcMut<T> {
  fn deref_mut(&mut self) -> &mut Self::Target {
    unsafe {
      let shared: *mut Data<T> = self.ptr.load(Ordering::Relaxed).cast();
      &mut (*shared).data
    }
  }
}

impl<T> core::borrow::Borrow<T> for ArcMut<T> {
  fn borrow(&self) -> &T {
    self
  }
}

impl<T> core::borrow::BorrowMut<T> for ArcMut<T> {
  fn borrow_mut(&mut self) -> &mut T {
    self
  }
}

impl<T> AsRef<T> for ArcMut<T> {
  fn as_ref(&self) -> &T {
    self
  }
}

impl<T> AsMut<T> for ArcMut<T> {
  fn as_mut(&mut self) -> &mut T {
    self
  }
}

impl<T> core::fmt::Debug for ArcMut<T>
where
  T: core::fmt::Debug,
{
  fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
    core::fmt::Debug::fmt(&**self, f)
  }
}

impl<T> core::fmt::Display for ArcMut<T>
where
  T: core::fmt::Display,
{
  fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
    core::fmt::Display::fmt(&**self, f)
  }
}

impl<T> PartialEq for ArcMut<T>
where
  T: PartialEq,
{
  fn eq(&self, other: &Self) -> bool {
    **self == **other
  }
}

impl<T> Eq for ArcMut<T> where T: Eq {}

impl<T: core::hash::Hash> core::hash::Hash for ArcMut<T> {
  fn hash<H: Hasher>(&self, state: &mut H) {
    (**self).hash(state)
  }
}

impl<T> PartialOrd for ArcMut<T>
where
  T: PartialOrd,
{
  fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
    (**self).partial_cmp(&**other)
  }
}

impl<T> Ord for ArcMut<T>
where
  T: Ord,
{
  fn cmp(&self, other: &Self) -> core::cmp::Ordering {
    (**self).cmp(&**other)
  }
}

impl<T> Drop for ArcMut<T> {
  fn drop(&mut self) {
    unsafe {
      self.ptr.with_mut(|shared| {
        let shared: *mut Data<T> = shared.cast();
        // `Shared` storage... follow the drop steps from Arc.
        if (*shared).refs.fetch_sub(1, Ordering::Release) != 1 {
          return;
        }

        // This fence is needed to prevent reordering of use of the data and
        // deletion of the data.  Because it is marked `Release`, the decreasing
        // of the reference count synchronizes with this `Acquire` fence. This
        // means that use of the data happens before decreasing the reference
        // count, which happens before this fence, which happens before the
        // deletion of the data.
        //
        // As explained in the [Boost documentation][1],
        //
        // > It is important to enforce any possible access to the object in one
        // > thread (through an existing reference) to *happen before* deleting
        // > the object in a different thread. This is achieved by a "release"
        // > operation after dropping a reference (any access to the object
        // > through this reference must obviously happened before), and an
        // > "acquire" operation before deleting the object.
        //
        // [1]: (www.boost.org/doc/libs/1_55_0/doc/html/atomic/usage_examples.html)
        //
        // Thread sanitizer does not support atomic fences. Use an atomic load
        // instead.
        (*shared).refs.load(Ordering::Acquire);
        // Drop the data
        let _ = Box::from_raw(shared);
      });
    }
  }
}

impl<T> ArcMut<T> {
  /// Constructs a new `ArcMut<T>`.
  ///
  /// # Examples
  ///
  /// ```
  /// use arcmut::ArcMut;
  ///
  /// let five = ArcMut::new(5);
  /// ```
  pub fn new(data: T) -> Self {
    let data = Box::new(Data {
      refs: AtomicUsize::new(1),
      data,
    });

    Self {
      ptr: AtomicPtr::new(Box::into_raw(data) as *mut ()),
      _marker: core::marker::PhantomData,
    }
  }

  /// Returns `true` if the two ArcMuts point to the same allocation in a vein similar to [`ptr::eq`]. This function ignores the metadata of dyn Trait pointers.
  ///
  /// # Examples
  ///
  /// ```
  /// use arcmut::ArcMut;
  ///
  /// let five = ArcMut::new(5);
  /// let same_five = ArcMut::clone(&five);
  /// let other_five = ArcMut::new(5);
  ///
  /// assert!(ArcMut::ptr_eq(&five, &same_five));
  /// assert!(!ArcMut::ptr_eq(&five, &other_five));
  /// ```
  pub fn ptr_eq(this: &Self, other: &Self) -> bool {
    let this = this.ptr.load(Ordering::Relaxed);
    let other = other.ptr.load(Ordering::Relaxed);
    ptr::eq(this, other)
  }

  /// Gets the number of strong (`ArcMut`) pointers to this allocation.
  ///
  /// # Safety
  ///
  /// This method by itself is safe, but using it correctly requires extra care.
  /// Another thread can change the strong count at any time,
  /// including potentially between calling this method and acting on the result.
  ///
  /// # Examples
  ///
  /// ```
  /// use arcmut::ArcMut;
  ///
  /// let five = ArcMut::new(5);
  /// let _also_five = ArcMut::clone(&five);
  ///
  /// // This assertion is deterministic because we haven't shared
  /// // the `Arc` between threads.
  /// assert_eq!(2, ArcMut::count(&five));
  /// ```
  pub fn count(this: &Self) -> usize {
    unsafe {
      let shared: *mut Data<T> = this.ptr.load(Ordering::Relaxed).cast();
      (*shared).refs.load(Ordering::Acquire)
    }
  }
}

unsafe impl<T: Sync + Send> Send for ArcMut<T> {}
unsafe impl<T: Sync + Send> Sync for ArcMut<T> {}

#[cfg(feature = "serde")]
const _: () = {
  use serde::{Deserialize, Serialize};

  impl<T> Serialize for ArcMut<T>
  where
    T: Serialize,
  {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
      S: serde::Serializer,
    {
      (**self).serialize(serializer)
    }
  }

  impl<'de, T> Deserialize<'de> for ArcMut<T>
  where
    T: Deserialize<'de>,
  {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
      D: serde::Deserializer<'de>,
    {
      T::deserialize(deserializer).map(Self::new)
    }
  }
};

#[inline(never)]
#[cold]
fn abort() -> ! {
  #[cfg(feature = "std")]
  {
    std::process::abort()
  }

  #[cfg(not(feature = "std"))]
  {
    struct Abort;
    impl Drop for Abort {
      fn drop(&mut self) {
        panic!();
      }
    }
    let _a = Abort;
    panic!("abort");
  }
}

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

  macro_rules! test {
    ($($tt:tt)*) => {
      #[cfg(loom)]
      {
        loom::model(|| {
          $($tt)*
        });
      }

      #[cfg(not(loom))]
      {
        $($tt)*
      }
    };
  }

  #[test]
  fn test_clone() {
    test! {
      let x = ArcMut::new(5);
      let y = x.clone();
      assert_eq!(5, *x);
      assert_eq!(5, *y);
    }
  }

  #[test]
  fn test_deref() {
    test! {
      let x = ArcMut::new(5);
      assert_eq!(5, *x);
    }
  }

  #[test]
  fn test_deref_mut() {
    test! {
      let mut x = ArcMut::new(5);
      *x = 10;
      assert_eq!(10, *x);
    }
  }

  #[test]
  fn test_as_ref() {
    test! {
      let x = ArcMut::new(5);
      assert_eq!(5, *x.as_ref());
    }
  }

  #[test]
  fn test_as_mut() {
    test! {
      let mut x = ArcMut::new(5);
      *x.as_mut() = 10;
      assert_eq!(10, *x.as_ref());
    }
  }

  #[test]
  fn test_default() {
    test! {
      let x: ArcMut<i32> = Default::default();
      assert_eq!(0, *x);
    }
  }

  #[test]
  fn test_from() {
    test! {
      let x = 5;
      let x = ArcMut::from(x);
      assert_eq!(5, *x);
    }
  }

  #[test]
  fn test_partial_eq() {
    test! {
      let x = ArcMut::new(5);
      let y = ArcMut::new(5);
      assert_eq!(x, y);
    }
  }

  #[test]
  fn test_partial_ord() {
    test! {
      let x = ArcMut::new(5);
      let y = ArcMut::new(5);
      assert_eq!(x.partial_cmp(&y), Some(core::cmp::Ordering::Equal));
    }
  }

  #[test]
  fn test_ord() {
    test! {
      let x = ArcMut::new(5);
      let y = ArcMut::new(5);
      assert_eq!(x.cmp(&y), core::cmp::Ordering::Equal)
    }
  }

  #[test]
  #[cfg(feature = "std")]
  fn test_hash() {
    test! {
      use core::hash::{Hash, Hasher};
      let x = ArcMut::new(5);
      let mut hasher = std::collections::hash_map::DefaultHasher::new();
      x.hash(&mut hasher);
      let hash = hasher.finish();
      assert_eq!(hash, 14828406784900857967u64);
    }
  }

  #[test]
  fn test_fmt_debug() {
    test! {
      let x = ArcMut::new(5);
      assert_eq!("5", format!("{:?}", x));
    }
  }

  #[test]
  fn test_fmt_display() {
    test!(
      let x = ArcMut::new(5);
      assert_eq!("5", format!("{}", x));
    );
  }

  #[test]
  fn test_ptr_eq() {
    test!(
      let x = ArcMut::new(5);
    let y = ArcMut::clone(&x);
    let z = ArcMut::new(5);
    assert!(ArcMut::ptr_eq(&x, &y));
    assert!(!ArcMut::ptr_eq(&x, &z));
    );
  }

  #[test]
  fn test_count() {
    test!(
      let x = ArcMut::new(5);
    assert_eq!(1, ArcMut::count(&x));
    let y = ArcMut::clone(&x);
    assert_eq!(2, ArcMut::count(&x));
    assert_eq!(2, ArcMut::count(&y));
    drop(x);
    assert_eq!(1, ArcMut::count(&y));
    drop(y);
    );
  }

  #[test]
  #[cfg(feature = "std")]
  fn test_thread() {
    #[cfg(loom)]
    use loom::thread;

    #[cfg(not(loom))]
    use std::thread;

    test!(
      let arc = ArcMut::new(alloc::vec![100u8; 100]);
      for _ in 0..2 {
        let data = arc.clone();
        thread::spawn(move || {
          assert_eq!(data.len(), 100,);
          assert_eq!(data.as_ref(), &[100u8; 100]);
        });
      }
      while ArcMut::count(&arc) > 1 {
        thread::yield_now();
      }

      assert_eq!(arc.len(), 100,);

      assert_eq!(arc.as_ref(), &[100u8; 100]);
    );
  }
}