From 309a5d5051c42a15d2811a62b476335f36a66ae7 Mon Sep 17 00:00:00 2001 From: Niklas Fiekas Date: Mon, 25 Dec 2023 10:58:26 +0100 Subject: [PATCH] Implement FromIterator for RootCertStore (#1708) Co-authored-by: Daniel McCarney --- examples/src/bin/limitedclient.rs | 3 +-- examples/src/bin/simple_0rtt_client.rs | 3 +-- examples/src/bin/simpleclient.rs | 3 +-- provider-example/examples/client.rs | 3 +-- rustls/src/lib.rs | 10 ++++------ rustls/src/webpki/anchors.rs | 11 +++++++++-- 6 files changed, 17 insertions(+), 16 deletions(-) diff --git a/examples/src/bin/limitedclient.rs b/examples/src/bin/limitedclient.rs index 248c0c40..92e5a4ec 100644 --- a/examples/src/bin/limitedclient.rs +++ b/examples/src/bin/limitedclient.rs @@ -8,8 +8,7 @@ use std::net::TcpStream; use std::sync::Arc; fn main() { - let mut root_store = rustls::RootCertStore::empty(); - root_store.extend( + let root_store = rustls::RootCertStore::from_iter( webpki_roots::TLS_SERVER_ROOTS .iter() .cloned(), diff --git a/examples/src/bin/simple_0rtt_client.rs b/examples/src/bin/simple_0rtt_client.rs index cdc201b7..a20ba5c8 100644 --- a/examples/src/bin/simple_0rtt_client.rs +++ b/examples/src/bin/simple_0rtt_client.rs @@ -66,8 +66,7 @@ fn start_connection(config: &Arc, domain_name: &str) { fn main() { env_logger::init(); - let mut root_store = RootCertStore::empty(); - root_store.extend( + let root_store = RootCertStore::from_iter( webpki_roots::TLS_SERVER_ROOTS .iter() .cloned(), diff --git a/examples/src/bin/simpleclient.rs b/examples/src/bin/simpleclient.rs index 90441442..35b7c15a 100644 --- a/examples/src/bin/simpleclient.rs +++ b/examples/src/bin/simpleclient.rs @@ -15,8 +15,7 @@ use std::sync::Arc; use rustls::RootCertStore; fn main() { - let mut root_store = RootCertStore::empty(); - root_store.extend( + let root_store = RootCertStore::from_iter( webpki_roots::TLS_SERVER_ROOTS .iter() .cloned(), diff --git a/provider-example/examples/client.rs b/provider-example/examples/client.rs index fa625e40..9fab7e09 100644 --- a/provider-example/examples/client.rs +++ b/provider-example/examples/client.rs @@ -5,8 +5,7 @@ use std::sync::Arc; fn main() { env_logger::init(); - let mut root_store = rustls::RootCertStore::empty(); - root_store.extend( + let root_store = rustls::RootCertStore::from_iter( webpki_roots::TLS_SERVER_ROOTS .iter() .cloned(), diff --git a/rustls/src/lib.rs b/rustls/src/lib.rs index 36ce1bc8..3b916782 100644 --- a/rustls/src/lib.rs +++ b/rustls/src/lib.rs @@ -116,11 +116,10 @@ //! //! ```rust,no_run //! # #[cfg(feature = "ring")] { -//! let mut root_store = rustls::RootCertStore::empty(); -//! root_store.extend( +//! let root_store = rustls::RootCertStore::from_iter( //! webpki_roots::TLS_SERVER_ROOTS //! .iter() -//! .cloned() +//! .cloned(), //! ); //! # } //! ``` @@ -147,11 +146,10 @@ //! # use rustls; //! # use webpki; //! # use std::sync::Arc; -//! # let mut root_store = rustls::RootCertStore::empty(); -//! # root_store.extend( +//! # let root_store = rustls::RootCertStore::from_iter( //! # webpki_roots::TLS_SERVER_ROOTS //! # .iter() -//! # .cloned() +//! # .cloned(), //! # ); //! # let config = rustls::ClientConfig::builder() //! # .with_root_certificates(root_store) diff --git a/rustls/src/webpki/anchors.rs b/rustls/src/webpki/anchors.rs index 429832ab..1467b0bf 100644 --- a/rustls/src/webpki/anchors.rs +++ b/rustls/src/webpki/anchors.rs @@ -103,6 +103,14 @@ impl RootCertStore { } } +impl FromIterator> for RootCertStore { + fn from_iter>>(iter: T) -> Self { + Self { + roots: iter.into_iter().collect(), + } + } +} + impl Extend> for RootCertStore { fn extend>>(&mut self, iter: T) { self.roots.extend(iter); @@ -122,13 +130,12 @@ fn root_cert_store_debug() { use core::iter; use pki_types::Der; - let mut store = RootCertStore::empty(); let ta = TrustAnchor { subject: Der::from_slice(&[]), subject_public_key_info: Der::from_slice(&[]), name_constraints: None, }; - store.extend(iter::repeat(ta).take(138)); + let store = RootCertStore::from_iter(iter::repeat(ta).take(138)); assert_eq!( format!("{:?}", store),