Skip to content

Commit f4d79d9

Browse files
committed
Add doc to serialize and deserialize
1 parent 70d1954 commit f4d79d9

2 files changed

Lines changed: 104 additions & 0 deletions

File tree

src/all_storages/mod.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1807,6 +1807,28 @@ impl core::fmt::Debug for AllStoragesMemoryUsage<'_> {
18071807
#[cfg(feature = "serde1")]
18081808
impl AllStorages {
18091809
/// Serializes the view using the provided serializer.
1810+
///
1811+
/// ### Example
1812+
///
1813+
/// ```
1814+
/// use shipyard::{AllStoragesViewMut, Component, View, World};
1815+
///
1816+
/// #[derive(Component, serde::Serialize)]
1817+
/// struct Name(String);
1818+
///
1819+
/// let world = World::new();
1820+
/// let mut all_storages = world.borrow::<AllStoragesViewMut>().unwrap();
1821+
///
1822+
/// let eid1 = all_storages.add_entity(Name("Alice".to_string()));
1823+
///
1824+
/// let mut serialized = Vec::new();
1825+
/// all_storages
1826+
/// .serialize::<_, View<Name>>(&mut serde_json::ser::Serializer::new(&mut serialized))
1827+
/// .unwrap_or_else(|_| panic!());
1828+
///
1829+
/// let serialized_str = String::from_utf8(serialized).unwrap();
1830+
/// assert_eq!(serialized_str, r#"[[{"index":0,"gen":0},"Alice"]]"#);
1831+
/// ```
18101832
pub fn serialize<'a, S: serde::Serializer, V: Borrow>(
18111833
&'a self,
18121834
serializer: S,
@@ -1826,6 +1848,37 @@ impl AllStorages {
18261848
}
18271849

18281850
/// Deserializes the view using the provided deserializer.
1851+
///
1852+
/// ### Example
1853+
///
1854+
/// ```
1855+
/// use shipyard::{AllStoragesViewMut, Component, EntityId, ViewMut, World};
1856+
///
1857+
/// #[derive(Component, serde::Deserialize)]
1858+
/// struct Name(String);
1859+
///
1860+
/// let world = World::new();
1861+
/// let mut all_storages = world.borrow::<AllStoragesViewMut>().unwrap();
1862+
///
1863+
/// let mut serialized = r#"[[{"index":0,"gen":0},"Alice"]]"#;
1864+
/// all_storages
1865+
/// .deserialize::<_, ViewMut<Name>>(&mut serde_json::de::Deserializer::from_str(serialized))
1866+
/// .unwrap_or_else(|_| panic!());
1867+
///
1868+
/// let alice_eid = EntityId::new_from_index_and_gen(0, 0);
1869+
/// assert_eq!(all_storages.get::<&Name>(alice_eid).unwrap().0, "Alice");
1870+
///
1871+
/// // Careful here, the World is not in a stable state
1872+
///
1873+
/// assert_eq!(all_storages.is_entity_alive(alice_eid), false);
1874+
///
1875+
/// // We can use World::spawn for example to fix the problem
1876+
/// // another solution would be to serialize EntitiesViewMut
1877+
///
1878+
/// all_storages.spawn(alice_eid);
1879+
///
1880+
/// assert_eq!(all_storages.is_entity_alive(alice_eid), true);
1881+
/// ```
18291882
pub fn deserialize<'a, 'de, D: serde::Deserializer<'de>, V: Borrow>(
18301883
&'a self,
18311884
deserializer: D,

src/world.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1777,6 +1777,27 @@ impl core::fmt::Debug for WorldMemoryUsage<'_> {
17771777
#[cfg(feature = "serde1")]
17781778
impl World {
17791779
/// Serializes the view using the provided serializer.
1780+
///
1781+
/// ### Example
1782+
///
1783+
/// ```
1784+
/// use shipyard::{Component, View, World};
1785+
///
1786+
/// #[derive(Component, serde::Serialize)]
1787+
/// struct Name(String);
1788+
///
1789+
/// let mut world = World::new();
1790+
///
1791+
/// let eid1 = world.add_entity(Name("Alice".to_string()));
1792+
///
1793+
/// let mut serialized = Vec::new();
1794+
/// world
1795+
/// .serialize::<_, View<Name>>(&mut serde_json::ser::Serializer::new(&mut serialized))
1796+
/// .unwrap_or_else(|_| panic!());
1797+
///
1798+
/// let serialized_str = String::from_utf8(serialized).unwrap();
1799+
/// assert_eq!(serialized_str, r#"[[{"index":0,"gen":0},"Alice"]]"#);
1800+
/// ```
17801801
pub fn serialize<'w, S: serde::Serializer, V: WorldBorrow>(
17811802
&'w self,
17821803
serializer: S,
@@ -1796,6 +1817,36 @@ impl World {
17961817
}
17971818

17981819
/// Deserializes the view using the provided deserializer.
1820+
///
1821+
/// ### Example
1822+
///
1823+
/// ```
1824+
/// use shipyard::{Component, EntityId, ViewMut, World};
1825+
///
1826+
/// #[derive(Component, serde::Deserialize)]
1827+
/// struct Name(String);
1828+
///
1829+
/// let mut world = World::new();
1830+
///
1831+
/// let mut serialized = r#"[[{"index":0,"gen":0},"Alice"]]"#;
1832+
/// world
1833+
/// .deserialize::<_, ViewMut<Name>>(&mut serde_json::de::Deserializer::from_str(serialized))
1834+
/// .unwrap_or_else(|_| panic!());
1835+
///
1836+
/// let alice_eid = EntityId::new_from_index_and_gen(0, 0);
1837+
/// assert_eq!(world.get::<&Name>(alice_eid).unwrap().0, "Alice");
1838+
///
1839+
/// // Careful here, the World is not in a stable state
1840+
///
1841+
/// assert_eq!(world.is_entity_alive(alice_eid), false);
1842+
///
1843+
/// // We can use World::spawn for example to fix the problem
1844+
/// // another solution would be to serialize EntitiesViewMut
1845+
///
1846+
/// world.spawn(alice_eid);
1847+
///
1848+
/// assert_eq!(world.is_entity_alive(alice_eid), true);
1849+
/// ```
17991850
pub fn deserialize<'w, 'de, D: serde::Deserializer<'de>, V: WorldBorrow>(
18001851
&'w self,
18011852
deserializer: D,

0 commit comments

Comments
 (0)