fastbuf/
lib.rs

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
#![feature(slice_index_methods)]
#![feature(min_specialization)]
#![feature(const_copy_from_slice)]
#![feature(const_trait_impl)]
#![cfg_attr(feature = "std", feature(new_zeroed_alloc))]
#![feature(allocator_api)]
#![cfg_attr(test, feature(test))]
#![cfg_attr(not(feature = "std"), no_std)]
#![doc = include_str!("../README.md")]

#[cfg(test)]
extern crate self as fastbuf;
#[cfg(test)]
extern crate test;

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

mod traits;
pub use traits::*;

mod buffer;
pub use buffer::*;

mod chunk;
pub use chunk::*;

pub struct EmptyAlloc;
unsafe impl std::alloc::Allocator for EmptyAlloc {
    fn allocate(
        &self,
        layout: std::alloc::Layout,
    ) -> Result<std::ptr::NonNull<[u8]>, std::alloc::AllocError> {
        unreachable!()
    }

    unsafe fn deallocate(&self, ptr: std::ptr::NonNull<u8>, layout: std::alloc::Layout) {
        unreachable!()
    }
}

pub(crate) mod macros {

    #[macro_export]
    macro_rules! declare_trait {
        ($visibility:vis trait $name:ident<($($generics:tt)*)>: ($($supertrait:path),*) {$($body:tt)*}) => {
            #[cfg(not(feature = "const-trait"))]
            $visibility trait $name<$($generics)*>: $($supertrait + )* {
                $($body)*
            }

            #[cfg(feature = "const-trait")]
            #[const_trait]
            $visibility trait $name<$($generics)*>: $(const $supertrait +)* {
                $($body)*
            }
        };
    }

    #[macro_export]
    macro_rules! declare_impl {
        (($($impl:tt)*), ($($impl_const:tt)*) {$($body:tt)*}) => {
            #[cfg(feature = "const-trait")]
            $($impl_const)* { $($body)* }

            #[cfg(not(feature = "const-trait"))]
            $($impl)* { $($body)* }
        };
    }

    #[cfg(feature = "const-trait")]
    #[macro_export]
    macro_rules! const_min {
        ($a:expr, $b:expr) => {
            konst::min!($a, $b)
        };
    }

    #[cfg(not(feature = "const-trait"))]
    #[macro_export]
    macro_rules! const_min {
        ($a:expr, $b:expr) => {
            core::cmp::min($a, $b)
        };
    }
}