Skip to content

Commit 5f6f252

Browse files
committed
feat(status): add as_u16() method
A `From<StatusCode> for u16` has existed, but the docs cannot show it. It also is slightly more annoying to use if type inference can't figure the types out. This includes an explicit method to get a `u16`.
1 parent 7081c44 commit 5f6f252

File tree

1 file changed

+29
-7
lines changed

1 file changed

+29
-7
lines changed

src/status.rs

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,28 @@ impl StatusCode {
307307
}
308308
}
309309

310-
fn to_u16(&self) -> u16 {
310+
/// Get the `u16` code from this `StatusCode`.
311+
///
312+
/// Also available as `From`/`Into<u16>`.
313+
///
314+
/// # Example
315+
///
316+
/// ```
317+
/// use hyper::StatusCode;
318+
///
319+
/// let status = StatusCode::Ok;
320+
/// assert_eq!(status.as_u16(), 200);
321+
///
322+
/// // Into
323+
/// let num: u16 = status.into();
324+
/// assert_eq!(num, 200);
325+
///
326+
/// // From
327+
/// let other = u16::from(status);
328+
/// assert_eq!(num, other);
329+
/// ```
330+
#[inline]
331+
pub fn as_u16(&self) -> u16 {
311332
match *self {
312333
StatusCode::Continue => 100,
313334
StatusCode::SwitchingProtocols => 101,
@@ -498,7 +519,7 @@ impl StatusCode {
498519
}
499520

500521
fn class(&self) -> StatusClass {
501-
match self.to_u16() {
522+
match self.as_u16() {
502523
100...199 => StatusClass::Informational,
503524
200...299 => StatusClass::Success,
504525
300...399 => StatusClass::Redirection,
@@ -520,16 +541,17 @@ impl Copy for StatusCode {}
520541
/// "123 <unknown status code>");
521542
/// ```
522543
impl fmt::Display for StatusCode {
544+
#[inline]
523545
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
524-
write!(f, "{} {}", self.to_u16(),
546+
write!(f, "{} {}", self.as_u16(),
525547
self.canonical_reason().unwrap_or("<unknown status code>"))
526548
}
527549
}
528550

529551
impl PartialEq for StatusCode {
530552
#[inline]
531553
fn eq(&self, other: &StatusCode) -> bool {
532-
self.to_u16() == other.to_u16()
554+
self.as_u16() == other.as_u16()
533555
}
534556
}
535557

@@ -545,7 +567,7 @@ impl Clone for StatusCode {
545567
impl PartialOrd for StatusCode {
546568
#[inline]
547569
fn partial_cmp(&self, other: &StatusCode) -> Option<Ordering> {
548-
self.to_u16().partial_cmp(&(other.to_u16()))
570+
self.as_u16().partial_cmp(&(other.as_u16()))
549571
}
550572
}
551573

@@ -570,7 +592,7 @@ impl Default for StatusCode {
570592

571593
impl From<StatusCode> for u16 {
572594
fn from(code: StatusCode) -> u16 {
573-
code.to_u16()
595+
code.as_u16()
574596
}
575597
}
576598

@@ -632,7 +654,7 @@ mod tests {
632654
// - canonical reason
633655
fn validate(num: u16, status_code: StatusCode, _default_code: StatusCode, reason: Option<&str>) {
634656
assert_eq!(StatusCode::from_u16(num), status_code);
635-
assert_eq!(status_code.to_u16(), num);
657+
assert_eq!(status_code.as_u16(), num);
636658
//assert_eq!(status_code.class().default_code(), default_code);
637659
assert_eq!(status_code.canonical_reason(), reason);
638660
}

0 commit comments

Comments
 (0)