pub struct HeadWriter<'v> { /* private fields */ }Expand description
Writes a message head into a caller-owned buffer (reuse it to avoid allocation). Field names
and values are validated, so a value cannot split the response; framing goes through
content_length and chunked only.
Implementations§
Source§impl<'v> HeadWriter<'v>
impl<'v> HeadWriter<'v>
Sourcepub fn response(out: &'v mut Vec<u8>, status: u16) -> Self
pub fn response(out: &'v mut Vec<u8>, status: u16) -> Self
Starts a response: HTTP/1.1 <status> <reason>. status must be 100 to 999.
Examples found in repository?
examples/bench_parse.rs (line 91)
60fn main() {
61 let twenty = twenty_headers();
62 let cases: [(&str, &[u8]); 3] = [("curl (3 fields)", CURL), ("browser (15 fields)", BROWSER), ("20 fields", &twenty)];
63 let best = level();
64 println!("scan level detected: {best:?}");
65 println!("{:<22} {:>6} {:>12} {:>12} {:>14} {:>9}", "request", "bytes", "simd ns", "scalar ns", "cameleer ns", "GB/s simd");
66 for (name, req) in cases {
67 assert!(cameleer::parse(req).is_ok());
68 force(best);
69 let simd = measure(|| {
70 black_box(simoom(req));
71 });
72 force(Level::Scalar);
73 let scalar = measure(|| {
74 black_box(simoom(req));
75 });
76 force(best);
77 let cam = measure(|| {
78 black_box(cameleer::parse(black_box(req)).ok());
79 });
80 println!(
81 "{name:<22} {:>6} {simd:>12.1} {scalar:>12.1} {cam:>14.1} {:>9.2}",
82 req.len(),
83 req.len() as f64 / simd
84 );
85 }
86 // Serialization.
87 let date = DateCache::new(1_700_000_000);
88 let mut out = Vec::with_capacity(256);
89 let head = measure(|| {
90 out.clear();
91 let mut w = HeadWriter::response(&mut out, 200);
92 w.header(b"Content-Type", b"application/json").unwrap().date(&date).content_length(1234);
93 w.finish();
94 black_box(&out);
95 });
96 let mut fixed = StaticHead::new(200, &[(b"Content-Type", b"text/plain"), (b"Server", b"simoom")], b"Hello, World!").unwrap();
97 let mut secs = 1_700_000_000;
98 let mut dc = DateCache::new(secs);
99 let stat = measure(|| {
100 black_box(fixed.full(&dc).len());
101 });
102 let stat_tick = measure(|| {
103 secs += 1;
104 dc.update(secs);
105 black_box(fixed.full(&dc).len());
106 });
107 println!("HeadWriter (status, 1 field, Date, Content-Length): {head:.1} ns");
108 println!("StaticHead::full (fixed reply, same second): {stat:.1} ns; with a new second every call: {stat_tick:.1} ns");
109}Sourcepub fn request(
out: &'v mut Vec<u8>,
method: &[u8],
target: &[u8],
) -> Result<Self, Error>
pub fn request( out: &'v mut Vec<u8>, method: &[u8], target: &[u8], ) -> Result<Self, Error>
Starts a request: <method> <target> HTTP/1.1.
Sourcepub fn header(&mut self, name: &[u8], value: &[u8]) -> Result<&mut Self, Error>
pub fn header(&mut self, name: &[u8], value: &[u8]) -> Result<&mut Self, Error>
Adds a field.
Examples found in repository?
examples/bench_parse.rs (line 92)
60fn main() {
61 let twenty = twenty_headers();
62 let cases: [(&str, &[u8]); 3] = [("curl (3 fields)", CURL), ("browser (15 fields)", BROWSER), ("20 fields", &twenty)];
63 let best = level();
64 println!("scan level detected: {best:?}");
65 println!("{:<22} {:>6} {:>12} {:>12} {:>14} {:>9}", "request", "bytes", "simd ns", "scalar ns", "cameleer ns", "GB/s simd");
66 for (name, req) in cases {
67 assert!(cameleer::parse(req).is_ok());
68 force(best);
69 let simd = measure(|| {
70 black_box(simoom(req));
71 });
72 force(Level::Scalar);
73 let scalar = measure(|| {
74 black_box(simoom(req));
75 });
76 force(best);
77 let cam = measure(|| {
78 black_box(cameleer::parse(black_box(req)).ok());
79 });
80 println!(
81 "{name:<22} {:>6} {simd:>12.1} {scalar:>12.1} {cam:>14.1} {:>9.2}",
82 req.len(),
83 req.len() as f64 / simd
84 );
85 }
86 // Serialization.
87 let date = DateCache::new(1_700_000_000);
88 let mut out = Vec::with_capacity(256);
89 let head = measure(|| {
90 out.clear();
91 let mut w = HeadWriter::response(&mut out, 200);
92 w.header(b"Content-Type", b"application/json").unwrap().date(&date).content_length(1234);
93 w.finish();
94 black_box(&out);
95 });
96 let mut fixed = StaticHead::new(200, &[(b"Content-Type", b"text/plain"), (b"Server", b"simoom")], b"Hello, World!").unwrap();
97 let mut secs = 1_700_000_000;
98 let mut dc = DateCache::new(secs);
99 let stat = measure(|| {
100 black_box(fixed.full(&dc).len());
101 });
102 let stat_tick = measure(|| {
103 secs += 1;
104 dc.update(secs);
105 black_box(fixed.full(&dc).len());
106 });
107 println!("HeadWriter (status, 1 field, Date, Content-Length): {head:.1} ns");
108 println!("StaticHead::full (fixed reply, same second): {stat:.1} ns; with a new second every call: {stat_tick:.1} ns");
109}Sourcepub fn date(&mut self, date: &DateCache) -> &mut Self
pub fn date(&mut self, date: &DateCache) -> &mut Self
Adds Date from the cache.
Examples found in repository?
examples/bench_parse.rs (line 92)
60fn main() {
61 let twenty = twenty_headers();
62 let cases: [(&str, &[u8]); 3] = [("curl (3 fields)", CURL), ("browser (15 fields)", BROWSER), ("20 fields", &twenty)];
63 let best = level();
64 println!("scan level detected: {best:?}");
65 println!("{:<22} {:>6} {:>12} {:>12} {:>14} {:>9}", "request", "bytes", "simd ns", "scalar ns", "cameleer ns", "GB/s simd");
66 for (name, req) in cases {
67 assert!(cameleer::parse(req).is_ok());
68 force(best);
69 let simd = measure(|| {
70 black_box(simoom(req));
71 });
72 force(Level::Scalar);
73 let scalar = measure(|| {
74 black_box(simoom(req));
75 });
76 force(best);
77 let cam = measure(|| {
78 black_box(cameleer::parse(black_box(req)).ok());
79 });
80 println!(
81 "{name:<22} {:>6} {simd:>12.1} {scalar:>12.1} {cam:>14.1} {:>9.2}",
82 req.len(),
83 req.len() as f64 / simd
84 );
85 }
86 // Serialization.
87 let date = DateCache::new(1_700_000_000);
88 let mut out = Vec::with_capacity(256);
89 let head = measure(|| {
90 out.clear();
91 let mut w = HeadWriter::response(&mut out, 200);
92 w.header(b"Content-Type", b"application/json").unwrap().date(&date).content_length(1234);
93 w.finish();
94 black_box(&out);
95 });
96 let mut fixed = StaticHead::new(200, &[(b"Content-Type", b"text/plain"), (b"Server", b"simoom")], b"Hello, World!").unwrap();
97 let mut secs = 1_700_000_000;
98 let mut dc = DateCache::new(secs);
99 let stat = measure(|| {
100 black_box(fixed.full(&dc).len());
101 });
102 let stat_tick = measure(|| {
103 secs += 1;
104 dc.update(secs);
105 black_box(fixed.full(&dc).len());
106 });
107 println!("HeadWriter (status, 1 field, Date, Content-Length): {head:.1} ns");
108 println!("StaticHead::full (fixed reply, same second): {stat:.1} ns; with a new second every call: {stat_tick:.1} ns");
109}Sourcepub fn content_length(&mut self, n: u64) -> &mut Self
pub fn content_length(&mut self, n: u64) -> &mut Self
Adds Content-Length.
Examples found in repository?
examples/bench_parse.rs (line 92)
60fn main() {
61 let twenty = twenty_headers();
62 let cases: [(&str, &[u8]); 3] = [("curl (3 fields)", CURL), ("browser (15 fields)", BROWSER), ("20 fields", &twenty)];
63 let best = level();
64 println!("scan level detected: {best:?}");
65 println!("{:<22} {:>6} {:>12} {:>12} {:>14} {:>9}", "request", "bytes", "simd ns", "scalar ns", "cameleer ns", "GB/s simd");
66 for (name, req) in cases {
67 assert!(cameleer::parse(req).is_ok());
68 force(best);
69 let simd = measure(|| {
70 black_box(simoom(req));
71 });
72 force(Level::Scalar);
73 let scalar = measure(|| {
74 black_box(simoom(req));
75 });
76 force(best);
77 let cam = measure(|| {
78 black_box(cameleer::parse(black_box(req)).ok());
79 });
80 println!(
81 "{name:<22} {:>6} {simd:>12.1} {scalar:>12.1} {cam:>14.1} {:>9.2}",
82 req.len(),
83 req.len() as f64 / simd
84 );
85 }
86 // Serialization.
87 let date = DateCache::new(1_700_000_000);
88 let mut out = Vec::with_capacity(256);
89 let head = measure(|| {
90 out.clear();
91 let mut w = HeadWriter::response(&mut out, 200);
92 w.header(b"Content-Type", b"application/json").unwrap().date(&date).content_length(1234);
93 w.finish();
94 black_box(&out);
95 });
96 let mut fixed = StaticHead::new(200, &[(b"Content-Type", b"text/plain"), (b"Server", b"simoom")], b"Hello, World!").unwrap();
97 let mut secs = 1_700_000_000;
98 let mut dc = DateCache::new(secs);
99 let stat = measure(|| {
100 black_box(fixed.full(&dc).len());
101 });
102 let stat_tick = measure(|| {
103 secs += 1;
104 dc.update(secs);
105 black_box(fixed.full(&dc).len());
106 });
107 println!("HeadWriter (status, 1 field, Date, Content-Length): {head:.1} ns");
108 println!("StaticHead::full (fixed reply, same second): {stat:.1} ns; with a new second every call: {stat_tick:.1} ns");
109}Sourcepub fn finish(self)
pub fn finish(self)
Ends the head with the empty line.
Examples found in repository?
examples/bench_parse.rs (line 93)
60fn main() {
61 let twenty = twenty_headers();
62 let cases: [(&str, &[u8]); 3] = [("curl (3 fields)", CURL), ("browser (15 fields)", BROWSER), ("20 fields", &twenty)];
63 let best = level();
64 println!("scan level detected: {best:?}");
65 println!("{:<22} {:>6} {:>12} {:>12} {:>14} {:>9}", "request", "bytes", "simd ns", "scalar ns", "cameleer ns", "GB/s simd");
66 for (name, req) in cases {
67 assert!(cameleer::parse(req).is_ok());
68 force(best);
69 let simd = measure(|| {
70 black_box(simoom(req));
71 });
72 force(Level::Scalar);
73 let scalar = measure(|| {
74 black_box(simoom(req));
75 });
76 force(best);
77 let cam = measure(|| {
78 black_box(cameleer::parse(black_box(req)).ok());
79 });
80 println!(
81 "{name:<22} {:>6} {simd:>12.1} {scalar:>12.1} {cam:>14.1} {:>9.2}",
82 req.len(),
83 req.len() as f64 / simd
84 );
85 }
86 // Serialization.
87 let date = DateCache::new(1_700_000_000);
88 let mut out = Vec::with_capacity(256);
89 let head = measure(|| {
90 out.clear();
91 let mut w = HeadWriter::response(&mut out, 200);
92 w.header(b"Content-Type", b"application/json").unwrap().date(&date).content_length(1234);
93 w.finish();
94 black_box(&out);
95 });
96 let mut fixed = StaticHead::new(200, &[(b"Content-Type", b"text/plain"), (b"Server", b"simoom")], b"Hello, World!").unwrap();
97 let mut secs = 1_700_000_000;
98 let mut dc = DateCache::new(secs);
99 let stat = measure(|| {
100 black_box(fixed.full(&dc).len());
101 });
102 let stat_tick = measure(|| {
103 secs += 1;
104 dc.update(secs);
105 black_box(fixed.full(&dc).len());
106 });
107 println!("HeadWriter (status, 1 field, Date, Content-Length): {head:.1} ns");
108 println!("StaticHead::full (fixed reply, same second): {stat:.1} ns; with a new second every call: {stat_tick:.1} ns");
109}Trait Implementations§
Auto Trait Implementations§
impl<'v> !UnwindSafe for HeadWriter<'v>
impl<'v> Forget for HeadWriter<'v>
impl<'v> Freeze for HeadWriter<'v>
impl<'v> Move for HeadWriter<'v>
impl<'v> RefUnwindSafe for HeadWriter<'v>
impl<'v> Send for HeadWriter<'v>
impl<'v> Sync for HeadWriter<'v>
impl<'v> Unpin for HeadWriter<'v>
impl<'v> UnsafeUnpin for HeadWriter<'v>
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