Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve bare_trait_objects lints #42

Merged
merged 1 commit into from
May 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ fn main() {
// be possible with serde::Serializer because of object safety, but type
// erasure makes it possible with erased_serde::Serializer.
let mut formats: Map<&str, Box<dyn Serializer>> = Map::new();
formats.insert("json", Box::new(Serializer::erase(json)));
formats.insert("cbor", Box::new(Serializer::erase(cbor)));
formats.insert("json", Box::new(<dyn Serializer>::erase(json)));
formats.insert("cbor", Box::new(<dyn Serializer>::erase(cbor)));

// These are boxed trait objects as well. Same thing here - type erasure
// makes this possible.
Expand Down Expand Up @@ -92,8 +92,8 @@ fn main() {
// The values in this map are boxed trait objects, which is not possible
// with the normal serde::Deserializer because of object safety.
let mut formats: Map<&str, Box<dyn Deserializer>> = Map::new();
formats.insert("json", Box::new(Deserializer::erase(json)));
formats.insert("cbor", Box::new(Deserializer::erase(cbor)));
formats.insert("json", Box::new(<dyn Deserializer>::erase(json)));
formats.insert("cbor", Box::new(<dyn Deserializer>::erase(cbor)));

// Pick a Deserializer out of the formats map.
let format = formats.get_mut("json").unwrap();
Expand Down
16 changes: 8 additions & 8 deletions src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ use serde::serde_if_integer128;
/// // The values in this map are boxed trait objects, which is not possible
/// // with the normal serde::Deserializer because of object safety.
/// let mut formats: Map<&str, Box<dyn Deserializer>> = Map::new();
/// formats.insert("json", Box::new(Deserializer::erase(json)));
/// formats.insert("cbor", Box::new(Deserializer::erase(cbor)));
/// formats.insert("json", Box::new(<dyn Deserializer>::erase(json)));
/// formats.insert("cbor", Box::new(<dyn Deserializer>::erase(cbor)));
///
/// // Pick a Deserializer out of the formats map.
/// let format = formats.get_mut("json").unwrap();
Expand Down Expand Up @@ -66,8 +66,8 @@ pub trait DeserializeSeed<'de> {
/// // The values in this map are boxed trait objects, which is not possible
/// // with the normal serde::Deserializer because of object safety.
/// let mut formats: Map<&str, Box<dyn Deserializer>> = Map::new();
/// formats.insert("json", Box::new(Deserializer::erase(json)));
/// formats.insert("cbor", Box::new(Deserializer::erase(cbor)));
/// formats.insert("json", Box::new(<dyn Deserializer>::erase(json)));
/// formats.insert("cbor", Box::new(<dyn Deserializer>::erase(cbor)));
///
/// // Pick a Deserializer out of the formats map.
/// let format = formats.get_mut("json").unwrap();
Expand Down Expand Up @@ -220,8 +220,8 @@ impl<'de> dyn Deserializer<'de> {
/// // The values in this map are boxed trait objects, which is not possible
/// // with the normal serde::Deserializer because of object safety.
/// let mut formats: Map<&str, Box<dyn Deserializer>> = Map::new();
/// formats.insert("json", Box::new(Deserializer::erase(json)));
/// formats.insert("cbor", Box::new(Deserializer::erase(cbor)));
/// formats.insert("json", Box::new(<dyn Deserializer>::erase(json)));
/// formats.insert("cbor", Box::new(<dyn Deserializer>::erase(cbor)));
///
/// // Pick a Deserializer out of the formats map.
/// let format = formats.get_mut("json").unwrap();
Expand Down Expand Up @@ -1249,14 +1249,14 @@ mod tests {
// test borrowed trait object
{
let mut de = serde_json::Deserializer::from_slice(json);
let de: &mut dyn Deserializer = &mut Deserializer::erase(&mut de);
let de: &mut dyn Deserializer = &mut <dyn Deserializer>::erase(&mut de);
assert_eq!(expected, deserialize::<T>(de).unwrap());
}

// test boxed trait object
{
let mut de = serde_json::Deserializer::from_slice(json);
let mut de: Box<dyn Deserializer> = Box::new(Deserializer::erase(&mut de));
let mut de: Box<dyn Deserializer> = Box::new(<dyn Deserializer>::erase(&mut de));
assert_eq!(expected, deserialize::<T>(&mut de).unwrap());
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@
//! // be possible with serde::Serializer because of object safety, but type
//! // erasure makes it possible with erased_serde::Serializer.
//! let mut formats: Map<&str, Box<dyn Serializer>> = Map::new();
//! formats.insert("json", Box::new(Serializer::erase(json)));
//! formats.insert("cbor", Box::new(Serializer::erase(cbor)));
//! formats.insert("json", Box::new(<dyn Serializer>::erase(json)));
//! formats.insert("cbor", Box::new(<dyn Serializer>::erase(cbor)));
//!
//! // These are boxed trait objects as well. Same thing here - type erasure
//! // makes this possible.
Expand Down Expand Up @@ -83,8 +83,8 @@
//! // The values in this map are boxed trait objects, which is not possible
//! // with the normal serde::Deserializer because of object safety.
//! let mut formats: Map<&str, Box<dyn Deserializer>> = Map::new();
//! formats.insert("json", Box::new(Deserializer::erase(json)));
//! formats.insert("cbor", Box::new(Deserializer::erase(cbor)));
//! formats.insert("json", Box::new(<dyn Deserializer>::erase(json)));
//! formats.insert("cbor", Box::new(<dyn Deserializer>::erase(cbor)));
//!
//! // Pick a Deserializer out of the formats map.
//! let format = formats.get_mut("json").unwrap();
Expand Down
16 changes: 8 additions & 8 deletions src/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ use serde::serde_if_integer128;
/// // be possible with serde::Serializer because of object safety, but type
/// // erasure makes it possible with erased_serde::Serializer.
/// let mut formats: Map<&str, Box<dyn Serializer>> = Map::new();
/// formats.insert("json", Box::new(Serializer::erase(json)));
/// formats.insert("cbor", Box::new(Serializer::erase(cbor)));
/// formats.insert("json", Box::new(<dyn Serializer>::erase(json)));
/// formats.insert("cbor", Box::new(<dyn Serializer>::erase(cbor)));
///
/// // These are boxed trait objects as well. Same thing here - type erasure
/// // makes this possible.
Expand Down Expand Up @@ -73,8 +73,8 @@ pub trait Serialize {
/// // be possible with serde::Serializer because of object safety, but type
/// // erasure makes it possible with erased_serde::Serializer.
/// let mut formats: Map<&str, Box<dyn Serializer>> = Map::new();
/// formats.insert("json", Box::new(Serializer::erase(json)));
/// formats.insert("cbor", Box::new(Serializer::erase(cbor)));
/// formats.insert("json", Box::new(<dyn Serializer>::erase(json)));
/// formats.insert("cbor", Box::new(<dyn Serializer>::erase(cbor)));
///
/// // These are boxed trait objects as well. Same thing here - type erasure
/// // makes this possible.
Expand Down Expand Up @@ -176,8 +176,8 @@ impl dyn Serializer {
/// // be possible with serde::Serializer because of object safety, but type
/// // erasure makes it possible with erased_serde::Serializer.
/// let mut formats: Map<&str, Box<dyn Serializer>> = Map::new();
/// formats.insert("json", Box::new(Serializer::erase(json)));
/// formats.insert("cbor", Box::new(Serializer::erase(cbor)));
/// formats.insert("json", Box::new(<dyn Serializer>::erase(json)));
/// formats.insert("cbor", Box::new(<dyn Serializer>::erase(cbor)));
///
/// // These are boxed trait objects as well. Same thing here - type erasure
/// // makes this possible.
Expand Down Expand Up @@ -1004,7 +1004,7 @@ mod tests {

{
let mut ser = serde_json::Serializer::new(&mut buf);
let ser: &mut dyn Serializer = &mut Serializer::erase(&mut ser);
let ser: &mut dyn Serializer = &mut <dyn Serializer>::erase(&mut ser);

obj.erased_serialize(ser).unwrap();
}
Expand All @@ -1020,7 +1020,7 @@ mod tests {

{
let mut ser = serde_json::Serializer::new(&mut buf);
let mut ser: Box<dyn Serializer> = Box::new(Serializer::erase(&mut ser));
let mut ser: Box<dyn Serializer> = Box::new(<dyn Serializer>::erase(&mut ser));

obj.erased_serialize(&mut ser).unwrap();
}
Expand Down
8 changes: 4 additions & 4 deletions tests/readme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ fn serialization() {
// be possible with serde::Serializer because of object safety, but type
// erasure makes it possible with erased_serde::Serializer.
let mut formats: Map<&str, Box<dyn Serializer>> = Map::new();
formats.insert("json", Box::new(Serializer::erase(json)));
formats.insert("cbor", Box::new(Serializer::erase(cbor)));
formats.insert("json", Box::new(<dyn Serializer>::erase(json)));
formats.insert("cbor", Box::new(<dyn Serializer>::erase(cbor)));

// These are boxed trait objects as well. Same thing here - type erasure
// makes this possible.
Expand Down Expand Up @@ -45,8 +45,8 @@ fn deserialization() {
// The values in this map are boxed trait objects, which is not possible
// with the normal serde::Deserializer because of object safety.
let mut formats: Map<&str, Box<dyn Deserializer>> = Map::new();
formats.insert("json", Box::new(Deserializer::erase(json)));
formats.insert("cbor", Box::new(Deserializer::erase(cbor)));
formats.insert("json", Box::new(<dyn Deserializer>::erase(json)));
formats.insert("cbor", Box::new(<dyn Deserializer>::erase(cbor)));

// Pick a Deserializer out of the formats map.
let format = formats.get_mut("json").unwrap();
Expand Down