/rust/registry/src/index.crates.io-1949cf8c6b5b557f/surrealmx-0.22.0/src/kv.rs
Line | Count | Source |
1 | | // Copyright © SurrealDB Ltd |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | use bytes::Bytes; |
16 | | use std::borrow::Cow; |
17 | | |
18 | | /// An optimised trait for converting values to bytes only when needed |
19 | | pub trait IntoBytes { |
20 | | /// Convert the key to a slice of bytes |
21 | | fn as_slice(&self) -> &[u8]; |
22 | | /// Convert the key to an owned bytes slice |
23 | | fn into_bytes(self) -> Bytes; |
24 | | } |
25 | | |
26 | | impl IntoBytes for &[u8] { |
27 | 818k | fn as_slice(&self) -> &[u8] { |
28 | | // Get the bytes reference |
29 | 818k | self |
30 | 818k | } |
31 | | |
32 | 0 | fn into_bytes(self) -> Bytes { |
33 | | // Must copy from &[u8] |
34 | 0 | Bytes::copy_from_slice(self) |
35 | 0 | } |
36 | | } |
37 | | |
38 | | impl IntoBytes for Vec<u8> { |
39 | 702k | fn as_slice(&self) -> &[u8] { |
40 | 702k | self.as_slice() |
41 | 702k | } |
42 | | |
43 | 1.17M | fn into_bytes(self) -> Bytes { |
44 | | // Zero-copy from Vec<u8> |
45 | 1.17M | Bytes::from(self) |
46 | 1.17M | } |
47 | | } |
48 | | |
49 | | impl IntoBytes for &Vec<u8> { |
50 | 139k | fn as_slice(&self) -> &[u8] { |
51 | | // Get the bytes reference |
52 | 139k | &self[..] |
53 | 139k | } |
54 | | |
55 | 0 | fn into_bytes(self) -> Bytes { |
56 | | // Must copy from &Vec<u8> |
57 | 0 | Bytes::copy_from_slice(&self[..]) |
58 | 0 | } |
59 | | } |
60 | | |
61 | | impl IntoBytes for Bytes { |
62 | 0 | fn as_slice(&self) -> &[u8] { |
63 | | // Get the bytes reference |
64 | 0 | self.as_ref() |
65 | 0 | } |
66 | | |
67 | 0 | fn into_bytes(self) -> Bytes { |
68 | | // Zero-copy from self |
69 | 0 | self |
70 | 0 | } |
71 | | } |
72 | | |
73 | | impl IntoBytes for &Bytes { |
74 | 0 | fn as_slice(&self) -> &[u8] { |
75 | | // Get the bytes reference |
76 | 0 | self.as_ref() |
77 | 0 | } |
78 | | |
79 | 0 | fn into_bytes(self) -> Bytes { |
80 | | // Zero-copy from self |
81 | 0 | self.clone() |
82 | 0 | } |
83 | | } |
84 | | |
85 | | impl IntoBytes for &str { |
86 | 0 | fn as_slice(&self) -> &[u8] { |
87 | | // Get the string bytes reference |
88 | 0 | self.as_bytes() |
89 | 0 | } |
90 | | |
91 | 0 | fn into_bytes(self) -> Bytes { |
92 | | // Must copy from &str |
93 | 0 | Bytes::copy_from_slice(self.as_bytes()) |
94 | 0 | } |
95 | | } |
96 | | |
97 | | impl IntoBytes for String { |
98 | 0 | fn as_slice(&self) -> &[u8] { |
99 | | // Get the string bytes reference |
100 | 0 | self.as_bytes() |
101 | 0 | } |
102 | | |
103 | 0 | fn into_bytes(self) -> Bytes { |
104 | | // Zero-copy from String |
105 | 0 | Bytes::from(self.into_bytes()) |
106 | 0 | } |
107 | | } |
108 | | |
109 | | impl IntoBytes for &String { |
110 | 0 | fn as_slice(&self) -> &[u8] { |
111 | | // Get the string bytes reference |
112 | 0 | self.as_bytes() |
113 | 0 | } |
114 | | |
115 | 0 | fn into_bytes(self) -> Bytes { |
116 | | // Must copy from &String |
117 | 0 | Bytes::copy_from_slice(self.as_bytes()) |
118 | 0 | } |
119 | | } |
120 | | |
121 | | impl IntoBytes for Box<[u8]> { |
122 | 0 | fn as_slice(&self) -> &[u8] { |
123 | | // Get the bytes reference |
124 | 0 | self.as_ref() |
125 | 0 | } |
126 | | |
127 | 0 | fn into_bytes(self) -> Bytes { |
128 | | // Zero-copy from Box<[u8]> |
129 | 0 | Bytes::from(self) |
130 | 0 | } |
131 | | } |
132 | | |
133 | | impl<'a> IntoBytes for Cow<'a, [u8]> { |
134 | 0 | fn as_slice(&self) -> &[u8] { |
135 | | // Get the bytes reference |
136 | 0 | self.as_ref() |
137 | 0 | } |
138 | | |
139 | 0 | fn into_bytes(self) -> Bytes { |
140 | | // Match the Cow variant |
141 | 0 | match self { |
142 | 0 | Cow::Borrowed(s) => Bytes::copy_from_slice(s), |
143 | 0 | Cow::Owned(v) => Bytes::from(v), |
144 | | } |
145 | 0 | } |
146 | | } |