indexedlog/
macros.rs

1/*
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8// Implement traits for typed offset structs.
9macro_rules! impl_offset {
10    ($type:ident, $type_int:expr, $name:expr) => {
11        impl TypedOffsetMethods for $type {
12            #[inline]
13            fn type_int() -> u8 {
14                $type_int
15            }
16
17            #[inline]
18            fn from_offset_unchecked(offset: Offset) -> Self {
19                $type(offset)
20            }
21
22            #[inline]
23            fn to_offset(&self) -> Offset {
24                self.0
25            }
26        }
27
28        impl Deref for $type {
29            type Target = Offset;
30
31            #[inline]
32            fn deref(&self) -> &Offset {
33                &self.0
34            }
35        }
36
37        impl Debug for $type {
38            fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
39                if self.is_null() {
40                    write!(f, "None")
41                } else {
42                    if self.is_dirty() {
43                        write!(f, "{}[{}]", $name, self.dirty_index())
44                    } else {
45                        // `Offset` will print "Disk[{}]".
46                        self.0.fmt(f)
47                    }
48                }
49            }
50        }
51
52        impl From<$type> for Offset {
53            #[inline]
54            fn from(x: $type) -> Offset {
55                x.0
56            }
57        }
58
59        impl From<$type> for u64 {
60            #[inline]
61            fn from(x: $type) -> u64 {
62                (x.0).0
63            }
64        }
65
66        impl From<$type> for usize {
67            #[inline]
68            fn from(x: $type) -> usize {
69                (x.0).0 as usize
70            }
71        }
72    };
73}