aol 0.3.2

Generic purpose append only log implementation.
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
#![doc = include_str!("../README.md")]
#![cfg_attr(not(any(feature = "std", test)), no_std)]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![cfg_attr(docsrs, allow(unused_attributes))]
#![deny(missing_docs, warnings)]
#![allow(clippy::type_complexity)]

#[cfg(not(any(feature = "std", feature = "alloc")))]
compile_error!("`aol` cannot be compiled when both `std` is disabled.");

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

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

#[cfg(feature = "std")]
use core::mem;

mod types;
pub use types::*;

mod impls;

mod snapshot;
pub use snapshot::*;

pub use dbutils::{checksum, leb128};
pub use either;

/// A vacant buffer that can be filled with bytes.
pub mod buffer {
  pub use dbutils::{buffer::VacantBuffer, error::*};
}

/// Append-only log implementation based on [`std::fs`].
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
mod fs;
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
pub use fs::*;

#[cfg(feature = "std")]
const MAGIC_LEN: usize = mem::size_of::<u16>();

/// Magic text for the append only log, this will never be changed.
#[cfg(feature = "std")]
const MAGIC_TEXT: &[u8] = b"aol!";
#[cfg(feature = "std")]
const MAGIC_TEXT_LEN: usize = MAGIC_TEXT.len();
#[cfg(feature = "std")]
const MAGIC_VERSION_LEN: usize = mem::size_of::<u16>();
#[cfg(feature = "std")]
const ENTRY_HEADER_SIZE: usize = 1 + LEN_BUF_SIZE; // flag + len
#[cfg(feature = "std")]
const FIXED_ENTRY_LEN: usize = ENTRY_HEADER_SIZE + CHECKSUM_SIZE; // flag + len + checksum
#[cfg(feature = "std")]
const CHECKSUM_SIZE: usize = mem::size_of::<u64>();
#[cfg(feature = "std")]
const LEN_BUF_SIZE: usize = mem::size_of::<u32>();
#[cfg(feature = "std")]
const HEADER_SIZE: usize = MAGIC_TEXT_LEN + MAGIC_LEN + MAGIC_VERSION_LEN; // magic text + external magic + magic

const DELETE_FLAG: u8 = 0b00000001;
const MASK: u8 = 0b11111110;
const CUSTOM_CORE_MASK: CustomFlagsCore = CustomFlagsCore::from_bits_retain(MASK);

bitflags::bitflags! {
  #[derive(Default, PartialEq, Eq, Copy, Clone, Hash)]
  struct CustomFlagsCore: u8 {
    const BIT1 = 0b00000010;
    const BIT2 = 0b00000100;
    const BIT3 = 0b00001000;
    const BIT4 = 0b00010000;
    const BIT5 = 0b00100000;
    const BIT6 = 0b01000000;
    const BIT7 = 0b10000000;
  }
}

/// A 7-bit custom flag.
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct CustomFlags(CustomFlagsCore);

impl core::fmt::Debug for CustomFlags {
  fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
    f.debug_tuple("CustomFlags")
      .field(&(self.0.bits() & MASK))
      .finish()
  }
}

impl core::fmt::Display for CustomFlags {
  fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
    write!(f, "{}", self.0.bits() & MASK)
  }
}

