]> git.proxmox.com Git - rustc.git/blobdiff - vendor/measureme/src/serialization.rs
New upstream version 1.41.1+dfsg1
[rustc.git] / vendor / measureme / src / serialization.rs
index f7bb6dd2bbc41fa55abf1b6e98b94bf46e5789ca..fe071196c58798bf51d36b772a7357b67ea2b40f 100644 (file)
@@ -1,5 +1,6 @@
 use std::error::Error;
 use std::path::Path;
+use parking_lot::Mutex;
 
 #[derive(Clone, Copy, Eq, PartialEq, Debug)]
 pub struct Addr(pub u32);
@@ -10,7 +11,7 @@ impl Addr {
     }
 }
 
-pub trait SerializationSink: Sized {
+pub trait SerializationSink: Sized + Send + Sync + 'static {
     fn from_path(path: &Path) -> Result<Self, Box<dyn Error>>;
 
     fn write_atomic<W>(&self, num_bytes: usize, write: W) -> Addr
@@ -18,51 +19,48 @@ pub trait SerializationSink: Sized {
         W: FnOnce(&mut [u8]);
 }
 
-#[cfg(test)]
-pub mod test {
-    use super::*;
-    use std::sync::Mutex;
-
-    pub struct TestSink {
-        data: Mutex<Vec<u8>>,
-    }
+/// A `SerializationSink` that writes to an internal `Vec<u8>` and can be
+/// converted into this raw `Vec<u8>`. This implementation is only meant to be
+/// used for testing and is not very efficient.
+pub struct ByteVecSink {
+    data: Mutex<Vec<u8>>,
+}
 
-    impl TestSink {
-        pub fn new() -> TestSink {
-            TestSink {
-                data: Mutex::new(Vec::new()),
-            }
+impl ByteVecSink {
+    pub fn new() -> ByteVecSink {
+        ByteVecSink {
+            data: Mutex::new(Vec::new()),
         }
+    }
 
-        pub fn into_bytes(self) -> Vec<u8> {
-            self.data.into_inner().unwrap()
-        }
+    pub fn into_bytes(self) -> Vec<u8> {
+        self.data.into_inner()
     }
+}
 
-    impl SerializationSink for TestSink {
-        fn from_path(_path: &Path) -> Result<Self, Box<dyn Error>> {
-            unimplemented!()
-        }
+impl SerializationSink for ByteVecSink {
+    fn from_path(_path: &Path) -> Result<Self, Box<dyn Error>> {
+        unimplemented!()
+    }
 
-        fn write_atomic<W>(&self, num_bytes: usize, write: W) -> Addr
-        where
-            W: FnOnce(&mut [u8]),
-        {
-            let mut data = self.data.lock().unwrap();
+    fn write_atomic<W>(&self, num_bytes: usize, write: W) -> Addr
+    where
+        W: FnOnce(&mut [u8]),
+    {
+        let mut data = self.data.lock();
 
-            let start = data.len();
+        let start = data.len();
 
-            data.resize(start + num_bytes, 0);
+        data.resize(start + num_bytes, 0);
 
-            write(&mut data[start..]);
+        write(&mut data[start..]);
 
-            Addr(start as u32)
-        }
+        Addr(start as u32)
     }
+}
 
-    impl std::fmt::Debug for TestSink {
-        fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
-            write!(f, "TestSink")
-        }
+impl std::fmt::Debug for ByteVecSink {
+    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+        write!(f, "ByteVecSink")
     }
 }