musli/context/
context_error.rs1use core::error::Error;
9use core::fmt;
10
11#[cfg(feature = "alloc")]
12use rust_alloc::string::{String, ToString};
13
14#[diagnostic::on_unimplemented(
16 message = "`ContextError` must be implemented for `{Self}`, or any error type captured by custom contexts",
17 note = "use `musli::context::ErrorMarker` to ignore errors",
18 note = "use `std::io::Error` and `std::string::String`, if the `std` or `alloc` features are enabled for `musli`"
19)]
20pub trait ContextError<A> {
21 fn custom<T>(alloc: A, error: T) -> Self
23 where
24 T: 'static + Send + Sync + Error;
25
26 fn message<T>(alloc: A, message: T) -> Self
31 where
32 T: fmt::Display;
33}
34
35#[cfg(feature = "std")]
36impl<A> ContextError<A> for std::io::Error {
37 #[inline]
38 fn custom<T>(_: A, message: T) -> Self
39 where
40 T: 'static + Send + Sync + Error,
41 {
42 std::io::Error::new(std::io::ErrorKind::Other, message)
43 }
44
45 #[inline]
46 fn message<T>(_: A, message: T) -> Self
47 where
48 T: fmt::Display,
49 {
50 std::io::Error::new(std::io::ErrorKind::Other, std::format!("{message}"))
51 }
52}
53
54#[cfg(feature = "alloc")]
55impl<A> ContextError<A> for String {
56 #[inline]
57 fn custom<T>(_: A, message: T) -> Self
58 where
59 T: fmt::Display,
60 {
61 message.to_string()
62 }
63
64 #[inline]
65 fn message<T>(_: A, message: T) -> Self
66 where
67 T: fmt::Display,
68 {
69 message.to_string()
70 }
71}