wiki/
boring_impls.rs

1//! This is really boring. Move on.
2
3use std::borrow::Cow;
4use std::fmt;
5use std::num::{NonZeroU16, NonZeroU32, NonZeroU64, NonZeroUsize};
6use std::ops::Deref;
7
8use crate::req::PageSpec;
9use crate::url::{BufferedName, TriStr, UrlParamWriter, WriteUrlValue};
10
11impl Deref for TriStr<'_> {
12    type Target = str;
13    fn deref(&self) -> &Self::Target {
14        match self {
15            Self::Owned(s) => s,
16            Self::Static(s) => s,
17            Self::Shared(s) => s,
18        }
19    }
20}
21
22impl From<TriStr<'_>> for Cow<'static, str> {
23    fn from(s: TriStr<'_>) -> Self {
24        match s {
25            TriStr::Shared(s) => Self::Owned(s.to_owned()),
26            TriStr::Owned(s) => Self::Owned(s),
27            TriStr::Static(s) => Self::Borrowed(s),
28        }
29    }
30}
31
32impl fmt::Display for TriStr<'_> {
33    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34        (**self).fmt(f)
35    }
36}
37
38impl From<String> for TriStr<'_> {
39    fn from(s: String) -> Self {
40        Self::Owned(s)
41    }
42}
43
44impl<'w, T: UrlParamWriter> UrlParamWriter for &'w mut T {
45    type E = T::E;
46    fn add(&mut self, name: TriStr<'_>, value: TriStr<'_>) -> Result<(), Self::E> {
47        (*self).add(name, value)
48    }
49}
50
51macro_rules! display_impls {
52    ($($ty:ty),*$(,)?) => {$(
53        impl WriteUrlValue for $ty {
54            fn ser<W: UrlParamWriter>(&self, w: BufferedName<'_, W>) -> Result<(), W::E> {
55                w.write(TriStr::Owned(self.to_string()))?;
56                Ok(())
57            }
58        }
59    )*};
60}
61
62display_impls! {
63    u8,
64    u16,
65    u32,
66    u64,
67    usize,
68    NonZeroU16,
69    NonZeroU32,
70    NonZeroU64,
71    NonZeroUsize,
72}
73
74impl From<&'_ str> for PageSpec {
75    fn from(s: &'_ str) -> Self {
76        Self::Title(s.into())
77    }
78}