1use super::*;
6use crate::parts_write_adapter::CoreWriteAsPartsWrite;
7use core::convert::Infallible;
8
9pub trait TryWriteable {
96 type Error;
98
99 fn try_write_to<W: fmt::Write + ?Sized>(
152 &self,
153 sink: &mut W,
154 ) -> Result<Result<(), Self::Error>, fmt::Error> {
155 self.try_write_to_parts(&mut CoreWriteAsPartsWrite(sink))
156 }
157
158 fn try_write_to_parts<S: PartsWrite + ?Sized>(
166 &self,
167 sink: &mut S,
168 ) -> Result<Result<(), Self::Error>, fmt::Error>;
169
170 fn writeable_length_hint(&self) -> LengthHint {
175 LengthHint::undefined()
176 }
177
178 #[cfg(feature = "alloc")]
197 fn try_write_to_string(&self) -> Result<Cow<'_, str>, (Self::Error, Cow<'_, str>)> {
198 let hint = self.writeable_length_hint();
199 if hint.is_zero() {
200 return Ok(Cow::Borrowed(""));
201 }
202 let mut output = String::with_capacity(hint.capacity());
203 match self
204 .try_write_to(&mut output)
205 .unwrap_or_else(|fmt::Error| Ok(()))
206 {
207 Ok(()) => Ok(Cow::Owned(output)),
208 Err(e) => Err((e, Cow::Owned(output))),
209 }
210 }
211}
212
213impl<T, E> TryWriteable for Result<T, E>
214where
215 T: Writeable,
216 E: Writeable + Clone,
217{
218 type Error = E;
219
220 #[inline]
221 fn try_write_to<W: fmt::Write + ?Sized>(
222 &self,
223 sink: &mut W,
224 ) -> Result<Result<(), Self::Error>, fmt::Error> {
225 match self {
226 Ok(t) => t.write_to(sink).map(Ok),
227 Err(e) => e.write_to(sink).map(|()| Err(e.clone())),
228 }
229 }
230
231 #[inline]
232 fn try_write_to_parts<S: PartsWrite + ?Sized>(
233 &self,
234 sink: &mut S,
235 ) -> Result<Result<(), Self::Error>, fmt::Error> {
236 match self {
237 Ok(t) => t.write_to_parts(sink).map(Ok),
238 Err(e) => sink
239 .with_part(Part::ERROR, |sink| e.write_to_parts(sink))
240 .map(|()| Err(e.clone())),
241 }
242 }
243
244 #[inline]
245 fn writeable_length_hint(&self) -> LengthHint {
246 match self {
247 Ok(t) => t.writeable_length_hint(),
248 Err(e) => e.writeable_length_hint(),
249 }
250 }
251
252 #[inline]
253 #[cfg(feature = "alloc")]
254 fn try_write_to_string(&self) -> Result<Cow<'_, str>, (Self::Error, Cow<'_, str>)> {
255 match self {
256 Ok(t) => Ok(t.write_to_string()),
257 Err(e) => Err((e.clone(), e.write_to_string())),
258 }
259 }
260}
261
262#[derive(#[automatically_derived]
#[allow(clippy::exhaustive_structs)]
impl<T: ::core::fmt::Debug> ::core::fmt::Debug for
TryWriteableInfallibleAsWriteable<T> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"TryWriteableInfallibleAsWriteable", &&self.0)
}
}Debug, #[automatically_derived]
#[allow(clippy::exhaustive_structs)]
impl<T: ::core::clone::Clone> ::core::clone::Clone for
TryWriteableInfallibleAsWriteable<T> {
#[inline]
fn clone(&self) -> TryWriteableInfallibleAsWriteable<T> {
TryWriteableInfallibleAsWriteable(::core::clone::Clone::clone(&self.0))
}
}Clone, #[automatically_derived]
#[allow(clippy::exhaustive_structs)]
impl<T: ::core::cmp::PartialEq> ::core::cmp::PartialEq for
TryWriteableInfallibleAsWriteable<T> {
#[inline]
fn eq(&self, other: &TryWriteableInfallibleAsWriteable<T>) -> bool {
self.0 == other.0
}
}PartialEq, #[automatically_derived]
#[allow(clippy::exhaustive_structs)]
impl<T: ::core::cmp::Eq> ::core::cmp::Eq for
TryWriteableInfallibleAsWriteable<T> {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) { let _: ::core::cmp::AssertParamIsEq<T>; }
}Eq, #[automatically_derived]
#[allow(clippy::exhaustive_structs)]
impl<T: ::core::cmp::PartialOrd> ::core::cmp::PartialOrd for
TryWriteableInfallibleAsWriteable<T> {
#[inline]
fn partial_cmp(&self, other: &TryWriteableInfallibleAsWriteable<T>)
-> ::core::option::Option<::core::cmp::Ordering> {
::core::cmp::PartialOrd::partial_cmp(&self.0, &other.0)
}
}PartialOrd, #[automatically_derived]
#[allow(clippy::exhaustive_structs)]
impl<T: ::core::cmp::Ord> ::core::cmp::Ord for
TryWriteableInfallibleAsWriteable<T> {
#[inline]
fn cmp(&self, other: &TryWriteableInfallibleAsWriteable<T>)
-> ::core::cmp::Ordering {
::core::cmp::Ord::cmp(&self.0, &other.0)
}
}Ord, #[automatically_derived]
#[allow(clippy::exhaustive_structs)]
impl<T: ::core::hash::Hash> ::core::hash::Hash for
TryWriteableInfallibleAsWriteable<T> {
#[inline]
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
::core::hash::Hash::hash(&self.0, state)
}
}Hash)]
265#[repr(transparent)]
266#[allow(clippy::exhaustive_structs)] pub struct TryWriteableInfallibleAsWriteable<T>(pub T);
268
269impl<T> Writeable for TryWriteableInfallibleAsWriteable<T>
270where
271 T: TryWriteable<Error = Infallible>,
272{
273 #[inline]
274 fn write_to<W: fmt::Write + ?Sized>(&self, sink: &mut W) -> fmt::Result {
275 match self.0.try_write_to(sink) {
276 Ok(Ok(())) => Ok(()),
277 Ok(Err(infallible)) => match infallible {},
278 Err(e) => Err(e),
279 }
280 }
281
282 #[inline]
283 fn write_to_parts<S: PartsWrite + ?Sized>(&self, sink: &mut S) -> fmt::Result {
284 match self.0.try_write_to_parts(sink) {
285 Ok(Ok(())) => Ok(()),
286 Ok(Err(infallible)) => match infallible {},
287 Err(e) => Err(e),
288 }
289 }
290
291 #[inline]
292 fn writeable_length_hint(&self) -> LengthHint {
293 self.0.writeable_length_hint()
294 }
295
296 #[inline]
297 #[cfg(feature = "alloc")]
298 fn write_to_string(&self) -> Cow<'_, str> {
299 match self.0.try_write_to_string() {
300 Ok(s) => s,
301 Err((infallible, _)) => match infallible {},
302 }
303 }
304}
305
306impl<T> fmt::Display for TryWriteableInfallibleAsWriteable<T>
307where
308 T: TryWriteable<Error = Infallible>,
309{
310 #[inline]
311 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
312 self.write_to(f)
313 }
314}
315
316#[derive(#[automatically_derived]
#[allow(clippy::exhaustive_structs)]
impl<T: ::core::fmt::Debug> ::core::fmt::Debug for
WriteableAsTryWriteableInfallible<T> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_tuple_field1_finish(f,
"WriteableAsTryWriteableInfallible", &&self.0)
}
}Debug, #[automatically_derived]
#[allow(clippy::exhaustive_structs)]
impl<T: ::core::clone::Clone> ::core::clone::Clone for
WriteableAsTryWriteableInfallible<T> {
#[inline]
fn clone(&self) -> WriteableAsTryWriteableInfallible<T> {
WriteableAsTryWriteableInfallible(::core::clone::Clone::clone(&self.0))
}
}Clone, #[automatically_derived]
#[allow(clippy::exhaustive_structs)]
impl<T: ::core::cmp::PartialEq> ::core::cmp::PartialEq for
WriteableAsTryWriteableInfallible<T> {
#[inline]
fn eq(&self, other: &WriteableAsTryWriteableInfallible<T>) -> bool {
self.0 == other.0
}
}PartialEq, #[automatically_derived]
#[allow(clippy::exhaustive_structs)]
impl<T: ::core::cmp::Eq> ::core::cmp::Eq for
WriteableAsTryWriteableInfallible<T> {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) { let _: ::core::cmp::AssertParamIsEq<T>; }
}Eq, #[automatically_derived]
#[allow(clippy::exhaustive_structs)]
impl<T: ::core::cmp::PartialOrd> ::core::cmp::PartialOrd for
WriteableAsTryWriteableInfallible<T> {
#[inline]
fn partial_cmp(&self, other: &WriteableAsTryWriteableInfallible<T>)
-> ::core::option::Option<::core::cmp::Ordering> {
::core::cmp::PartialOrd::partial_cmp(&self.0, &other.0)
}
}PartialOrd, #[automatically_derived]
#[allow(clippy::exhaustive_structs)]
impl<T: ::core::cmp::Ord> ::core::cmp::Ord for
WriteableAsTryWriteableInfallible<T> {
#[inline]
fn cmp(&self, other: &WriteableAsTryWriteableInfallible<T>)
-> ::core::cmp::Ordering {
::core::cmp::Ord::cmp(&self.0, &other.0)
}
}Ord, #[automatically_derived]
#[allow(clippy::exhaustive_structs)]
impl<T: ::core::hash::Hash> ::core::hash::Hash for
WriteableAsTryWriteableInfallible<T> {
#[inline]
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
::core::hash::Hash::hash(&self.0, state)
}
}Hash)]
319#[repr(transparent)]
320#[allow(clippy::exhaustive_structs)] pub struct WriteableAsTryWriteableInfallible<T>(pub T);
322
323impl<T> TryWriteable for WriteableAsTryWriteableInfallible<T>
324where
325 T: Writeable,
326{
327 type Error = Infallible;
328
329 #[inline]
330 fn try_write_to<W: fmt::Write + ?Sized>(
331 &self,
332 sink: &mut W,
333 ) -> Result<Result<(), Infallible>, fmt::Error> {
334 self.0.write_to(sink).map(Ok)
335 }
336
337 #[inline]
338 fn try_write_to_parts<S: PartsWrite + ?Sized>(
339 &self,
340 sink: &mut S,
341 ) -> Result<Result<(), Infallible>, fmt::Error> {
342 self.0.write_to_parts(sink).map(Ok)
343 }
344
345 #[inline]
346 fn writeable_length_hint(&self) -> LengthHint {
347 self.0.writeable_length_hint()
348 }
349
350 #[inline]
351 #[cfg(feature = "alloc")]
352 fn try_write_to_string(&self) -> Result<Cow<'_, str>, (Infallible, Cow<'_, str>)> {
353 Ok(self.0.write_to_string())
354 }
355}
356
357#[macro_export]
378macro_rules! assert_try_writeable_eq {
379 ($actual_writeable:expr, $expected_str:expr $(,)?) => {
380 $crate::assert_try_writeable_eq!($actual_writeable, $expected_str, Ok(()))
381 };
382 ($actual_writeable:expr, $expected_str:expr, $expected_result:expr $(,)?) => {
383 $crate::assert_try_writeable_eq!($actual_writeable, $expected_str, $expected_result, "")
384 };
385 ($actual_writeable:expr, $expected_str:expr, $expected_result:expr, $($arg:tt)+) => {{
386 $crate::assert_try_writeable_eq!(@internal, $actual_writeable, $expected_str, $expected_result, $($arg)*);
387 }};
388 (@internal, $actual_writeable:expr, $expected_str:expr, $expected_result:expr, $($arg:tt)+) => {{
389 use $crate::TryWriteable;
390 let actual_writeable = &$actual_writeable;
391 let (actual_str, actual_parts, actual_error) = $crate::_internal::try_writeable_to_parts_for_test(actual_writeable);
392 assert_eq!(actual_str, $expected_str, $($arg)*);
393 assert_eq!(actual_error, Result::<(), _>::from($expected_result).err(), $($arg)*);
394 let actual_result = match actual_writeable.try_write_to_string() {
395 Ok(actual_cow_str) => {
396 assert_eq!(actual_cow_str, $expected_str, $($arg)+);
397 Ok(())
398 }
399 Err((e, actual_cow_str)) => {
400 assert_eq!(actual_cow_str, $expected_str, $($arg)+);
401 Err(e)
402 }
403 };
404 assert_eq!(actual_result, Result::<(), _>::from($expected_result), $($arg)*);
405 let length_hint = actual_writeable.writeable_length_hint();
406 assert!(
407 length_hint.0 <= actual_str.len(),
408 "hint lower bound {} larger than actual length {}: {}",
409 length_hint.0, actual_str.len(), format!($($arg)*),
410 );
411 if let Some(upper) = length_hint.1 {
412 assert!(
413 actual_str.len() <= upper,
414 "hint upper bound {} smaller than actual length {}: {}",
415 length_hint.0, actual_str.len(), format!($($arg)*),
416 );
417 }
418 actual_parts }};
420}
421
422#[macro_export]
424macro_rules! assert_try_writeable_parts_eq {
425 ($actual_writeable:expr, $expected_str:expr, $expected_parts:expr $(,)?) => {
426 $crate::assert_try_writeable_parts_eq!($actual_writeable, $expected_str, Ok(()), $expected_parts)
427 };
428 ($actual_writeable:expr, $expected_str:expr, $expected_result:expr, $expected_parts:expr $(,)?) => {
429 $crate::assert_try_writeable_parts_eq!($actual_writeable, $expected_str, $expected_result, $expected_parts, "")
430 };
431 ($actual_writeable:expr, $expected_str:expr, $expected_result:expr, $expected_parts:expr, $($arg:tt)+) => {{
432 let actual_parts = $crate::assert_try_writeable_eq!(@internal, $actual_writeable, $expected_str, $expected_result, $($arg)*);
433 assert_eq!(actual_parts, $expected_parts, $($arg)+);
434 }};
435}
436
437#[test]
438fn test_result_try_writeable() {
439 let mut result: Result<&str, usize> = Ok("success");
440 assert_try_writeable_eq!(result, "success");
441 result = Err(44);
442 assert_try_writeable_eq!(result, "44", Err(44));
443 assert_try_writeable_parts_eq!(result, "44", Err(44), [(0, 2, Part::ERROR)])
444}