pub struct ContentEncoding(/* private fields */);Expand description
A wrapper type for content encoding that represents the compression or encoding scheme used in an HTTP message body. This is used in HTTP’s Content-Encoding header.
Implementations§
Source§impl ContentEncoding
impl ContentEncoding
Sourcepub fn new(encoding: Encoding) -> Self
pub fn new(encoding: Encoding) -> Self
Create a new ContentEncoding with the specified encoding
Examples found in repository?
examples/encode_decode.rs (line 132)
117fn content_encoding_examples() {
118 println!("\n\n2. Content-Encoding Examples");
119 println!("============================");
120
121 // Example 1: Creating Content-Encoding
122 println!("\n2a. Creating Content-Encoding headers:");
123 let encodings_to_test = vec![
124 Encoding::Gzip,
125 Encoding::Deflate,
126 Encoding::Br,
127 Encoding::Zstd,
128 Encoding::Custom("lz4".to_string()),
129 ];
130
131 for encoding in encodings_to_test {
132 let content_encoding = ContentEncoding::new(encoding);
133 println!(" Created: {:?}", content_encoding);
134
135 // Encode to header value
136 let mut values = Vec::new();
137 content_encoding.encode(&mut values);
138
139 if let Some(header_value) = values.first() {
140 if let Ok(as_str) = header_value.to_str() {
141 println!(" Header value: {}", as_str);
142 }
143 }
144 }
145
146 // Example 2: Decoding Content-Encoding headers
147 println!("\n2b. Decoding Content-Encoding headers:");
148 let test_headers = vec!["gzip", "deflate", "br", "zstd", "custom-encoding"];
149
150 for header_str in test_headers {
151 println!(" Decoding: \"{}\"", header_str);
152
153 let header_values = vec![HeaderValue::from_str(header_str).unwrap()];
154 match ContentEncoding::decode(&mut header_values.iter()) {
155 Ok(decoded) => {
156 println!(" Success: {:?}", decoded);
157 println!(" Encoding: {:?}", decoded.encoding());
158 }
159 Err(e) => println!(" Error: {:?}", e),
160 }
161 }
162}More examples
examples/basic_usage.rs (line 96)
90fn content_encoding_examples() {
91 println!("\n\n2. Content-Encoding Header Examples");
92 println!("===================================");
93
94 // Example 2a: Creating and encoding Content-Encoding headers
95 println!("\n2a. Creating and encoding Content-Encoding:");
96 let content_encoding = ContentEncoding::new(Encoding::Gzip);
97 println!(" Created ContentEncoding: {:?}", content_encoding);
98
99 // Use with HeaderMap
100 let mut headers = HeaderMap::new();
101 headers.typed_insert(content_encoding);
102
103 if let Some(header_value) = headers.get(http::header::CONTENT_ENCODING) {
104 println!(
105 " Header value: {}",
106 header_value.to_str().unwrap_or("invalid")
107 );
108 }
109
110 // Example 2b: Decoding Content-Encoding headers
111 println!("\n2b. Decoding Content-Encoding headers:");
112 let test_values = vec!["gzip", "deflate", "br", "zstd"];
113
114 for encoding_str in test_values {
115 println!(" Testing: {}", encoding_str);
116 let header_values = vec![HeaderValue::from_str(encoding_str).unwrap()];
117
118 match ContentEncoding::decode(&mut header_values.iter()) {
119 Ok(decoded) => println!(" Decoded: {:?}", decoded),
120 Err(e) => println!(" Error: {:?}", e),
121 }
122 }
123
124 // Example 2c: Multiple identical values (valid)
125 println!("\n2c. Multiple identical Content-Encoding values:");
126 let identical_values = vec![
127 HeaderValue::from_str("gzip").unwrap(),
128 HeaderValue::from_str("gzip").unwrap(),
129 ];
130
131 match ContentEncoding::decode(&mut identical_values.iter()) {
132 Ok(decoded) => println!(" Multiple identical values decoded: {:?}", decoded),
133 Err(e) => println!(" Error: {:?}", e),
134 }
135
136 // Example 2d: Conflicting values (should error)
137 println!("\n2d. Conflicting Content-Encoding values (should error):");
138 let conflicting_values = vec![
139 HeaderValue::from_str("gzip").unwrap(),
140 HeaderValue::from_str("deflate").unwrap(),
141 ];
142
143 match ContentEncoding::decode(&mut conflicting_values.iter()) {
144 Ok(decoded) => println!(" Unexpectedly decoded: {:?}", decoded),
145 Err(_) => println!(" Correctly rejected conflicting values"),
146 }
147}Sourcepub fn encoding(&self) -> &Encoding
pub fn encoding(&self) -> &Encoding
Get the encoding value
Examples found in repository?
examples/encode_decode.rs (line 157)
117fn content_encoding_examples() {
118 println!("\n\n2. Content-Encoding Examples");
119 println!("============================");
120
121 // Example 1: Creating Content-Encoding
122 println!("\n2a. Creating Content-Encoding headers:");
123 let encodings_to_test = vec![
124 Encoding::Gzip,
125 Encoding::Deflate,
126 Encoding::Br,
127 Encoding::Zstd,
128 Encoding::Custom("lz4".to_string()),
129 ];
130
131 for encoding in encodings_to_test {
132 let content_encoding = ContentEncoding::new(encoding);
133 println!(" Created: {:?}", content_encoding);
134
135 // Encode to header value
136 let mut values = Vec::new();
137 content_encoding.encode(&mut values);
138
139 if let Some(header_value) = values.first() {
140 if let Ok(as_str) = header_value.to_str() {
141 println!(" Header value: {}", as_str);
142 }
143 }
144 }
145
146 // Example 2: Decoding Content-Encoding headers
147 println!("\n2b. Decoding Content-Encoding headers:");
148 let test_headers = vec!["gzip", "deflate", "br", "zstd", "custom-encoding"];
149
150 for header_str in test_headers {
151 println!(" Decoding: \"{}\"", header_str);
152
153 let header_values = vec![HeaderValue::from_str(header_str).unwrap()];
154 match ContentEncoding::decode(&mut header_values.iter()) {
155 Ok(decoded) => {
156 println!(" Success: {:?}", decoded);
157 println!(" Encoding: {:?}", decoded.encoding());
158 }
159 Err(e) => println!(" Error: {:?}", e),
160 }
161 }
162}Trait Implementations§
Source§impl Clone for ContentEncoding
impl Clone for ContentEncoding
Source§fn clone(&self) -> ContentEncoding
fn clone(&self) -> ContentEncoding
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for ContentEncoding
impl Debug for ContentEncoding
Source§impl Header for ContentEncoding
Available on crate feature http_crates only.
impl Header for ContentEncoding
Available on crate feature
http_crates only.Source§impl Ord for ContentEncoding
impl Ord for ContentEncoding
Source§fn cmp(&self, other: &ContentEncoding) -> Ordering
fn cmp(&self, other: &ContentEncoding) -> Ordering
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Compares and returns the maximum of two values. Read more
Source§impl PartialEq for ContentEncoding
impl PartialEq for ContentEncoding
Source§impl PartialOrd for ContentEncoding
impl PartialOrd for ContentEncoding
impl Eq for ContentEncoding
impl StructuralPartialEq for ContentEncoding
Auto Trait Implementations§
impl Freeze for ContentEncoding
impl RefUnwindSafe for ContentEncoding
impl Send for ContentEncoding
impl Sync for ContentEncoding
impl Unpin for ContentEncoding
impl UnwindSafe for ContentEncoding
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more