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