···1use std::io::{self, Read, Write};
2+use std::ops::Deref;
3use std::sync::atomic::{AtomicU64, Ordering};
4use std::time::Duration;
56+use arc_swap::RefCnt;
7use byteview::ByteView;
8use ordered_varint::Variable;
9+use rclite::Arc;
1011pub fn get_time() -> Duration {
12 std::time::SystemTime::now()
···323 }
324 }
325}
326+327+pub type ArcliteSwap<T> = arc_swap::ArcSwapAny<ArcRefCnt<T>>;
328+329+pub struct ArcRefCnt<T>(Arc<T>);
330+331+impl<T> ArcRefCnt<T> {
332+ pub fn new(value: T) -> Self {
333+ Self(Arc::new(value))
334+ }
335+}
336+337+impl<T> Deref for ArcRefCnt<T> {
338+ type Target = T;
339+340+ fn deref(&self) -> &Self::Target {
341+ &self.0
342+ }
343+}
344+345+impl<T> Clone for ArcRefCnt<T> {
346+ fn clone(&self) -> Self {
347+ Self(self.0.clone())
348+ }
349+}
350+351+// SAFETY: uhhhhhhhh copied the Arc impl from arc_swap xd
352+unsafe impl<T> RefCnt for ArcRefCnt<T> {
353+ type Base = T;
354+355+ fn into_ptr(me: Self) -> *mut Self::Base {
356+ Arc::into_raw(me.0) as *mut T
357+ }
358+359+ fn as_ptr(me: &Self) -> *mut Self::Base {
360+ // Slightly convoluted way to do this, but this avoids stacked borrows violations. The same
361+ // intention as
362+ //
363+ // me as &T as *const T as *mut T
364+ //
365+ // We first create a "shallow copy" of me - one that doesn't really own its ref count
366+ // (that's OK, me _does_ own it, so it can't be destroyed in the meantime).
367+ // Then we can use into_raw (which preserves not having the ref count).
368+ //
369+ // We need to "revert" the changes we did. In current std implementation, the combination
370+ // of from_raw and forget is no-op. But formally, into_raw shall be paired with from_raw
371+ // and that read shall be paired with forget to properly "close the brackets". In future
372+ // versions of STD, these may become something else that's not really no-op (unlikely, but
373+ // possible), so we future-proof it a bit.
374+375+ // SAFETY: &T cast to *const T will always be aligned, initialised and valid for reads
376+ let ptr = Arc::into_raw(unsafe { std::ptr::read(&me.0) });
377+ let ptr = ptr as *mut T;
378+379+ // SAFETY: We got the pointer from into_raw just above
380+ std::mem::forget(unsafe { Arc::from_raw(ptr) });
381+382+ ptr
383+ }
384+385+ unsafe fn from_ptr(ptr: *const Self::Base) -> Self {
386+ Self(unsafe { Arc::from_raw(ptr) })
387+ }
388+}