Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
|
|
@ -0,0 +1,2 @@
|
|||
serde = { version = "1.0.89", features = ["derive"] }
|
||||
bincode = "1.1.2"
|
||||
48
Task/Object-serialization/Rust/object-serialization-2.rust
Normal file
48
Task/Object-serialization/Rust/object-serialization-2.rust
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
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(())
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
serde = { version = "1.0.89", features = ["derive"] }
|
||||
bincode = "1.1.2"
|
||||
typetag = "0.1.1"
|
||||
86
Task/Object-serialization/Rust/object-serialization-4.rust
Normal file
86
Task/Object-serialization/Rust/object-serialization-4.rust
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
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(())
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue