diff options
author | Azul <azul@riseup.net> | 2020-12-09 20:26:52 +0100 |
---|---|---|
committer | Azul <azul@riseup.net> | 2020-12-10 13:57:19 +0100 |
commit | 227db80b9d40526387f1e4a64f90464618fb885b (patch) | |
tree | bcf1ab1ed4d61fffea8d382872262650c5f49d86 | |
parent | 80b59ecf7b33dcd2392eee8b4e2def4101410749 (diff) |
buffered-reader: Require Cookies to be Send and Sync.
- This way the entire `BufferedReader<C>` will be `Send` and `Sync`.
- Modify all other crates accordingly.
- See #615.
35 files changed, 219 insertions, 149 deletions
diff --git a/autocrypt/src/lib.rs b/autocrypt/src/lib.rs index 3b52cb69..7a51b5f3 100644 --- a/autocrypt/src/lib.rs +++ b/autocrypt/src/lib.rs @@ -538,7 +538,7 @@ impl AutocryptSetupMessage { /// Parses the autocrypt setup message in `r`. /// /// `passcode` is the passcode used to protect the message. - pub fn from_reader<'a, R: io::Read + 'a>(r: R) + pub fn from_reader<'a, R: io::Read + Send + Sync + 'a>(r: R) -> Result<AutocryptSetupMessageParser<'a>> { // The outer message uses ASCII-armor. It includes a password // hint. Hence, we need to parse it aggressively. diff --git a/buffered-reader/src/adapter.rs b/buffered-reader/src/adapter.rs index 65c7a8aa..3734d9bc 100644 --- a/buffered-reader/src/adapter.rs +++ b/buffered-reader/src/adapter.rs @@ -10,7 +10,7 @@ use super::*; /// `Adapter` also changes cookie types, but does no buffering of its /// own. #[derive(Debug)] -pub struct Adapter<T: BufferedReader<B>, B: fmt::Debug, C: fmt::Debug> { +pub struct Adapter<T: BufferedReader<B>, B: fmt::Debug + Send + Sync, C: fmt::Debug + Sync + Send> { reader: T, _ghostly_cookie: std::marker::PhantomData<B>, cookie: C, @@ -21,13 +21,13 @@ assert_send_and_sync!(Adapter<T, B, C> B: fmt::Debug, C: fmt::Debug); -impl<T: BufferedReader<B>, B: fmt::Debug, C: fmt::Debug> fmt::Display for Adapter<T, B, C> { +impl<T: BufferedReader<B>, B: fmt::Debug + Send + Sync, C: fmt::Debug + Sync + Send> fmt::Display for Adapter<T, B, C> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.debug_struct("Adapter").finish() } } -impl<T: BufferedReader<B>, B: fmt::Debug> Adapter<T, B, ()> { +impl<T: BufferedReader<B>, B: fmt::Debug + Sync + Send> Adapter<T, B, ()> { /// Instantiates a new adapter. /// /// `reader` is the source to wrap. @@ -36,7 +36,7 @@ impl<T: BufferedReader<B>, B: fmt::Debug> Adapter<T, B, ()> { } } -impl<T: BufferedReader<B>, B: fmt::Debug, C: fmt::Debug> Adapter<T, B, C> { +impl<T: BufferedReader<B>, B: fmt::Debug + Send + Sync, C: fmt::Debug + Sync + Send> Adapter<T, B, C> { /// Like `new()`, but sets a cookie. /// /// The cookie can be retrieved using the `cookie_ref` and @@ -51,13 +51,13 @@ impl<T: BufferedReader<B>, B: fmt::Debug, C: fmt::Debug> Adapter<T, B, C> { } } -impl<T: BufferedReader<B>, B: fmt::Debug, C: fmt::Debug> io::Read for Adapter<T, B, C> { +impl<T: BufferedReader<B>, B: fmt::Debug + Send + Sync, C: fmt::Debug + Sync + Send> io::Read for Adapter<T, B, C> { fn read(&mut self, buf: &mut [u8]) -> Result<usize, io::Error> { self.reader.read(buf) } } -impl<T: BufferedReader<B>, B: fmt::Debug, C: fmt::Debug> BufferedReader<C> for Adapter<T, B, C> { +impl<T: BufferedReader<B>, B: fmt::Debug + Send + Sync, C: fmt::Debug + Sync + Send> BufferedReader<C> for Adapter<T, B, C> { fn buffer(&self) -> &[u8] { self.reader.buffer() } diff --git a/buffered-reader/src/decompress_bzip2.rs b/buffered-reader/src/decompress_bzip2.rs index 8902c2af..febe37fc 100644 --- a/buffered-reader/src/decompress_bzip2.rs +++ b/buffered-reader/src/decompress_bzip2.rs @@ -9,7 +9,7 @@ use super::*; /// Decompresses the underlying `BufferedReader` using the bzip2 /// algorithm. #[derive(Debug)] -pub struct Bzip<R: BufferedReader<C>, C: fmt::Debug> { +pub struct Bzip<R: BufferedReader<C>, C: fmt::Debug + Sync + Send> { reader: Generic<BzDecoder<R>, C>, } @@ -26,7 +26,7 @@ impl <R: BufferedReader<()>> Bzip<R, ()> { } } -impl <R: BufferedReader<C>, C: fmt::Debug> Bzip<R, C> { +impl <R: BufferedReader<C>, C: fmt::Debug + Sync + Send> Bzip<R, C> { /// Like `new()`, but uses a cookie. /// /// The cookie can be retrieved using the `cookie_ref` and @@ -39,19 +39,19 @@ impl <R: BufferedReader<C>, C: fmt::Debug> Bzip<R, C> { } } -impl<R: BufferedReader<C>, C: fmt::Debug> io::Read for Bzip<R, C> { +impl<R: BufferedReader<C>, C: fmt::Debug + Sync + Send> io::Read for Bzip<R, C> { fn read(&mut self, buf: &mut [u8]) -> Result<usize, io::Error> { self.reader.read(buf) } } -impl<R: BufferedReader<C>, C: fmt::Debug> fmt::Display for Bzip<R, C> { +impl<R: BufferedReader<C>, C: fmt::Debug + Sync + Send> fmt::Display for Bzip<R, C> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.debug_struct("Bzip").finish() } } -impl<R: BufferedReader<C>, C: fmt::Debug> BufferedReader<C> for Bzip<R, C> { +impl<R: BufferedReader<C>, C: fmt::Debug + Send + Sync> BufferedReader<C> for Bzip<R, C> { fn buffer(&self) -> &[u8] { return self.reader.buffer(); } diff --git a/buffered-reader/src/decompress_deflate.rs b/buffered-reader/src/decompress_deflate.rs index 43084a5f..d1d1a16b 100644 --- a/buffered-reader/src/decompress_deflate.rs +++ b/buffered-reader/src/decompress_deflate.rs @@ -9,7 +9,7 @@ use super::*; /// Decompresses the underlying `BufferedReader` using the deflate /// algorithm. #[derive(Debug)] -pub struct Deflate<R: BufferedReader<C>, C: fmt::Debug> { +pub struct Deflate<R: BufferedReader<C>, C: fmt::Debug + Sync + Send> { reader: Generic<DeflateDecoder<R>, C>, } @@ -26,7 +26,7 @@ impl <R: BufferedReader<()>> Deflate<R, ()> { } } -impl <R: BufferedReader<C>, C: fmt::Debug> Deflate<R, C> { +impl <R: BufferedReader<C>, C: fmt::Debug + Sync + Send> Deflate<R, C> { /// Like `new()`, but uses a cookie. /// /// The cookie can be retrieved using the `cookie_ref` and @@ -39,19 +39,19 @@ impl <R: BufferedReader<C>, C: fmt::Debug> Deflate<R, C> { } } -impl<R: BufferedReader<C>, C: fmt::Debug> io::Read for Deflate<R, C> { +impl<R: BufferedReader<C>, C: fmt::Debug + Sync + Send> io::Read for Deflate<R, C> { fn read(&mut self, buf: &mut [u8]) -> Result<usize, io::Error> { self.reader.read(buf) } } -impl<R: BufferedReader<C>, C: fmt::Debug> fmt::Display for Deflate<R, C> { +impl<R: BufferedReader<C>, C: fmt::Debug + Sync + Send> fmt::Display for Deflate<R, C> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.debug_struct("Deflate").finish() } } -impl<R: BufferedReader<C>, C: fmt::Debug> BufferedReader<C> +impl<R: BufferedReader<C>, C: fmt::Debug + Send + Sync> BufferedReader<C> for Deflate<R, C> { fn buffer(&self) -> &[u8] { return self.reader.buffer(); @@ -127,7 +127,7 @@ impl<R: BufferedReader<C>, C: fmt::Debug> BufferedReader<C> /// Decompresses the underlying `BufferedReader` using the zlib /// algorithm. -pub struct Zlib<R: BufferedReader<C>, C: fmt::Debug> { +pub struct Zlib<R: BufferedReader<C>, C: fmt::Debug + Sync + Send> { reader: Generic<ZlibDecoder<R>, C>, } @@ -144,7 +144,7 @@ impl <R: BufferedReader<()>> Zlib<R, ()> { } } -impl <R: BufferedReader<C>, C: fmt::Debug> Zlib<R, C> { +impl <R: BufferedReader<C>, C: fmt::Debug + Sync + Send> Zlib<R, C> { /// Like `new()`, but uses a cookie. /// /// The cookie can be retrieved using the `cookie_ref` and @@ -157,19 +157,19 @@ impl <R: BufferedReader<C>, C: fmt::Debug> Zlib<R, C> { } } -impl<R: BufferedReader<C>, C: fmt::Debug> io::Read for Zlib<R, C> { +impl<R: BufferedReader<C>, C: fmt::Debug + Sync + Send> io::Read for Zlib<R, C> { fn read(&mut self, buf: &mut [u8]) -> Result<usize, io::Error> { self.reader.read(buf) } } -impl<R: BufferedReader<C>, C: fmt::Debug> fmt::Display for Zlib<R, C> { +impl<R: BufferedReader<C>, C: fmt::Debug + Sync + Send> fmt::Display for Zlib<R, C> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "Zlib") } } -impl<R: BufferedReader<C>, C: fmt::Debug> fmt::Debug for Zlib<R, C> { +impl<R: BufferedReader<C>, C: fmt::Debug + Sync + Send> fmt::Debug for Zlib<R, C> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.debug_struct("Zlib") .field("reader", &self.get_ref().unwrap()) @@ -177,7 +177,7 @@ impl<R: BufferedReader<C>, C: fmt::Debug> fmt::Debug for Zlib<R, C> { } } -impl<R: BufferedReader<C>, C: fmt::Debug> BufferedReader<C> +impl<R: BufferedReader<C>, C: fmt::Debug + Send + Sync> BufferedReader<C> for Zlib<R, C> { fn buffer(&self) -> &[u8] { return self.reader.buffer(); diff --git a/buffered-reader/src/dup.rs b/buffered-reader/src/dup.rs index 50a9aa26..d7d3f7e5 100644 --- a/buffered-reader/src/dup.rs +++ b/buffered-reader/src/dup.rs @@ -11,7 +11,7 @@ use super::*; /// much data as you read. Thus, it should only be used for peeking /// at the underlying `BufferedReader`. #[derive(Debug)] -pub struct Dup<T: BufferedReader<C>, C: fmt::Debug> { +pub struct Dup<T: BufferedReader<C>, C: fmt::Debug + Sync + Send> { reader: T, // The number of bytes that have been consumed. @@ -25,7 +25,7 @@ assert_send_and_sync!(Dup<T, C> where T: BufferedReader<C>, C: fmt::Debug); -impl<T: BufferedReader<C>, C: fmt::Debug> fmt::Display for Dup<T, C> { +impl<T: BufferedReader<C>, C: fmt::Debug + Sync + Send> fmt::Display for Dup<T, C> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.debug_struct("Dup") .field("cursor", &self.cursor) @@ -42,7 +42,7 @@ impl<T: BufferedReader<()>> Dup<T, ()> { } } -impl<T: BufferedReader<C>, C: fmt::Debug> Dup<T, C> { +impl<T: BufferedReader<C>, C: fmt::Debug + Sync + Send> Dup<T, C> { /// Like `new()`, but uses a cookie. /// /// The cookie can be retrieved using the `cookie_ref` and @@ -66,7 +66,7 @@ impl<T: BufferedReader<C>, C: fmt::Debug> Dup<T, C> { } } -impl<T: BufferedReader<C>, C: fmt::Debug> io::Read for Dup<T, C> { +impl<T: BufferedReader<C>, C: fmt::Debug + Sync + Send> io::Read for Dup<T, C> { fn read(&mut self, buf: &mut [u8]) -> Result<usize, io::Error> { let data = self.reader.data(self.cursor + buf.len())?; assert!(data.len() >= self.cursor); @@ -81,7 +81,7 @@ impl<T: BufferedReader<C>, C: fmt::Debug> io::Read for Dup<T, C> { } } -impl<T: BufferedReader<C>, C: fmt::Debug> BufferedReader<C> for Dup<T, C> { +impl<T: BufferedReader<C>, C: fmt::Debug + Send + Sync> BufferedReader<C> for Dup<T, C> { fn buffer(&self) -> &[u8] { let data = self.reader.buffer(); assert!(data.len() >= self.cursor); diff --git a/buffered-reader/src/eof.rs b/buffered-reader/src/eof.rs index c21ecde9..b0312630 100644 --- a/buffered-reader/src/eof.rs +++ b/buffered-reader/src/eof.rs @@ -44,7 +44,7 @@ impl<C> Read for EOF<C> { } } -impl<C: fmt::Debug> BufferedReader<C> for EOF<C> { +impl<C: fmt::Debug + Sync + Send> BufferedReader<C> for EOF<C> { fn buffer(&self) -> &[u8] { return &b""[..]; } diff --git a/buffered-reader/src/file_generic.rs b/buffered-reader/src/file_generic.rs index cc9f1367..2c61e571 100644 --- a/buffered-reader/src/file_generic.rs +++ b/buffered-reader/src/file_generic.rs @@ -10,18 +10,18 @@ use crate::file_error::FileError; /// /// This is a generic implementation that may be replaced by /// platform-specific versions. -pub struct File<C: fmt::Debug>(Generic<fs::File, C>, PathBuf); +pub struct File<C: fmt::Debug + Sync + Send>(Generic<fs::File, C>, PathBuf); assert_send_and_sync!(File<C> where C: fmt::Debug); -impl<C: fmt::Debug> fmt::Display for File<C> { +impl<C: fmt::Debug + Sync + Send> fmt::Display for File<C> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "File {:?}", self.1.display()) } } -impl<C: fmt::Debug> fmt::Debug for File<C> { +impl<C: fmt::Debug + Sync + Send> fmt::Debug for File<C> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.debug_tuple("File") .field(&self.0) @@ -37,7 +37,7 @@ impl File<()> { } } -impl<C: fmt::Debug> File<C> { +impl<C: fmt::Debug + Sync + Send> File<C> { /// Like `open()`, but sets a cookie. pub fn with_cookie<P: AsRef<Path>>(path: P, cookie: C) -> io::Result<Self> { let path = path.as_ref(); @@ -46,14 +46,14 @@ impl<C: fmt::Debug> File<C> { } } -impl<C: fmt::Debug> io::Read for File<C> { +impl<C: fmt::Debug + Sync + Send> io::Read for File<C> { fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { self.0.read(buf) .map_err(|e| FileError::new(&self.1, e)) } } -impl<C: fmt::Debug> BufferedReader<C> for File<C> { +impl<C: fmt::Debug + Sync + Send> BufferedReader<C> for File<C> { fn buffer(&self) -> &[u8] { self.0.buffer() } diff --git a/buffered-reader/src/file_unix.rs b/buffered-reader/src/file_unix.rs index 9502021f..a1298737 100644 --- a/buffered-reader/src/file_unix.rs +++ b/buffered-reader/src/file_unix.rs @@ -25,18 +25,18 @@ const MMAP_THRESHOLD: u64 = 16 * 4096; /// /// This implementation tries to mmap the file, falling back to /// just using a generic reader. -pub struct File<'a, C: fmt::Debug>(Imp<'a, C>, PathBuf); +pub struct File<'a, C: fmt::Debug + Sync + Send>(Imp<'a, C>, PathBuf); assert_send_and_sync!(File<'_, C> where C: fmt::Debug); -impl<'a, C: fmt::Debug> fmt::Display for File<'a, C> { +impl<'a, C: fmt::Debug + Sync + Send> fmt::Display for File<'a, C> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{} {:?}", self.0, self.1.display()) } } -impl<'a, C: fmt::Debug> fmt::Debug for File<'a, C> { +impl<'a, C: fmt::Debug + Sync + Send> fmt::Debug for File<'a, C> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.debug_tuple("File") .field(&self.0) @@ -46,14 +46,14 @@ impl<'a, C: fmt::Debug> fmt::Debug for File<'a, C> { } /// The implementation. -enum Imp<'a, C: fmt::Debug> { +enum Imp<'a, C: fmt::Debug + Sync + Send> { Generic(Generic<fs::File, C>), MMAP { reader: Memory<'a, C>, } } -impl<'a, C: fmt::Debug> Drop for Imp<'a, C> { +impl<'a, C: fmt::Debug + Sync + Send> Drop for Imp<'a, C> { fn drop(&mut self) { match self { Imp::Generic(_) => (), @@ -67,7 +67,7 @@ impl<'a, C: fmt::Debug> Drop for Imp<'a, C> { } } -impl<'a, C: fmt::Debug> fmt::Display for Imp<'a, C> { +impl<'a, C: fmt::Debug + Sync + Send> fmt::Display for Imp<'a, C> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "File(")?; match self { @@ -78,7 +78,7 @@ impl<'a, C: fmt::Debug> fmt::Display for Imp<'a, C> { } } -impl<'a, C: fmt::Debug> fmt::Debug for Imp<'a, C> { +impl<'a, C: fmt::Debug + Sync + Send> fmt::Debug for Imp<'a, C> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { Imp::Generic(ref g) => @@ -102,7 +102,7 @@ impl<'a> File<'a, ()> { } } -impl<'a, C: fmt::Debug> File<'a, C> { +impl<'a, C: fmt::Debug + Sync + Send> File<'a, C> { /// Like `open()`, but sets a cookie. pub fn with_cookie<P: AsRef<Path>>(path: P, cookie: C) -> io::Result<Self> { let path = path.as_ref(); @@ -160,7 +160,7 @@ impl<'a, C: fmt::Debug> File<'a, C> { } } -impl<'a, C: fmt::Debug> io::Read for File<'a, C> { +impl<'a, C: fmt::Debug + Sync + Send> io::Read for File<'a, C> { fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { match self.0 { Imp::Generic(ref mut reader) => reader.read(buf), @@ -169,7 +169,7 @@ impl<'a, C: fmt::Debug> io::Read for File<'a, C> { } } -impl<'a, C: fmt::Debug> BufferedReader<C> for File<'a, C> { +impl<'a, C: fmt::Debug + Sync + Send> BufferedReader<C> for File<'a, C> { fn buffer(&self) -> &[u8] { match self.0 { Imp::Generic(ref reader) => reader.buffer(), diff --git a/buffered-reader/src/generic.rs b/buffered-reader/src/generic.rs index 25c56272..d2e9acfa 100644 --- a/buffered-reader/src/generic.rs +++ b/buffered-reader/src/generic.rs @@ -13,7 +13,7 @@ use super::*; /// /// [`File`]: struct.File.html /// [`Memory`]: struct.Memory.html -pub struct Generic<T: io::Read, C: fmt::Debug> { +pub struct Generic<T: io::Read + Send + Sync, C: fmt::Debug + Sync + Send> { buffer: Option<Box<[u8]>>, // The next byte to read in the buffer. cursor: usize, @@ -32,13 +32,13 @@ assert_send_and_sync!(Generic<T, C> where T: io::Read, C: fmt::Debug); -impl<T: io::Read, C: fmt::Debug> fmt::Display for Generic<T, C> { +impl<T: io::Read + Send + Sync, C: fmt::Debug + Sync + Send> fmt::Display for Generic<T, C> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "Generic") } } -impl<T: io::Read, C: fmt::Debug> fmt::Debug for Generic<T, C> { +impl<T: io::Read + Send + Sync, C: fmt::Debug + Sync + Send> fmt::Debug for Generic<T, C> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let buffered_data = if let Some(ref buffer) = self.buffer { buffer.len() - self.cursor @@ -53,7 +53,7 @@ impl<T: io::Read, C: fmt::Debug> fmt::Debug for Generic<T, C> { } } -impl<T: io::Read> Generic<T, ()> { +impl<T: io::Read + Send + Sync> Generic<T, ()> { /// Instantiate a new generic reader. `reader` is the source to /// wrap. `preferred_chuck_size` is the preferred chuck size. If /// None, then the default will be used, which is usually what you @@ -63,7 +63,7 @@ impl<T: io::Read> Generic<T, ()> { } } -impl<T: io::Read, C: fmt::Debug> Generic<T, C> { +impl<T: io::Read + Send + Sync, C: fmt::Debug + Sync + Send> Generic<T, C> { /// Like `new()`, but sets a cookie, which can be retrieved using /// the `cookie_ref` and `cookie_mut` methods, and set using /// the `cookie_set` method. @@ -206,13 +206,13 @@ impl<T: io::Read, C: fmt::Debug> Generic<T, C> { } } |