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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
use crate::options::HEADER_SIZE;
use super::*;
/// The abstraction for the common methods of log.
pub trait Log: sealed::Sealed {
/// The identifier type (file ID) for the log.
type Id;
/// Returns the identifier of the log.
///
/// ## Example
///
/// ```rust
/// use valog::{sync::ValueLog, Builder, Log};
///
/// let log = Builder::new()
/// .with_capacity(100)
/// .alloc::<ValueLog>(1)
/// .unwrap();
///
/// assert_eq!(log.id(), &1);
/// ```
fn id(&self) -> &Self::Id;
/// Calculates the checksum of the given bytes.
///
/// ## Example
///
/// ```rust
/// use valog::{sync::ValueLog, Builder, Log, checksum::{Crc32, BuildChecksumer}};
///
/// let log = Builder::new()
/// .with_capacity(100)
/// .alloc::<ValueLog>(1)
/// .unwrap();
///
/// let bytes = b"Hello, valog!";
/// assert_eq!(log.checksum(bytes), Crc32::new().checksum_one(bytes));
/// ```
fn checksum(&self, bytes: &[u8]) -> u64;
/// Returns the options of the log.
///
/// ## Example
///
/// ```rust
/// use valog::{sync::ValueLog, Builder, Log};
///
/// let log = Builder::new()
/// .with_capacity(100)
/// .alloc::<ValueLog>(1)
/// .unwrap();
///
/// assert_eq!(log.options().capacity(), 100);
/// ```
fn options(&self) -> &Options;
/// Returns the magic version of the log.
///
/// ## Example
///
/// ```rust
/// use valog::{sync::ValueLog, Builder, Log};
///
/// let log = Builder::new()
/// .with_capacity(100)
/// .with_magic_version(1)
/// .alloc::<ValueLog>(1)
/// .unwrap();
///
/// assert_eq!(log.magic_version(), 1);
/// ```
fn magic_version(&self) -> u16 {
self.options().magic_version()
}
/// Returns the version of the log.
///
/// ## Example
///
/// ```rust
/// use valog::{sync::ValueLog, Builder, Log};
///
/// let log = Builder::new()
/// .with_capacity(100)
/// .alloc::<ValueLog>(1)
/// .unwrap();
///
/// assert_eq!(log.version(), 0);
/// ```
#[inline]
fn version(&self) -> u16 {
self.allocator().magic_version()
}
/// Returns the discarded bytes of the log.
///
/// ## Example
///
/// ```rust
/// use valog::{sync::ValueLog, Builder, Log, LogWriter};
///
///
/// let log = Builder::new()
/// .with_capacity(100)
/// .alloc::<ValueLog>(1)
/// .unwrap();
///
/// assert_eq!(log.discarded(), 0);
///
/// log.insert_tombstone(b"Hello, valog!").unwrap();
/// assert_eq!(log.discarded(), 13);
/// ```
#[inline]
fn discarded(&self) -> u32 {
self.allocator().discarded()
}
/// Returns the data offset of the log.
///
/// ## Example
///
/// ```rust
/// use valog::{sync::ValueLog, Builder, Log};
///
/// let log = Builder::new()
/// .with_capacity(100)
/// .alloc::<ValueLog>(1)
/// .unwrap();
///
/// assert_eq!(log.data_offset(), 9); // header size is 8, so data start at 9.
///
/// let log = Builder::new()
/// .with_capacity(100)
/// .with_reserved(8)
/// .alloc::<ValueLog>(1)
/// .unwrap();
///
/// assert_eq!(log.data_offset(), 17); // header size is 8, reserved is 8, so data start at 17.
/// ```
fn data_offset(&self) -> usize {
Allocator::data_offset(self.allocator())
}
/// Returns the path of the log.
///
/// If the log is in memory, this method will return `None`.
///
/// ## Example
///
/// ```rust
/// use valog::{sync::ValueLog, Builder, Log};
///
/// let log = Builder::new()
/// .with_capacity(100)
/// .alloc::<ValueLog>(1)
/// .unwrap();
///
/// assert_eq!(log.path(), None);
///
/// let path = tempfile::NamedTempFile::new().unwrap().into_temp_path();
/// # std::fs::remove_file(&path);
/// let log = unsafe {
/// Builder::new()
/// .with_capacity(100)
/// .with_create(true)
/// .with_write(true)
/// .with_read(true)
/// .map_mut::<ValueLog, _>(&path, 0)
/// .unwrap()
/// };
///
/// assert_eq!(log.path().map(|p| p.as_path()), Some(path.as_ref()));
/// ```
#[cfg(all(feature = "memmap", not(target_family = "wasm")))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "memmap", not(target_family = "wasm")))))]
#[inline]
fn path(&self) -> Option<&<Self::Allocator as Allocator>::Path> {
self.allocator().path()
}
/// Returns `true` if the log is in memory.
///
/// ## Example
///
/// ```rust
/// use valog::{sync::ValueLog, Builder, Log};
///
/// let log = Builder::new()
/// .with_capacity(100)
/// .alloc::<ValueLog>(1)
/// .unwrap();
///
/// assert!(log.in_memory());
///
/// # #[cfg(all(feature = "memmap", not(target_family = "wasm")))]
/// # {
/// let path = tempfile::NamedTempFile::new().unwrap().into_temp_path();
/// # std::fs::remove_file(&path);
/// let log = unsafe {
/// Builder::new()
/// .with_capacity(100)
/// .with_create(true)
/// .with_write(true)
/// .with_read(true)
/// .map_mut::<ValueLog, _>(&path, 0)
/// .unwrap()
/// };
/// assert!(!log.in_memory());
/// # }
///
/// ```
#[inline]
fn in_memory(&self) -> bool {
self.allocator().is_inmemory()
}
/// Returns `true` if the log is on disk.
///
/// ## Example
///
/// ```rust
/// use valog::{sync::ValueLog, Builder, Log};
///
/// let log = Builder::new()
/// .with_capacity(100)
/// .alloc::<ValueLog>(1)
/// .unwrap();
///
/// assert!(!log.on_disk());
///
/// # #[cfg(all(feature = "memmap", not(target_family = "wasm")))]
/// # {
/// let path = tempfile::NamedTempFile::new().unwrap().into_temp_path();
/// # std::fs::remove_file(&path);
/// let log = unsafe {
/// Builder::new()
/// .with_capacity(100)
/// .with_create(true)
/// .with_write(true)
/// .with_read(true)
/// .map_mut::<ValueLog, _>(&path, 0)
/// .unwrap()
/// };
/// assert!(log.on_disk());
/// # }
/// ```
#[inline]
fn on_disk(&self) -> bool {
self.allocator().is_ondisk()
}
/// Returns `true` if the log is using a memory map backend.
///
/// ## Example
///
/// ```rust
/// use valog::{sync::ValueLog, Builder, Log};
///
/// let log = Builder::new()
/// .with_capacity(100)
/// .alloc::<ValueLog>(1)
/// .unwrap();
///
/// assert!(!log.is_map());
///
/// let log = Builder::new()
/// .with_capacity(100)
/// .map_anon::<ValueLog>(0)
/// .unwrap();
///
/// assert!(log.is_map());
///
/// # #[cfg(all(feature = "memmap", not(target_family = "wasm")))]
/// # {
/// let path = tempfile::NamedTempFile::new().unwrap().into_temp_path();
/// # std::fs::remove_file(&path);
/// let log = unsafe {
/// Builder::new()
/// .with_capacity(100)
/// .with_create(true)
/// .with_write(true)
/// .with_read(true)
/// .map_mut::<ValueLog, _>(&path, 0)
/// .unwrap()
/// };
/// assert!(log.is_map());
/// # }
/// ```
#[cfg(all(feature = "memmap", not(target_family = "wasm")))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "memmap", not(target_family = "wasm")))))]
#[inline]
fn is_map(&self) -> bool {
self.allocator().is_map()
}
/// Returns the reserved space in the WAL.
///
/// ## Safety
/// - The writer must ensure that the returned slice is not modified.
/// - This method is not thread-safe, so be careful when using it.
///
/// ## Example
///
/// ```rust
/// use valog::{sync::ValueLog, Builder, Log};
///
/// let log = Builder::new()
/// .with_capacity(100)
/// .alloc::<ValueLog>(1)
/// .unwrap();
///
/// let reserved = unsafe { log.reserved_slice() };
/// assert!(reserved.is_empty());
///
/// let log = Builder::new()
/// .with_capacity(100)
/// .with_reserved(8)
/// .alloc::<ValueLog>(1)
/// .unwrap();
///
/// let reserved = unsafe { log.reserved_slice() };
/// assert_eq!(reserved.len(), 8);
/// ```
#[inline]
unsafe fn reserved_slice(&self) -> &[u8] {
let reserved = self.options().reserved();
if reserved == 0 {
return &[];
}
let allocator = self.allocator();
let reserved_slice = allocator.reserved_slice();
&reserved_slice[HEADER_SIZE..]
}
/// Locks the underlying file for exclusive access, only works on mmap with a file backend.
///
/// ## Example
///
/// ```rust
/// use valog::{sync::ValueLog, Builder, Log};
/// # let path = tempfile::NamedTempFile::new().unwrap().into_temp_path();
/// # std::fs::remove_file(&path);
/// /// Create a new file without automatic syncing.
/// let log = unsafe {
/// Builder::new()
/// .with_sync(false)
/// .with_create(true)
/// .with_read(true)
/// .with_write(true)
/// .with_capacity(100)
/// .map_mut::<ValueLog, _>(&path, 0).unwrap()
/// };
///
/// log.lock_exclusive().unwrap();
/// ```
#[cfg(all(feature = "memmap", not(target_family = "wasm")))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "memmap", not(target_family = "wasm")))))]
#[inline]
fn lock_exclusive(&self) -> std::io::Result<()> {
self.allocator().lock_exclusive()
}
/// Locks the underlying file for shared access, only works on mmap with a file backend.
///
/// ## Example
///
/// ```rust
/// use valog::{sync::ValueLog, Builder, Log};
/// # let path = tempfile::NamedTempFile::new().unwrap().into_temp_path();
/// # std::fs::remove_file(&path);
/// /// Create a new file without automatic syncing.
/// let log = unsafe {
/// Builder::new()
/// .with_sync(false)
/// .with_create(true)
/// .with_read(true)
/// .with_write(true)
/// .with_capacity(100)
/// .map_mut::<ValueLog, _>(&path, 0).unwrap()
/// };
///
/// log.lock_shared().unwrap();
/// ```
#[cfg(all(feature = "memmap", not(target_family = "wasm")))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "memmap", not(target_family = "wasm")))))]
#[inline]
fn lock_shared(&self) -> std::io::Result<()> {
self.allocator().lock_shared()
}
/// Unlocks the underlying file, only works on mmap with a file backend.
///
/// ## Example
///
/// ```rust
/// use valog::{sync::ValueLog, Builder, Log};
/// # let path = tempfile::NamedTempFile::new().unwrap().into_temp_path();
/// # std::fs::remove_file(&path);
/// /// Create a new file without automatic syncing.
/// let log = unsafe {
/// Builder::new()
/// .with_sync(false)
/// .with_create(true)
/// .with_read(true)
/// .with_write(true)
/// .with_capacity(100)
/// .map_mut::<ValueLog, _>(&path, 0).unwrap()
/// };
///
/// log.lock_exclusive().unwrap();
///
/// log.unlock().unwrap();
/// ```
#[cfg(all(feature = "memmap", not(target_family = "wasm")))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "memmap", not(target_family = "wasm")))))]
#[inline]
fn unlock(&self) -> std::io::Result<()> {
self.allocator().unlock()
}
/// `mlock(ptr, len)`—Lock memory into RAM.
///
/// ## Safety
///
/// This function operates on raw pointers, but it should only be used on
/// memory which the caller owns. Technically, locking memory shouldn't violate
/// any invariants, but since unlocking it can violate invariants, this
/// function is also unsafe for symmetry.
///
/// Some implementations implicitly round the memory region out to the nearest
/// page boundaries, so this function may lock more memory than explicitly
/// requested if the memory isn't page-aligned. Other implementations fail if
/// the memory isn't page-aligned.
///
/// # References
/// - [POSIX]
/// - [Linux]
/// - [Apple]
/// - [FreeBSD]
/// - [NetBSD]
/// - [OpenBSD]
/// - [DragonFly BSD]
/// - [illumos]
/// - [glibc]
///
/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/mlock.html
/// [Linux]: https://man7.org/linux/man-pages/man2/mlock.2.html
/// [Apple]: https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/mlock.2.html
/// [FreeBSD]: https://man.freebsd.org/cgi/man.cgi?query=mlock&sektion=2
/// [NetBSD]: https://man.netbsd.org/mlock.2
/// [OpenBSD]: https://man.openbsd.org/mlock.2
/// [DragonFly BSD]: https://man.dragonflybsd.org/?command=mlock§ion=2
/// [illumos]: https://illumos.org/man/3C/mlock
/// [glibc]: https://www.gnu.org/software/libc/manual/html_node/Page-Lock-Functions.html#index-mlock
#[cfg(all(feature = "memmap", not(target_family = "wasm"),))]
#[cfg_attr(
docsrs,
doc(cfg(all(feature = "memmap", not(target_family = "wasm"),)))
)]
unsafe fn mlock(&self, offset: usize, len: usize) -> std::io::Result<()> {
self.allocator().mlock(offset, len)
}
/// `munlock(ptr, len)`—Unlock memory.
///
/// ## Safety
///
/// This function operates on raw pointers, but it should only be used on
/// memory which the caller owns, to avoid compromising the `mlock` invariants
/// of other unrelated code in the process.
///
/// Some implementations implicitly round the memory region out to the nearest
/// page boundaries, so this function may unlock more memory than explicitly
/// requested if the memory isn't page-aligned.
///
/// # References
/// - [POSIX]
/// - [Linux]
/// - [Apple]
/// - [FreeBSD]
/// - [NetBSD]
/// - [OpenBSD]
/// - [DragonFly BSD]
/// - [illumos]
/// - [glibc]
///
/// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/munlock.html
/// [Linux]: https://man7.org/linux/man-pages/man2/munlock.2.html
/// [Apple]: https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/munlock.2.html
/// [FreeBSD]: https://man.freebsd.org/cgi/man.cgi?query=munlock&sektion=2
/// [NetBSD]: https://man.netbsd.org/munlock.2
/// [OpenBSD]: https://man.openbsd.org/munlock.2
/// [DragonFly BSD]: https://man.dragonflybsd.org/?command=munlock§ion=2
/// [illumos]: https://illumos.org/man/3C/munlock
/// [glibc]: https://www.gnu.org/software/libc/manual/html_node/Page-Lock-Functions.html#index-munlock
#[cfg(all(feature = "memmap", not(target_family = "wasm"),))]
#[cfg_attr(
docsrs,
doc(cfg(all(feature = "memmap", not(target_family = "wasm"),)))
)]
unsafe fn munlock(&self, offset: usize, len: usize) -> std::io::Result<()> {
self.allocator().munlock(offset, len)
}
}
/// Extension methods for [`Log`].
pub trait LogExt: Log {
/// Flushes the whole log to the given writer.
///
/// ## Example
///
/// ```rust
/// use valog::{sync::ValueLog, Builder, Log, LogExt};
///
/// let log = Builder::new()
/// .with_capacity(100)
/// .alloc::<ValueLog>(1)
/// .unwrap();
///
/// let mut buf = Vec::new();
/// log.flush_to(&mut buf).unwrap();
/// let data_offset = log.data_offset();
/// assert_eq!(buf.len(), data_offset);
/// ```
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
#[inline]
fn flush_to(&self, w: &mut impl std::io::Write) -> std::io::Result<()> {
w.write_all(self.allocator().allocated_memory())
}
}
impl<L: Log> LogExt for L {}
/// The abstraction for the common mutable methods of log.
pub trait MutableLog: Log + Mutable {
/// Returns the mutable reference to the reserved slice.
///
/// ## Safety
/// - The caller must ensure that the there is no others accessing reserved slice for either read or write.
/// - This method is not thread-safe, so be careful when using it.
///
/// ## Example
///
/// ```rust
/// use valog::{sync::ValueLog, Builder, Log, MutableLog};
///
/// let log = Builder::new()
/// .with_capacity(100)
/// .alloc::<ValueLog>(1)
/// .unwrap();
///
/// let reserved = unsafe { log.reserved_slice_mut() };
/// assert!(reserved.is_empty());
///
/// let log = Builder::new()
/// .with_capacity(100)
/// .with_reserved(8)
/// .alloc::<ValueLog>(1)
/// .unwrap();
///
/// let reserved = unsafe { log.reserved_slice_mut() };
/// assert_eq!(reserved.len(), 8);
///
/// reserved.copy_from_slice(b"mysanity");
/// assert_eq!(reserved, b"mysanity");
///
/// let reserved = unsafe { log.reserved_slice() };
/// assert_eq!(reserved, b"mysanity");
/// ```
#[allow(clippy::mut_from_ref)]
#[inline]
unsafe fn reserved_slice_mut(&self) -> &mut [u8] {
let reserved = self.options().reserved();
if reserved == 0 {
return &mut [];
}
let allocator = self.allocator();
let reserved_slice = allocator.reserved_slice_mut();
&mut reserved_slice[HEADER_SIZE..]
}
/// Flushes the memory-mapped file to disk.
///
/// ## Example
///
/// ```rust
/// use valog::{sync::ValueLog, Builder, MutableLog};
/// # let path = tempfile::NamedTempFile::new().unwrap().into_temp_path();
/// # std::fs::remove_file(&path);
/// /// Create a new file without automatic syncing.
/// let arena = unsafe {
/// Builder::new()
/// .with_sync(false)
/// .with_create(true)
/// .with_read(true)
/// .with_write(true)
/// .with_capacity(100)
/// .map_mut::<ValueLog, _>(&path, 0).unwrap() };
///
/// arena.flush().unwrap();
/// ```
#[cfg(all(feature = "memmap", not(target_family = "wasm")))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "memmap", not(target_family = "wasm")))))]
#[inline]
fn flush(&self) -> std::io::Result<()> {
self.allocator().flush()
}
/// Flushes the memory-mapped file to disk asynchronously.
///
/// ## Example
///
/// ```rust
/// use valog::{sync::ValueLog, Builder, MutableLog};
/// # let path = tempfile::NamedTempFile::new().unwrap().into_temp_path();
/// # std::fs::remove_file(&path);
/// /// Create a new file without automatic syncing.
/// let log = unsafe {
/// Builder::new()
/// .with_sync(false)
/// .with_create(true)
/// .with_read(true)
/// .with_write(true)
/// .with_capacity(100)
/// .map_mut::<ValueLog, _>(&path, 0).unwrap()
/// };
///
/// log.flush_async().unwrap();
/// ```
#[cfg(all(feature = "memmap", not(target_family = "wasm")))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "memmap", not(target_family = "wasm")))))]
#[inline]
fn flush_async(&self) -> std::io::Result<()> {
self.allocator().flush_async()
}
/// Flushes outstanding memory map modifications in the range to disk.
///
/// ## Example
///
/// ```rust
/// use valog::{sync::ValueLog, Builder, MutableLog};
/// # let path = tempfile::NamedTempFile::new().unwrap().into_temp_path();
/// # std::fs::remove_file(&path);
///
/// /// Create a new file without automatic syncing.
/// let log = unsafe {
/// Builder::new()
/// .with_sync(false)
/// .with_create(true)
/// .with_read(true)
/// .with_write(true)
/// .with_capacity(100)
/// .map_mut::<ValueLog, _>(&path, 0).unwrap()
/// };
///
/// log.flush_range(0, 50).unwrap();
/// ```
#[cfg(all(feature = "memmap", not(target_family = "wasm")))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "memmap", not(target_family = "wasm")))))]
#[inline]
fn flush_range(&self, offset: usize, len: usize) -> std::io::Result<()> {
self.allocator().flush_header_and_range(offset, len)
}
/// Asynchronously flushes outstanding memory map modifications in the range to disk.
///
/// ## Example
///
/// ```rust
/// use valog::{sync::ValueLog, Builder, MutableLog};
/// # let path = tempfile::NamedTempFile::new().unwrap().into_temp_path();
/// # std::fs::remove_file(&path);
/// /// Create a new file without automatic syncing.
/// let vlog = unsafe {
/// Builder::new()
/// .with_sync(false)
/// .with_create(true)
/// .with_read(true)
/// .with_write(true)
/// .with_capacity(100)
/// .map_mut::<ValueLog, _>(&path, 0).unwrap()
/// };
///
/// vlog.flush_async_range(0, 50).unwrap();
/// ```
#[cfg(all(feature = "memmap", not(target_family = "wasm")))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "memmap", not(target_family = "wasm")))))]
#[inline]
fn flush_async_range(&self, offset: usize, len: usize) -> std::io::Result<()> {
self.allocator().flush_async_header_and_range(offset, len)
}
}
impl<L: Log + Mutable> MutableLog for L {}
pub trait AsLog {
type Log;
type Type;
fn as_log(&self) -> &Self::Log;
}
impl<L> sealed::Sealed for L
where
L: AsLog,
L::Log: Log,
{
type Allocator = <L::Log as sealed::Sealed>::Allocator;
#[inline]
fn allocator(&self) -> &Self::Allocator {
self.as_log().allocator()
}
}
impl<L> sealed::Constructor for L
where
L: AsLog,
L::Log: Log + sealed::Constructor<Id = <L::Log as Log>::Id>,
L: From<L::Log>,
{
type Checksumer = <L::Log as sealed::Constructor>::Checksumer;
type Id = <L::Log as Log>::Id;
fn construct(
fid: Self::Id,
allocator: Self::Allocator,
checksumer: Self::Checksumer,
options: Options,
) -> Self {
<L::Log as sealed::Constructor>::construct(fid, allocator, checksumer, options).into()
}
}
impl<L> Log for L
where
L: AsLog,
L::Log: Log,
{
type Id = <L::Log as Log>::Id;
#[inline]
fn id(&self) -> &Self::Id {
self.as_log().id()
}
#[inline]
fn checksum(&self, bytes: &[u8]) -> u64 {
self.as_log().checksum(bytes)
}
#[inline]
fn options(&self) -> &Options {
self.as_log().options()
}
}