try_specialize/type_fn.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
/// A trait that defines a mapping between an input type and an output type.
///
/// This trait is used to map specialization types to other wrapped or
/// associated specialization types. If generic types `T1` and `T2` are proven
/// to be equivalent, then types `<Mapper as TypeFn<T1>>::Output` and
/// `<Mapper as TypeFn<T2>>::Output` are also equivalent.
///
/// This trait can also be used to specialize
/// generics of the third-party library types that do not implement
/// [`LifetimeFree`].
///
/// [`LifetimeFree`]: crate::LifetimeFree
///
/// # Examples
///
/// Custom data encoders and decoders with customizable per-type encoding
/// and decoding errors and optimized byte array encoding and decoding.
/// Full example code is available at
/// [`examples/encode.rs`](https://github.com/zheland/try-specialize/blob/v0.1.0/examples/encode.rs).
/// ```rust
/// # use core::convert::Infallible;
/// # use core::{array, slice};
/// # use std::io::{self, Read, Write};
/// #
/// # use try_specialize::{Specialization, TypeFn};
/// #
/// # pub trait Encode {
/// # type EncodeError;
/// # fn encode_to<W>(&self, writer: &mut W) -> Result<(), Self::EncodeError>
/// # where
/// # W: ?Sized + Write;
/// # }
/// #
/// # pub trait Decode: Sized {
/// # type DecodeError;
/// # fn decode_from<R>(reader: &mut R) -> Result<Self, Self::DecodeError>
/// # where
/// # R: ?Sized + Read;
/// # }
/// #
/// # impl Encode for () {
/// # type EncodeError = Infallible;
/// #
/// # #[inline]
/// # fn encode_to<W>(&self, _writer: &mut W) -> Result<(), Self::EncodeError>
/// # where
/// # W: ?Sized + Write,
/// # {
/// # Ok(())
/// # }
/// # }
/// #
/// # impl Decode for () {
/// # type DecodeError = Infallible;
/// #
/// # #[inline]
/// # fn decode_from<R>(_reader: &mut R) -> Result<Self, Self::DecodeError>
/// # where
/// # R: ?Sized + Read,
/// # {
/// # Ok(())
/// # }
/// # }
/// #
/// # impl<T> Encode for Box<T>
/// # where
/// # T: Encode,
/// # {
/// # type EncodeError = T::EncodeError;
/// #
/// # #[inline]
/// # fn encode_to<W>(&self, writer: &mut W) -> Result<(), Self::EncodeError>
/// # where
/// # W: ?Sized + Write,
/// # {
/// # T::encode_to(self, writer)
/// # }
/// # }
/// #
/// # impl<T> Decode for Box<T>
/// # where
/// # T: Decode,
/// # {
/// # type DecodeError = T::DecodeError;
/// #
/// # #[inline]
/// # fn decode_from<R>(reader: &mut R) -> Result<Self, Self::DecodeError>
/// # where
/// # R: ?Sized + Read,
/// # {
/// # Ok(Self::new(T::decode_from(reader)?))
/// # }
/// # }
/// #
/// # impl Encode for u8 {
/// # type EncodeError = io::Error;
/// #
/// # #[inline]
/// # fn encode_to<W>(&self, writer: &mut W) -> Result<(), Self::EncodeError>
/// # where
/// # W: ?Sized + Write,
/// # {
/// # writer.write_all(&[*self])?;
/// # Ok(())
/// # }
/// # }
/// #
/// # impl Decode for u8 {
/// # type DecodeError = io::Error;
/// #
/// # #[inline]
/// # fn decode_from<R>(reader: &mut R) -> Result<Self, Self::DecodeError>
/// # where
/// # R: ?Sized + Read,
/// # {
/// # let mut byte: Self = 0;
/// # reader.read_exact(slice::from_mut(&mut byte))?;
/// # Ok(byte)
/// # }
/// # }
/// // ...
///
/// impl<T> Encode for [T]
/// where
/// T: Encode,
/// {
/// type EncodeError = T::EncodeError;
///
/// #[inline]
/// fn encode_to<W>(&self, writer: &mut W) -> Result<(), Self::EncodeError>
/// where
/// W: ?Sized + Write,
/// {
/// if let Some(spec) = Specialization::<[T], [u8]>::try_new() {
/// // Specialize self from `[T; N]` to `[u32; N]`
/// let bytes: &[u8] = spec.specialize_ref(self);
/// // Map type specialization to its associated error specialization.
/// let spec_err = spec.rev().map::<MapToEncodeError>();
/// writer
/// .write_all(bytes)
/// // Specialize error from `io::Error` to `Self::EncodeError`.
/// .map_err(|err| spec_err.specialize(err))?;
/// } else {
/// for item in self {
/// item.encode_to(writer)?;
/// }
/// }
/// Ok(())
/// }
/// }
///
/// // ...
/// # impl<T, const N: usize> Encode for [T; N]
/// # where
/// # T: Encode,
/// # {
/// # type EncodeError = T::EncodeError;
/// #
/// # #[inline]
/// # fn encode_to<W>(&self, writer: &mut W) -> Result<(), Self::EncodeError>
/// # where
/// # W: ?Sized + Write,
/// # {
/// # self.as_slice().encode_to(writer)
/// # }
/// # }
/// #
/// # impl<T, const N: usize> Decode for [T; N]
/// # where
/// # T: Decode + Default,
/// # {
/// # type DecodeError = T::DecodeError;
/// #
/// # #[inline]
/// # fn decode_from<R>(reader: &mut R) -> Result<Self, Self::DecodeError>
/// # where
/// # R: ?Sized + Read,
/// # {
/// # let spec = Specialization::<[T; N], [u8; N]>::try_new();
/// #
/// # if let Some(spec) = spec {
/// # let mut array = [0; N];
/// # reader
/// # .read_exact(&mut array)
/// # // Specialize `<[u8; N]>::Error` to `<[T; N]>::Error`
/// # .map_err(|err| spec.rev().map::<MapToDecodeError>().specialize(err))?;
/// # // Specialize `[u8; N]` to `[T; N]`
/// # let array = spec.rev().specialize(array);
/// # Ok(array)
/// # } else {
/// # // In real code it can be done without `Default` bound.
/// # // But then the code would be unnecessarily complex for the example.
/// # let mut array = array::from_fn(|_| T::default());
/// # for item in &mut array {
/// # *item = T::decode_from(reader)?;
/// # }
/// # Ok(array)
/// # }
/// # }
/// # }
/// #
/// # struct MapToEncodeError;
/// #
/// # impl<T> TypeFn<T> for MapToEncodeError
/// # where
/// # T: ?Sized + Encode,
/// # {
/// # type Output = T::EncodeError;
/// # }
/// #
/// # struct MapToDecodeError;
/// # impl<T> TypeFn<T> for MapToDecodeError
/// # where
/// # T: Decode,
/// # {
/// # type Output = T::DecodeError;
/// # }
/// #
/// # let mut array_buf = [0; 8];
/// # let mut buf = &mut array_buf[..];
/// # [1_u8, 2, 3].encode_to(&mut buf).unwrap();
/// # 4_u8.encode_to(&mut buf).unwrap();
/// # [(), (), (), ()].encode_to(&mut buf).unwrap();
/// # [5_u8, 6, 7, 8].map(Box::new).encode_to(&mut buf).unwrap();
/// # assert!(9_u8.encode_to(&mut buf).is_err());
/// # assert!([9_u8, 10].encode_to(&mut buf).is_err());
/// # ().encode_to(&mut buf).unwrap();
/// # [(), (), ()].encode_to(&mut buf).unwrap();
/// # assert!([9_u8, 10].map(Box::new).encode_to(&mut buf).is_err());
/// # assert_eq!(array_buf, [1, 2, 3, 4, 5, 6, 7, 8]);
/// #
/// # let buf = &mut array_buf.as_slice();
/// # assert_eq!(u8::decode_from(buf).unwrap(), 1);
/// # assert_eq!(<[u8; 4]>::decode_from(buf).unwrap(), [2, 3, 4, 5]);
/// # assert_eq!(<[(); 16]>::decode_from(buf).unwrap(), [(); 16]);
/// # assert_eq!(<[u8; 1]>::decode_from(buf).unwrap(), [6]);
/// # assert_eq!(
/// # <[Box<u8>; 2]>::decode_from(buf).unwrap(),
/// # [Box::new(7), Box::new(8)]
/// # );
/// # assert!(u8::decode_from(buf).is_err());
/// # assert!(<[u8; 1]>::decode_from(buf).is_err());
/// # assert_eq!(<[(); 2]>::decode_from(buf).unwrap(), [(); 2]);
/// # assert!(<[Box<u8>; 2]>::decode_from(buf).is_err());
/// ```
///
/// We can't use `reader.read_exact(&mut array)?;` in the example above because
/// its error variant is `io::Error` while the function error variant is
/// `T::Error`. But we can use the same specialization, but reversed and mapped:
/// - `[T; N] => [u8; N]`,
/// - with `.rev()`: `[u8; N] => [T; N]`,
/// - with `.map::<MapError>()`: `<[u8; N] as Decode>::Error => <[T; N] as
/// Decode>::Error`,
/// - and for the compiler `<[u8; N] as Decode>::Error` and `io::Error` are
/// equal types, so we can specialize the error as well: `io::Error => <[T; N]
/// as Decode>::Error`.
///
/// Truncated synthetic example with multiple generics specialization for a
/// third-party type:
/// ```rust
/// # #[cfg(feature = "std")] {
/// use try_specialize::{Specialization, TypeFn};
///
/// fn some_generic_fn<K, V>(value: hashbrown::HashMap<K, V>) -> &'static str {
/// struct MapIntoHashMap;
/// impl<K, V> TypeFn<(K, V)> for MapIntoHashMap {
/// type Output = hashbrown::HashMap<K, V>;
/// }
///
/// if let Some(spec) = Specialization::<(K, V), (u32, char)>::try_new() {
/// let spec = spec.map::<MapIntoHashMap>();
/// let value = spec.specialize(value);
/// specialized_impl(value)
/// } else {
/// default_impl(value)
/// }
/// }
///
/// fn default_impl<K, V>(value: hashbrown::HashMap<K, V>) -> &'static str {
/// // ...
/// # "default impl"
/// }
///
/// fn specialized_impl(value: hashbrown::HashMap<u32, char>) -> &'static str {
/// // ...
/// # "specialized impl"
/// }
/// #
/// # assert_eq!(
/// # some_generic_fn([(0_i32, 'a'), (1, 'b'), (2, 'c')].into_iter().collect()),
/// # "default impl"
/// # );
/// #
/// # assert_eq!(
/// # some_generic_fn(
/// # [(0_u32, "zero"), (1, "one"), (2, "two")]
/// # .into_iter()
/// # .collect()
/// # ),
/// # "default impl"
/// # );
/// #
/// # assert_eq!(
/// # some_generic_fn([(0_u32, 'a'), (1, 'b'), (2, 'c')].into_iter().collect()),
/// # "specialized impl"
/// # );
/// # }
/// ```
pub trait TypeFn<T>
where
T: ?Sized,
{
/// The returned type.
type Output: ?Sized;
}