Skip to content

Commit 98fbcf7

Browse files
author
zeroed
committed
Status should take TryInto<StatusCode>
Change the trait bound of S to accept a TryInto<StatusCode>; this would allow the Status trait to accept a wider range of input. Change the trait bound for 'with_status'. Refs: #155
1 parent daa327c commit 98fbcf7

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

src/status.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::{Error, StatusCode};
2-
use core::convert::{Infallible, Into, TryInto};
2+
use core::convert::{Infallible, TryInto};
33
use std::error::Error as StdError;
44
use std::fmt::Debug;
55

@@ -17,7 +17,8 @@ pub trait Status<T, E>: private::Sealed {
1717
/// lazily only once an error does occur.
1818
fn with_status<S, F>(self, f: F) -> Result<T, Error>
1919
where
20-
S: Into<StatusCode>,
20+
S: TryInto<StatusCode>,
21+
S::Error: Debug,
2122
F: FnOnce() -> S;
2223
}
2324

@@ -40,11 +41,14 @@ where
4041

4142
fn with_status<S, F>(self, f: F) -> Result<T, Error>
4243
where
43-
S: Into<StatusCode>,
44+
S: TryInto<StatusCode>,
45+
S::Error: Debug,
4446
F: FnOnce() -> S,
4547
{
4648
self.map_err(|error| {
47-
let status = f().into();
49+
let status = f()
50+
.try_into()
51+
.expect("Could not convert into a valid `StatusCode`");
4852
Error::new(status, error)
4953
})
5054
}
@@ -66,11 +70,14 @@ impl<T> Status<T, Infallible> for Option<T> {
6670

6771
fn with_status<S, F>(self, f: F) -> Result<T, Error>
6872
where
69-
S: Into<StatusCode>,
73+
S: TryInto<StatusCode>,
74+
S::Error: Debug,
7075
F: FnOnce() -> S,
7176
{
7277
self.ok_or_else(|| {
73-
let status = f().into();
78+
let status = f()
79+
.try_into()
80+
.expect("Could not convert into a valid `StatusCode`");
7481
Error::from_str(status, "NoneError")
7582
})
7683
}

0 commit comments

Comments
 (0)