Skip to content

Commit 099e419

Browse files
committed
Add more Response conversions
1 parent 1481603 commit 099e419

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

src/body.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,16 @@ impl From<Vec<u8>> for Body {
224224
}
225225
}
226226

227+
impl<'a> From<&'a [u8]> for Body {
228+
fn from(b: &'a [u8]) -> Self {
229+
Self {
230+
length: Some(b.len()),
231+
reader: Box::new(io::Cursor::new(b.to_owned())),
232+
mime: mime::BYTE_STREAM,
233+
}
234+
}
235+
}
236+
227237
impl From<()> for Body {
228238
fn from(_: ()) -> Self {
229239
Self::empty()

src/response.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,12 @@ impl From<Response> for Body {
497497
}
498498
}
499499

500+
impl From<()> for Response {
501+
fn from(_: ()) -> Self {
502+
Response::new(StatusCode::NoContent)
503+
}
504+
}
505+
500506
impl From<String> for Response {
501507
fn from(s: String) -> Self {
502508
let mut res = Response::new(StatusCode::Ok);
@@ -521,6 +527,20 @@ impl From<Vec<u8>> for Response {
521527
}
522528
}
523529

530+
impl<'a> From<&'a [u8]> for Response {
531+
fn from(b: &'a [u8]) -> Self {
532+
let mut res = Response::new(StatusCode::Ok);
533+
res.set_body(b);
534+
res
535+
}
536+
}
537+
538+
impl From<StatusCode> for Response {
539+
fn from(s: StatusCode) -> Self {
540+
Response::new(s)
541+
}
542+
}
543+
524544
impl IntoIterator for Response {
525545
type Item = (HeaderName, Vec<HeaderValue>);
526546
type IntoIter = headers::IntoIter;

0 commit comments

Comments
 (0)