Object serialization: Difference between revisions
Content added Content deleted
(Added Rust) |
|||
Line 2,184: | Line 2,184: | ||
) |
) |
||
puts "END LOADED DIVERSE COLLECTION"</lang> |
puts "END LOADED DIVERSE COLLECTION"</lang> |
||
=={{header|Rust}}== |
|||
Rust does not have inheritance, but this can be done with either enums or traits. |
|||
=== Enum === |
|||
Dependencies: |
|||
<lang TOML>serde = { version = "1.0.89", features = ["derive"] } |
|||
bincode = "1.1.2"</lang> |
|||
<lang Rust>use std::fmt; |
|||
use bincode::{deserialize, serialize}; |
|||
use serde::{Deserialize, Serialize}; |
|||
#[derive(Debug, Serialize, Deserialize)] |
|||
enum Animal { |
|||
Dog { name: String, color: String }, |
|||
Bird { name: String, wingspan: u8 }, |
|||
} |
|||
impl fmt::Display for Animal { |
|||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
|||
match self { |
|||
Animal::Dog { name, color } => write!(f, "{} is a dog with {} fur", name, color), |
|||
Animal::Bird { name, wingspan } => { |
|||
write!(f, "{} is a bird with a wingspan of {}", name, wingspan) |
|||
} |
|||
} |
|||
} |
|||
} |
|||
fn main() -> bincode::Result<()> { |
|||
let animals = vec![ |
|||
Animal::Dog { |
|||
name: "Rover".into(), |
|||
color: "brown".into(), |
|||
}, |
|||
Animal::Bird { |
|||
name: "Tweety".into(), |
|||
wingspan: 3, |
|||
}, |
|||
]; |
|||
for animal in &animals { |
|||
println!("{}", animal); |
|||
} |
|||
let serialized = serialize(&animals)?; |
|||
println!("Serialized into {} bytes", serialized.len()); |
|||
let deserialized: Vec<Animal> = deserialize(&serialized)?; |
|||
println!("{:#?}", deserialized); |
|||
Ok(()) |
|||
}</lang> |
|||
{{out}} |
|||
<pre>Rover is a dog with brown fur |
|||
Tweety is a bird with a wingspan of 3 |
|||
Serialized into 57 bytes |
|||
[ |
|||
Dog { |
|||
name: "Rover", |
|||
color: "brown" |
|||
}, |
|||
Bird { |
|||
name: "Tweety", |
|||
wingspan: 3 |
|||
} |
|||
]</pre> |
|||
=== Trait === |
|||
Dependencies: |
|||
<lang TOML>serde = { version = "1.0.89", features = ["derive"] } |
|||
bincode = "1.1.2" |
|||
typetag = "0.1.1"</lang> |
|||
<lang Rust>use std::fmt::{self, Debug, Display}; |
|||
use bincode::{deserialize, serialize}; |
|||
use serde::{Deserialize, Serialize}; |
|||
#[typetag::serde(tag = "animal")] |
|||
trait Animal: Display + Debug { |
|||
fn name(&self) -> Option<&str>; |
|||
fn feet(&self) -> u32; |
|||
} |
|||
#[derive(Debug, Deserialize, Serialize)] |
|||
struct Dog { |
|||
name: String, |
|||
color: String, |
|||
} |
|||
impl Display for Dog { |
|||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
|||
write!(f, "{} is a dog with {} fur", self.name, self.color) |
|||
} |
|||
} |
|||
#[typetag::serde] |
|||
impl Animal for Dog { |
|||
fn name(&self) -> Option<&str> { |
|||
Some(&self.name) |
|||
} |
|||
fn feet(&self) -> u32 { |
|||
4 |
|||
} |
|||
} |
|||
#[derive(Debug, Deserialize, Serialize)] |
|||
struct Bird { |
|||
name: String, |
|||
wingspan: u32, |
|||
} |
|||
impl Display for Bird { |
|||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
|||
write!( |
|||
f, |
|||
"{} is a bird with a wingspan of {}", |
|||
self.name, self.wingspan |
|||
) |
|||
} |
|||
} |
|||
#[typetag::serde] |
|||
impl Animal for Bird { |
|||
fn name(&self) -> Option<&str> { |
|||
Some(&self.name) |
|||
} |
|||
fn feet(&self) -> u32 { |
|||
2 |
|||
} |
|||
} |
|||
fn main() -> bincode::Result<()> { |
|||
let animals: Vec<Box<dyn Animal>> = vec![ |
|||
Box::new(Dog { |
|||
name: "Rover".into(), |
|||
color: "brown".into(), |
|||
}), |
|||
Box::new(Bird { |
|||
name: "Tweety".into(), |
|||
wingspan: 3, |
|||
}), |
|||
]; |
|||
for animal in &animals { |
|||
println!("{}", animal); |
|||
} |
|||
let serialized = serialize(&animals)?; |
|||
println!("Serialized into {} bytes", serialized.len()); |
|||
let deserialized: Vec<Box<dyn Animal>> = deserialize(&serialized)?; |
|||
println!("{:#?}", deserialized); |
|||
Ok(()) |
|||
}</lang> |
|||
{{out}} |
|||
<pre>Rover is a dog with brown fur |
|||
Tweety is a bird with a wingspan of 3 |
|||
Serialized into 172 bytes |
|||
[ |
|||
Dog { |
|||
name: "Rover", |
|||
color: "brown" |
|||
}, |
|||
Bird { |
|||
name: "Tweety", |
|||
wingspan: 3 |
|||
} |
|||
]</pre> |
|||
=={{header|Tcl}}== |
=={{header|Tcl}}== |