Skip to content

Commit daa327c

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 'status'. Refs: #155
1 parent 7e06db5 commit daa327c

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

src/status.rs

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

56
/// Provides the `status` method for `Result` and `Option`.
67
///
@@ -9,7 +10,8 @@ pub trait Status<T, E>: private::Sealed {
910
/// Wrap the error value with an additional status code.
1011
fn status<S>(self, status: S) -> Result<T, Error>
1112
where
12-
S: Into<StatusCode>;
13+
S: TryInto<StatusCode>,
14+
S::Error: Debug;
1315

1416
/// Wrap the error value with an additional status code that is evaluated
1517
/// lazily only once an error does occur.
@@ -25,10 +27,13 @@ where
2527
{
2628
fn status<S>(self, status: S) -> Result<T, Error>
2729
where
28-
S: Into<StatusCode>,
30+
S: TryInto<StatusCode>,
31+
S::Error: Debug,
2932
{
3033
self.map_err(|error| {
31-
let status = status.into();
34+
let status = status
35+
.try_into()
36+
.expect("Could not convert into a valid `StatusCode`");
3237
Error::new(status, error)
3338
})
3439
}
@@ -48,10 +53,13 @@ where
4853
impl<T> Status<T, Infallible> for Option<T> {
4954
fn status<S>(self, status: S) -> Result<T, Error>
5055
where
51-
S: Into<StatusCode>,
56+
S: TryInto<StatusCode>,
57+
S::Error: Debug,
5258
{
5359
self.ok_or_else(|| {
54-
let status = status.into();
60+
let status = status
61+
.try_into()
62+
.expect("Could not convert into a valid `StatusCode`");
5563
Error::from_str(status, "NoneError")
5664
})
5765
}

0 commit comments

Comments
 (0)