macro_rules! bits_api {
  ($($num:literal), +$(,)?) => {
    $(
      paste::paste! {
        #[doc = concat!("Set the bit", $num,)]
        ///
        /// ## Example
        ///
        /// ```rust
        /// use aol::CustomFlags;
        ///
        /// let mut flags = CustomFlags::empty();
        ///
        #[doc = concat!("flags.set_bit", $num, "();")]
        /// ```
        #[inline]
        pub fn [< set_bit $num >] (&mut self) {
          self.0.insert(CustomFlagsCore::[< BIT $num >]);
        }

        #[doc = concat!("Set the bit", $num,)]
        ///
        /// ## Example
        ///
        /// ```rust
        /// use aol::CustomFlags;
        ///
        /// let flags = CustomFlags::empty();
        ///
        #[doc = concat!("let flags = flags.with_bit", $num, "();")]
        #[doc = concat!("assert!(flags.bit", $num, "());")]
        /// ```
        #[inline]
        pub const fn [< with_bit $num >] (self) -> Self {
          Self(self.0.union(CustomFlagsCore::[< BIT $num >]))
        }

        #[doc = concat!("Clear the bit", $num)]
        ///
        /// ## Example
        ///
        /// ```rust
        /// use aol::CustomFlags;
        ///
        /// let mut flags = CustomFlags::all();
        ///
        #[doc = concat!("flags.clear_bit", $num, "();")]
        #[doc = concat!("assert_eq!(flags.bit", $num, "(), false);")]
        /// ```
        #[inline]
        pub fn [< clear_bit $num >](&mut self) {
          self.0.remove(CustomFlagsCore::[< BIT $num >]);
        }

        #[doc = concat!("Returns `true` if the bit", $num, " is set.")]
        ///
        /// ## Example
        ///
        /// ```rust
        /// use aol::CustomFlags;
        ///
        /// let flags = CustomFlags::all();
        ///
        #[doc = concat!("assert_eq!(flags.bit", $num, "(), true);")]
        /// ```
        #[inline]
        pub const fn [< bit $num >](&self) -> bool {
          self.0.contains(CustomFlagsCore::[< BIT $num >])
        }
      }
    )*
  };
}

