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

[Merged by Bors] - remove Serialize impl for dyn Array and friends #4780

Closed
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
40 changes: 1 addition & 39 deletions crates/bevy_reflect/src/array.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::{serde::Serializable, Reflect, ReflectMut, ReflectRef};
use serde::ser::SerializeSeq;
use std::fmt::Debug;
use std::{
any::Any,
Expand Down Expand Up @@ -145,7 +144,7 @@ unsafe impl Reflect for DynamicArray {
}

fn serializable(&self) -> Option<Serializable> {
Some(Serializable::Borrowed(self))
None
}
}

Expand Down Expand Up @@ -211,43 +210,6 @@ impl<'a> Iterator for ArrayIter<'a> {

impl<'a> ExactSizeIterator for ArrayIter<'a> {}

impl serde::Serialize for dyn Array {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
array_serialize(self, serializer)
}
}

impl serde::Serialize for DynamicArray {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
array_serialize(self, serializer)
}
}

/// Serializes the given [array](Array).
#[inline]
pub fn array_serialize<A: Array + ?Sized, S>(array: &A, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let mut seq = serializer.serialize_seq(Some(array.len()))?;
for element in array.iter() {
let serializable = element.serializable().ok_or_else(|| {
serde::ser::Error::custom(format!(
"Type '{}' does not support `Reflect` serialization",
element.type_name()
))
})?;
seq.serialize_element(serializable.borrow())?;
}
seq.end()
}

/// Returns the `u64` hash of the given [array](Array).
#[inline]
pub fn array_hash<A: Array>(array: &A) -> Option<u64> {
Expand Down
16 changes: 2 additions & 14 deletions crates/bevy_reflect/src/impls/std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ unsafe impl<T: FromReflect> Reflect for Vec<T> {
}

fn serializable(&self) -> Option<Serializable> {
Some(Serializable::Owned(Box::new(SerializeArrayLike(self))))
None
}
}

Expand Down Expand Up @@ -396,7 +396,7 @@ unsafe impl<T: Reflect, const N: usize> Reflect for [T; N] {

#[inline]
fn serializable(&self) -> Option<Serializable> {
Some(Serializable::Owned(Box::new(SerializeArrayLike(self))))
None
}
}

Expand All @@ -414,18 +414,6 @@ impl<T: FromReflect, const N: usize> FromReflect for [T; N] {
}
}

// Supports dynamic serialization for types that implement `Array`.
struct SerializeArrayLike<'a>(&'a dyn Array);

impl<'a> serde::Serialize for SerializeArrayLike<'a> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
crate::array_serialize(self.0, serializer)
}
}

// TODO:
// `FromType::from_type` requires `Deserialize<'de>` to be implemented for `T`.
// Currently serde only supports `Deserialize<'de>` for arrays up to size 32.
Expand Down
11 changes: 1 addition & 10 deletions crates/bevy_reflect/src/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ unsafe impl Reflect for DynamicList {
}

fn serializable(&self) -> Option<Serializable> {
Some(Serializable::Borrowed(self))
None
}

fn debug(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
Expand All @@ -182,15 +182,6 @@ impl Debug for DynamicList {
}
}

impl serde::Serialize for DynamicList {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
crate::array_serialize(self, serializer)
}
}

impl IntoIterator for DynamicList {
type Item = Box<dyn Reflect>;
type IntoIter = std::vec::IntoIter<Self::Item>;
Expand Down