summaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2022-09-20 13:05:55 +0200
committerLaurenz <laurmaedje@gmail.com>2022-09-20 16:37:15 +0200
commit757a701c1aa2a6fb80033c7e75666661818da6f9 (patch)
tree0415fec94d3856f4ebc97a1744cf2ba75fe8e7aa /src/util
parente29f55bb294cc298daad97accf6d8a76976b409c (diff)
A New World
Diffstat (limited to 'src/util')
-rw-r--r--src/util/buffer.rs59
-rw-r--r--src/util/mod.rs2
2 files changed, 61 insertions, 0 deletions
diff --git a/src/util/buffer.rs b/src/util/buffer.rs
new file mode 100644
index 00000000..daee86f9
--- /dev/null
+++ b/src/util/buffer.rs
@@ -0,0 +1,59 @@
+use std::fmt::{self, Debug, Formatter};
+use std::ops::Deref;
+use std::sync::Arc;
+
+use super::Prehashed;
+
+/// A shared buffer that is cheap to clone and hash.
+#[derive(Clone, Hash, Eq, PartialEq)]
+pub struct Buffer(Prehashed<Arc<Vec<u8>>>);
+
+impl Buffer {
+ /// Return a view into the buffer.
+ pub fn as_slice(&self) -> &[u8] {
+ self
+ }
+
+ /// Return a copy of the buffer as a vector.
+ pub fn to_vec(&self) -> Vec<u8> {
+ self.0.to_vec()
+ }
+}
+
+impl From<&[u8]> for Buffer {
+ fn from(slice: &[u8]) -> Self {
+ Self(Prehashed::new(Arc::new(slice.to_vec())))
+ }
+}
+
+impl From<Vec<u8>> for Buffer {
+ fn from(vec: Vec<u8>) -> Self {
+ Self(Prehashed::new(Arc::new(vec)))
+ }
+}
+
+impl From<Arc<Vec<u8>>> for Buffer {
+ fn from(arc: Arc<Vec<u8>>) -> Self {
+ Self(Prehashed::new(arc))
+ }
+}
+
+impl Deref for Buffer {
+ type Target = [u8];
+
+ fn deref(&self) -> &Self::Target {
+ &self.0
+ }
+}
+
+impl AsRef<[u8]> for Buffer {
+ fn as_ref(&self) -> &[u8] {
+ self
+ }
+}
+
+impl Debug for Buffer {
+ fn fmt(&self, f: &mut Formatter) -> fmt::Result {
+ f.pad("Buffer(..)")
+ }
+}
diff --git a/src/util/mod.rs b/src/util/mod.rs
index e0ba312f..4a48b1d3 100644
--- a/src/util/mod.rs
+++ b/src/util/mod.rs
@@ -2,8 +2,10 @@
#[macro_use]
mod eco;
+mod buffer;
mod hash;
+pub use buffer::Buffer;
pub use eco::EcoString;
pub use hash::Prehashed;