From 69ee1bfe53e1d25e39300d8f512d36f36c69131c Mon Sep 17 00:00:00 2001 From: Casper Beyer Date: Fri, 11 Aug 2023 13:30:23 +0800 Subject: [PATCH] Implement `From` for `HeaderValue` on integer types --- async-nats/src/header.rs | 72 +++++++++++++++++++++++++++++++++++----- 1 file changed, 64 insertions(+), 8 deletions(-) diff --git a/async-nats/src/header.rs b/async-nats/src/header.rs index 8f46a09ac..fcc7d3ba7 100644 --- a/async-nats/src/header.rs +++ b/async-nats/src/header.rs @@ -243,17 +243,51 @@ impl AsRef for HeaderValue { } } -impl FromStr for HeaderValue { - type Err = ParseHeaderValueError; +impl From for HeaderValue { + fn from(v: i16) -> Self { + Self { + inner: v.to_string(), + } + } +} - fn from_str(s: &str) -> Result { - if s.contains(['\r', '\n']) { - return Err(ParseHeaderValueError); +impl From for HeaderValue { + fn from(v: i32) -> Self { + Self { + inner: v.to_string(), } + } +} - Ok(HeaderValue { - inner: s.to_string(), - }) +impl From for HeaderValue { + fn from(v: i64) -> Self { + Self { + inner: v.to_string(), + } + } +} + +impl From for HeaderValue { + fn from(v: isize) -> Self { + Self { + inner: v.to_string(), + } + } +} + +impl From for HeaderValue { + fn from(v: u16) -> Self { + Self { + inner: v.to_string(), + } + } +} + +impl From for HeaderValue { + fn from(v: u32) -> Self { + Self { + inner: v.to_string(), + } } } @@ -265,6 +299,28 @@ impl From for HeaderValue { } } +impl From for HeaderValue { + fn from(v: usize) -> Self { + Self { + inner: v.to_string(), + } + } +} + +impl FromStr for HeaderValue { + type Err = ParseHeaderValueError; + + fn from_str(s: &str) -> Result { + if s.contains(['\r', '\n']) { + return Err(ParseHeaderValueError); + } + + Ok(HeaderValue { + inner: s.to_string(), + }) + } +} + impl From<&str> for HeaderValue { fn from(v: &str) -> Self { Self {