macro_rules! flags_api {
  ($($name:ident: #[$doc:meta]), +$(,)?) => {
    $(
      paste::paste! {
        #[$doc]
        ///
        /// ## Example
        ///
        /// ```rust
        /// use aol::CustomFlags;
        ///
        /// let flags1 = CustomFlags::all();
        /// let flags2 = CustomFlags::empty();
        ///
        #[doc = "let flags = flags1." $name "(flags2);"]
        /// ```
        #[inline]
        pub const fn $name(&self, other: Self) -> Self {
          Self(CustomFlagsCore::from_bits_retain((self.0.$name(other.0).bits() & MASK)))
        }
      }
    )*
  };
}

macro_rules! impl_bitwise_ops {
  ($($trait:ident, $method:ident, $assign_trait:ident, $assign_method:ident),+ $(,)?) => {
    $(
      impl core::ops::$trait for CustomFlags {
        type Output = Self;

        #[inline]
        fn $method(self, rhs: Self) -> Self::Output {
          #[allow(clippy::suspicious_arithmetic_impl)]
          Self(self.0.$method(rhs.0) & CUSTOM_CORE_MASK)
        }
      }

      impl core::ops::$assign_trait for CustomFlags {
        #[inline]
        #[allow(clippy::suspicious_op_assign_impl)]
        fn $assign_method(&mut self, rhs: Self) {
          self.0.$assign_method(rhs.0);
          self.0 &= CUSTOM_CORE_MASK;
        }
      }
    )*
  };
}

impl core::ops::Not for CustomFlags {
  type Output = Self;

  #[inline]
  fn not(self) -> Self::Output {
    Self(self.0.not() & CUSTOM_CORE_MASK)
  }
}

impl_bitwise_ops! {
  BitAnd, bitand, BitAndAssign, bitand_assign,
  BitOr, bitor, BitOrAssign, bitor_assign,
  BitXor, bitxor, BitXorAssign, bitxor_assign,
}

impl CustomFlags {
  /// Get a flags value with all known bits set.
  ///
  /// ## Example
  ///
  /// ```rust
  /// use aol::CustomFlags;
  ///
  /// let flags = CustomFlags::all();
  /// ```
  #[inline]
  pub const fn all() -> Self {
    Self(CustomFlagsCore::from_bits_retain(
      CustomFlagsCore::all().bits() & MASK,
    ))
  }

  /// Get a flags value with all known bits unset.
  ///
  /// ## Example
  ///
  /// ```rust
  /// use aol::CustomFlags;
  ///
  /// let flags = CustomFlags::empty();
  /// ```
  #[inline]
  pub const fn empty() -> Self {
    Self(CustomFlagsCore::empty())
  }

  /// Whether all set bits in a source flags value are also set in a target flags value.
  ///
  /// ## Example
  ///
  /// ```rust
  /// use aol::CustomFlags;
  ///
  /// let flags = CustomFlags::empty();
  ///
  /// assert_eq!(flags.contains(CustomFlags::empty()), true);
  /// ```
  #[inline]
  pub const fn contains(&self, other: Self) -> bool {
    self.0.contains(other.0)
  }

  /// The bitwise exclusive-or (`^`) of the bits in two flags values.
  ///
  /// ## Example
  ///
  /// ```rust
  /// use aol::CustomFlags;
  ///
  /// let mut flags = CustomFlags::all();
  /// flags.toggle(CustomFlags::all());
  ///
  /// assert_eq!(flags, CustomFlags::empty());
  /// ```
  #[inline]
  pub fn toggle(&mut self, other: Self) {
    self.0.toggle(other.0);
    self.0 &= CUSTOM_CORE_MASK;
  }

  /// The bitwise negation (`!`) of the bits in a flags value, truncating the result.
  ///
  /// ## Example
  ///
  /// ```rust
  /// use aol::CustomFlags;
  ///
  /// let flags = CustomFlags::all();
  ///
  /// assert_eq!(flags.complement(), CustomFlags::empty());
  /// ```
  #[inline]
  pub const fn complement(&self) -> Self {
    Self(CustomFlagsCore::from_bits_retain(
      self.0.complement().bits() & MASK,
    ))
  }

  /// Whether all bits in this flags value are unset.
  ///
  /// ## Example
  ///
  /// ```rust
  /// use aol::CustomFlags;
  ///
  /// let flags = CustomFlags::empty();
  ///
  /// assert_eq!(flags.is_empty(), true);
  /// ```
  #[inline]
  pub const fn is_empty(&self) -> bool {
    self.0.is_empty()
  }

  /// Whether any bits in this flags value are set.
  ///
  /// ## Example
  ///
  /// ```rust
  /// use aol::CustomFlags;
  ///
  /// let flags = CustomFlags::all();
  ///
  /// assert_eq!(flags.is_all(), true);
  /// ```
  #[inline]
  pub const fn is_all(&self) -> bool {
    self.0.is_all()
  }

  flags_api! {
    union: #[doc = "The bitwise intersection (`|`) of the bits in two flags values."],
    intersection: #[doc = "The bitwise intersection (`&`) of the bits in two flags values."],
    difference: #[doc = concat!("The intersection of a source flags value with the complement of a target flags value (`&!`).", "\n\n",
    "This method is not equivalent to `self & !other` when `other` has unknown bits set. difference won't truncate other, but the `!` operator will.")],
    symmetric_difference: #[doc = "The bitwise exclusive-or (`^`) of the bits in two flags values."],
  }

  bits_api!(1, 2, 3, 4, 5, 6, 7);

  #[inline]
  const fn bits(&self) -> u8 {
    self.0.bits()
  }
}

/// Flags for the snapshot entry.
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(transparent))]
#[repr(transparent)]
pub struct EntryFlags {
  value: u8,
}

impl core::fmt::Debug for EntryFlags {
  fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
    f.debug_struct("EntryFlags")
      .field("custom_flag", &self.custom_flag())
      .field(
        "op",
        if self.is_deletion() {
          &"deletion"
        } else {
          &"creation"
        },
      )
      .finish()
  }
}

