hyperium / http

Rust HTTP types
Apache License 2.0
1.12k stars 283 forks source link

A feature request: implement `retain` on HeaderMap #632

Open bassmanitram opened 8 months ago

bassmanitram commented 8 months ago

It would be really handy to have the retain method available (as per std::collections) in order to allow headers to be removed based upon a closure. This would, for example, avoid the double-scan and "clone key" issues when one has a list of headers that must be retained or removed:

let delete_list: Vec<HeaderName> = headers
            .keys()
            .filter(|k| !allowed_headers.contains(*k))
            .map(|k| k.clone())
            .collect::<Vec<HeaderName>>();
for header in delete_list {
    headers.remove(header);
}

would be replaced with

headers.retain(|(k,v)| allowed_headers.contains(*k));