diff options
author | Justus Winter <justus@sequoia-pgp.org> | 2020-05-13 16:48:49 +0200 |
---|---|---|
committer | Justus Winter <justus@sequoia-pgp.org> | 2020-05-13 16:49:49 +0200 |
commit | fb8c180a057a5c912a88a8d44f53cd53efb1d2d8 (patch) | |
tree | a918f25e3790ad50cedd1146c9ee8aa34a3606c8 | |
parent | 41f8260e0c8cd04a0d2e5ddce27a48522048385c (diff) |
openpgp: Avoid constructing vectors of recipients.
- Every iterator implements IntoIterator. Simplify accordingly.
-rw-r--r-- | guide/src/chapter_02.md | 16 | ||||
-rw-r--r-- | openpgp/examples/encrypt-for.rs | 4 | ||||
-rw-r--r-- | openpgp/examples/generate-encrypt-decrypt.rs | 4 | ||||
-rw-r--r-- | openpgp/src/serialize/stream.rs | 27 |
4 files changed, 13 insertions, 38 deletions
diff --git a/guide/src/chapter_02.md b/guide/src/chapter_02.md index 95e466f9..e3da3d80 100644 --- a/guide/src/chapter_02.md +++ b/guide/src/chapter_02.md @@ -55,12 +55,10 @@ fn main() { # fn encrypt(policy: &dyn Policy, # sink: &mut Write, plaintext: &str, recipient: &openpgp::Cert) # -> openpgp::Result<()> { -# // Build a vector of recipients to hand to Encryptor. # let recipients = # recipient.keys().with_policy(policy, None).alive().revoked(false) # .for_transport_encryption() -# .map(|ka| ka.key()) -# .collect::<Vec<_>>(); +# .map(|ka| ka.key()); # # // Start streaming an OpenPGP message. # let message = Message::new(sink); @@ -203,12 +201,10 @@ fn generate() -> openpgp::Result<openpgp::Cert> { # fn encrypt(policy: &dyn Policy, # sink: &mut Write, plaintext: &str, recipient: &openpgp::Cert) # -> openpgp::Result<()> { -# // Build a vector of recipients to hand to Encryptor. # let recipients = # recipient.keys().with_policy(policy, None).alive().revoked(false) # .for_transport_encryption() -# .map(|ka| ka.key()) -# .collect::<Vec<_>>(); +# .map(|ka| ka.key()); # # // Start streaming an OpenPGP message. # let message = Message::new(sink); @@ -351,12 +347,10 @@ implements [`io::Write`], and we simply write the plaintext to it. fn encrypt(policy: &dyn Policy, sink: &mut Write, plaintext: &str, recipient: &openpgp::Cert) -> openpgp::Result<()> { - // Build a vector of recipients to hand to Encryptor. let recipients = recipient.keys().with_policy(policy, None).alive().revoked(false) .for_transport_encryption() - .map(|ka| ka.key()) - .collect::<Vec<_>>(); + .map(|ka| ka.key()); // Start streaming an OpenPGP message. let message = Message::new(sink); @@ -513,12 +507,10 @@ Decrypted data can be read from this using [`io::Read`]. # fn encrypt(policy: &dyn Policy, # sink: &mut Write, plaintext: &str, recipient: &openpgp::Cert) # -> openpgp::Result<()> { -# // Build a vector of recipients to hand to Encryptor. # let recipients = # recipient.keys().with_policy(policy, None).alive().revoked(false) # .for_transport_encryption() -# .map(|ka| ka.key()) -# .collect::<Vec<_>>(); +# .map(|ka| ka.key()); # # // Start streaming an OpenPGP message. # let message = Message::new(sink); diff --git a/openpgp/examples/encrypt-for.rs b/openpgp/examples/encrypt-for.rs index d31f2cc9..a7701770 100644 --- a/openpgp/examples/encrypt-for.rs +++ b/openpgp/examples/encrypt-for.rs @@ -37,15 +37,13 @@ fn main() { .expect("Failed to read key") }).collect(); - // Build a vector of recipients to hand to Encryptor. let recipients = certs.iter() .flat_map(|cert| { cert.keys() .with_policy(p, None).alive().revoked(false).key_flags(&mode) }) - .map(|ka| ka.key()) - .collect::<Vec<_>>(); + .map(|ka| ka.key()); // Compose a writer stack corresponding to the output format and // packet structure we want. First, we want the output to be diff --git a/openpgp/examples/generate-encrypt-decrypt.rs b/openpgp/examples/generate-encrypt-decrypt.rs index dba5ebf8..e19c98b0 100644 --- a/openpgp/examples/generate-encrypt-decrypt.rs +++ b/openpgp/examples/generate-encrypt-decrypt.rs @@ -47,12 +47,10 @@ fn encrypt(p: &dyn Policy, sink: &mut dyn Write, plaintext: &str, recipient: &openpgp::Cert) -> openpgp::Result<()> { - // Build a vector of recipients to hand to Encryptor. let recipients = recipient.keys().with_policy(p, None).alive().revoked(false) .for_transport_encryption() - .map(|ka| ka.key()) - .collect::<Vec<_>>(); + .map(|ka| ka.key()); // Start streaming an OpenPGP message. let message = Message::new(sink); diff --git a/openpgp/src/serialize/stream.rs b/openpgp/src/serialize/stream.rs index c68c2099..9d95610d 100644 --- a/openpgp/src/serialize/stream.rs +++ b/openpgp/src/serialize/stream.rs @@ -97,14 +97,12 @@ //! //! let recipient: Cert = // ... //! # sender.clone(); -//! // Build a vector of recipients to hand to the `Encryptor`. //! // Note: One certificate may contain several suitable encryption keys. //! let recipients = //! recipient.keys().with_policy(p, None).alive().revoked(false) //! // Or `for_storage_encryption()`, for data at rest. //! .for_transport_encryption() -//! .map(|ka| ka.key()) -//! .collect::<Vec<_>>(); +//! .map(|ka| ka.key()); //! //! # let mut sink = vec![]; //! let message = Message::new(&mut sink); @@ -1858,13 +1856,11 @@ impl<'a> Recipient<'a> { /// # */ /// )?; /// - /// // Build a vector of recipients to hand to the `Encryptor`. /// let recipients = /// cert.keys().with_policy(p, None).alive().revoked(false) /// // Or `for_storage_encryption()`, for data at rest. /// .for_transport_encryption() - /// .map(|ka| ka.key()) - /// .collect::<Vec<_>>(); + /// .map(|ka| ka.key()); /// /// # let mut sink = vec![]; /// let message = Message::new(&mut sink); @@ -1935,7 +1931,6 @@ impl<'a> Recipient<'a> { /// # */ /// )?; /// - /// // Build a vector of recipients to hand to the `Encryptor`. /// let recipients = /// cert.keys().with_policy(p, None).alive().revoked(false) /// // Or `for_storage_encryption()`, for data at rest. @@ -2007,7 +2002,6 @@ impl<'a> Recipient<'a> { /// # */ /// )?; /// - /// // Build a vector of recipients to hand to the `Encryptor`. /// let recipients = /// cert.keys().with_policy(p, None).alive().revoked(false) /// // Or `for_storage_encryption()`, for data at rest. @@ -2017,8 +2011,7 @@ impl<'a> Recipient<'a> { /// // Set the recipient keyid to the wildcard id. /// r.set_keyid(KeyID::wildcard()); /// r - /// }) - /// .collect::<Vec<_>>(); + /// }); /// /// # let mut sink = vec![]; /// let message = Message::new(&mut sink); @@ -2117,13 +2110,11 @@ impl<'a> Encryptor<'a> { /// # */ /// )?; /// - /// // Build a vector of recipients to hand to the `Encryptor`. /// let recipients = /// cert.keys().with_policy(p, None).alive().revoked(false) /// // Or `for_storage_encryption()`, for data at rest. /// .for_transport_encryption() - /// .map(|ka| ka.key()) - /// .collect::<Vec<_>>(); + /// .map(|ka| ka.key()); /// /// # let mut sink = vec![]; /// let message = Message::new(&mut sink); @@ -2252,13 +2243,11 @@ impl<'a> Encryptor<'a> { /// # */ /// )?; /// - /// // Build a vector of recipients to hand to the `Encryptor`. /// let recipients = /// cert.keys().with_policy(p, None).alive().revoked(false) /// // Or `for_storage_encryption()`, for data at rest. /// .for_transport_encryption() - /// .map(|ka| ka.key()) - /// .collect::<Vec<_>>(); + /// .map(|ka| ka.key()); /// /// # let mut sink = vec![]; /// let message = Message::new(&mut sink); @@ -2338,13 +2327,11 @@ impl<'a> Encryptor<'a> { /// # */ /// )?; /// - /// // Build a vector of recipients to hand to the `Encryptor`. /// let recipients = /// cert.keys().with_policy(p, None).alive().revoked(false) /// // Or `for_storage_encryption()`, for data at rest. /// .for_transport_encryption() - /// .map(|ka| ka.key()) - /// .collect::<Vec<_>>(); + /// .map(|ka| ka.key()); /// /// # let mut sink = vec![]; /// let message = Message::new(&mut sink); @@ -3082,7 +3069,7 @@ mod test { let recipients = tsk .keys().with_policy(p, None) .for_storage_encryption().for_transport_encryption() - .map(|ka| ka.key()).collect::<Vec<_>>(); + .map(|ka| ka.key()); let encryptor = Encryptor::for_recipients(m, recipients) .aead_algo(AEADAlgorithm::EAX) .build().unwrap(); |