impl EntryFlags {
  /// Creates a new [`EntryFlags`] with the creation flag set
  ///
  /// ## Example
  ///
  /// ```rust
  /// use aol::EntryFlags;
  ///
  /// let entry = EntryFlags::creation();
  ///
  /// assert_eq!(entry.is_creation(), true);
  /// ```
  #[inline]
  pub const fn creation() -> Self {
    Self::creation_with_custom_flag(CustomFlags::empty())
  }

  /// Creates a new [`EntryFlags`] with the deletion flag set
  ///
  /// ## Example
  ///
  /// ```rust
  /// use aol::EntryFlags;
  ///
  /// let entry = EntryFlags::deletion();
  ///
  /// assert_eq!(entry.is_deletion(), true);
  /// ```
  #[inline]
  pub const fn deletion() -> Self {
    Self::deletion_with_custom_flag(CustomFlags::empty())
  }

  /// Creates a new [`EntryFlags`] with the deletion flag set
  ///
  /// ## Example
  ///
  /// ```rust
  /// use aol::{EntryFlags, CustomFlags};
  ///
  /// let entry = EntryFlags::deletion_with_custom_flag(CustomFlags::empty());
  ///
  /// assert_eq!(entry.is_deletion(), true);
  /// ```
  #[inline]
  pub const fn deletion_with_custom_flag(flag: CustomFlags) -> Self {
    let mut this = Self::new(flag);
    this.value |= DELETE_FLAG;
    this
  }

  /// Creates a new [`EntryFlags`] with the creation flag set
  ///
  /// ## Example
  ///
  /// ```rust
  /// use aol::{EntryFlags, CustomFlags};
  ///
  /// let entry = EntryFlags::creation_with_custom_flag(CustomFlags::empty());
  ///
  /// assert_eq!(entry.is_creation(), true);
  /// ```
  #[inline]
  pub const fn creation_with_custom_flag(flag: CustomFlags) -> Self {
    let mut this = Self::new(flag);
    this.value &= !DELETE_FLAG;
    this
  }

  /// Returns the custom flag
  ///
  /// ## Example
  ///
  /// ```rust
  /// use aol::{EntryFlags, CustomFlags};
  ///
  /// let entry = EntryFlags::creation_with_custom_flag(CustomFlags::empty());
  ///
  /// assert_eq!(entry.custom_flag(), CustomFlags::empty());
  /// ```
  #[inline]
  pub const fn custom_flag(&self) -> CustomFlags {
    CustomFlags(CustomFlagsCore::from_bits_retain(self.value & MASK))
  }

  /// Set the custom flag
  ///
  /// ## Example
  ///
  /// ```rust
  /// use aol::{EntryFlags, CustomFlags};
  ///
  /// let mut entry = EntryFlags::creation_with_custom_flag(CustomFlags::empty());
  ///
  /// entry.set_custom_flag(CustomFlags::all());
  ///
  /// assert_eq!(entry.custom_flag(), CustomFlags::all());
  /// ```
  #[inline]
  pub fn set_custom_flag(&mut self, flag: CustomFlags) {
    // Directly set the custom flags without shifting.
    self.value = (flag.bits() & MASK) | (self.value & DELETE_FLAG);
  }

  /// Returns `true` if the entry is a deletion.
  #[inline]
  pub const fn is_deletion(&self) -> bool {
    (self.value & DELETE_FLAG) != 0
  }

  /// Returns `true` if the entry is a creation.
  #[inline]
  pub const fn is_creation(&self) -> bool {
    (self.value & DELETE_FLAG) == 0
  }

  /// Get the underlying bits value.
  ///
  /// The returned value is exactly the bits set in this flags value.
  #[inline]
  pub const fn bits(&self) -> u8 {
    self.value
  }

  // Creates a new EntryFlags with initial value (excluding the first bit)
  #[inline]
  const fn new(flag: CustomFlags) -> Self {
    Self {
      value: flag.bits() & MASK,
    }
  }
}