Implement FromIterator for RootCertStore (#1708)

Co-authored-by: Daniel McCarney <daniel@binaryparadox.net>
This commit is contained in:
Niklas Fiekas 2023-12-25 10:58:26 +01:00 committed by GitHub
parent 0cd488dff6
commit 309a5d5051
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 17 additions and 16 deletions

View File

@ -8,8 +8,7 @@ use std::net::TcpStream;
use std::sync::Arc; use std::sync::Arc;
fn main() { fn main() {
let mut root_store = rustls::RootCertStore::empty(); let root_store = rustls::RootCertStore::from_iter(
root_store.extend(
webpki_roots::TLS_SERVER_ROOTS webpki_roots::TLS_SERVER_ROOTS
.iter() .iter()
.cloned(), .cloned(),

View File

@ -66,8 +66,7 @@ fn start_connection(config: &Arc<rustls::ClientConfig>, domain_name: &str) {
fn main() { fn main() {
env_logger::init(); env_logger::init();
let mut root_store = RootCertStore::empty(); let root_store = RootCertStore::from_iter(
root_store.extend(
webpki_roots::TLS_SERVER_ROOTS webpki_roots::TLS_SERVER_ROOTS
.iter() .iter()
.cloned(), .cloned(),

View File

@ -15,8 +15,7 @@ use std::sync::Arc;
use rustls::RootCertStore; use rustls::RootCertStore;
fn main() { fn main() {
let mut root_store = RootCertStore::empty(); let root_store = RootCertStore::from_iter(
root_store.extend(
webpki_roots::TLS_SERVER_ROOTS webpki_roots::TLS_SERVER_ROOTS
.iter() .iter()
.cloned(), .cloned(),

View File

@ -5,8 +5,7 @@ use std::sync::Arc;
fn main() { fn main() {
env_logger::init(); env_logger::init();
let mut root_store = rustls::RootCertStore::empty(); let root_store = rustls::RootCertStore::from_iter(
root_store.extend(
webpki_roots::TLS_SERVER_ROOTS webpki_roots::TLS_SERVER_ROOTS
.iter() .iter()
.cloned(), .cloned(),

View File

@ -116,11 +116,10 @@
//! //!
//! ```rust,no_run //! ```rust,no_run
//! # #[cfg(feature = "ring")] { //! # #[cfg(feature = "ring")] {
//! let mut root_store = rustls::RootCertStore::empty(); //! let root_store = rustls::RootCertStore::from_iter(
//! root_store.extend(
//! webpki_roots::TLS_SERVER_ROOTS //! webpki_roots::TLS_SERVER_ROOTS
//! .iter() //! .iter()
//! .cloned() //! .cloned(),
//! ); //! );
//! # } //! # }
//! ``` //! ```
@ -147,11 +146,10 @@
//! # use rustls; //! # use rustls;
//! # use webpki; //! # use webpki;
//! # use std::sync::Arc; //! # use std::sync::Arc;
//! # let mut root_store = rustls::RootCertStore::empty(); //! # let root_store = rustls::RootCertStore::from_iter(
//! # root_store.extend(
//! # webpki_roots::TLS_SERVER_ROOTS //! # webpki_roots::TLS_SERVER_ROOTS
//! # .iter() //! # .iter()
//! # .cloned() //! # .cloned(),
//! # ); //! # );
//! # let config = rustls::ClientConfig::builder() //! # let config = rustls::ClientConfig::builder()
//! # .with_root_certificates(root_store) //! # .with_root_certificates(root_store)

View File

@ -103,6 +103,14 @@ impl RootCertStore {
} }
} }
impl FromIterator<TrustAnchor<'static>> for RootCertStore {
fn from_iter<T: IntoIterator<Item = TrustAnchor<'static>>>(iter: T) -> Self {
Self {
roots: iter.into_iter().collect(),
}
}
}
impl Extend<TrustAnchor<'static>> for RootCertStore { impl Extend<TrustAnchor<'static>> for RootCertStore {
fn extend<T: IntoIterator<Item = TrustAnchor<'static>>>(&mut self, iter: T) { fn extend<T: IntoIterator<Item = TrustAnchor<'static>>>(&mut self, iter: T) {
self.roots.extend(iter); self.roots.extend(iter);
@ -122,13 +130,12 @@ fn root_cert_store_debug() {
use core::iter; use core::iter;
use pki_types::Der; use pki_types::Der;
let mut store = RootCertStore::empty();
let ta = TrustAnchor { let ta = TrustAnchor {
subject: Der::from_slice(&[]), subject: Der::from_slice(&[]),
subject_public_key_info: Der::from_slice(&[]), subject_public_key_info: Der::from_slice(&[]),
name_constraints: None, name_constraints: None,
}; };
store.extend(iter::repeat(ta).take(138)); let store = RootCertStore::from_iter(iter::repeat(ta).take(138));
assert_eq!( assert_eq!(
format!("{:?}", store), format!("{:?}", store),