From 5fe9bdd3562bf29d02d1ab798bbcff069173306b Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sat, 22 Jan 2022 12:44:52 -0800 Subject: [PATCH] Improve error on compiling with neither std nor alloc Before: error: expected item, found `"serde_json requires that either `std` (default) or `alloc` feature is enabled"` --> src/features_check/error.rs:1:1 | 1 | "serde_json requires that either `std` (default) or `alloc` feature is enabled" | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected item error[E0407]: method `visit_string` is not a member of trait `Visitor` --> src/raw.rs:455:5 | 455 | fn visit_string(self, s: String) -> Result | ^ ------------ help: there is an associated function with a similar name: `visit_str` | _____| | | 456 | | where 457 | | E: de::Error, 458 | | { 459 | | Ok(RawValue::from_owned(s.into_boxed_str())) 460 | | } | |_____^ not a member of trait `Visitor` error[E0046]: not all trait items implemented, missing: `collect_str` --> src/ser.rs:1376:1 | 1376 | impl<'a, W: io::Write, F: Formatter> ser::Serializer for RawValueStrEmitter<'a, W, F> { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ missing `collect_str` in implementation | = help: implement the missing item: `fn collect_str(self, _: &T) -> core::result::Result<::Ok, ::Error> where T: Display { todo!() }` error[E0046]: not all trait items implemented, missing: `collect_str` --> src/value/ser.rs:864:1 | 864 | impl serde::ser::Serializer for RawValueEmitter { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ missing `collect_str` in implementation | = help: implement the missing item: `fn collect_str(self, _: &T) -> core::result::Result<::Ok, ::Error> where T: Display { todo!() }` error[E0599]: no method named `visit_string` found for struct `BoxedFromString` in the current scope --> src/raw.rs:452:14 | 428 | pub struct BoxedFromString; | --------------------------- method `visit_string` not found for this ... 452 | self.visit_string(s.to_owned()) | ^^^^^^^^^^^^ method not found in `BoxedFromString` After: error: expected item, found `"serde_json requires that either `std` (default) or `alloc` feature is enabled"` --> src/features_check/error.rs:1:1 | 1 | "serde_json requires that either `std` (default) or `alloc` feature is enabled" | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected item --- src/raw.rs | 3 ++- src/ser.rs | 7 +++++++ src/value/ser.rs | 7 +++++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/raw.rs b/src/raw.rs index b4a4ef8df..c8377ac82 100644 --- a/src/raw.rs +++ b/src/raw.rs @@ -449,9 +449,10 @@ impl<'de> Visitor<'de> for BoxedFromString { where E: de::Error, { - self.visit_string(s.to_owned()) + Ok(RawValue::from_owned(s.to_owned().into_boxed_str())) } + #[cfg(any(feature = "std", feature = "alloc"))] fn visit_string(self, s: String) -> Result where E: de::Error, diff --git a/src/ser.rs b/src/ser.rs index b497c56c8..db77cd883 100644 --- a/src/ser.rs +++ b/src/ser.rs @@ -1548,6 +1548,13 @@ impl<'a, W: io::Write, F: Formatter> ser::Serializer for RawValueStrEmitter<'a, ) -> Result { Err(ser::Error::custom("expected RawValue")) } + + fn collect_str(self, value: &T) -> Result + where + T: ?Sized + Display, + { + self.serialize_str(&value.to_string()) + } } /// Represents a character escape code in a type-safe manner. diff --git a/src/value/ser.rs b/src/value/ser.rs index 5f59c6373..179380a51 100644 --- a/src/value/ser.rs +++ b/src/value/ser.rs @@ -1020,4 +1020,11 @@ impl serde::ser::Serializer for RawValueEmitter { ) -> Result { Err(invalid_raw_value()) } + + fn collect_str(self, value: &T) -> Result + where + T: ?Sized + Display, + { + self.serialize_str(&value.to_string()) + } }