changeset 11: |
d8f806f1d327 |
parent: |
315fedf35bc7
|
author: |
ellis <ellis@rwest.io> |
date: |
Sun, 14 May 2023 21:27:04 -0400 |
permissions: |
-rw-r--r-- |
description: |
obj updates |
1 //! obj/src/lib.rs --- Objective type library 2 #![feature(associated_type_bounds)] 4 pub use err::{Error, Result}; 14 use ron::extensions::Extensions; 15 use serde::{de::DeserializeOwned, Deserialize, Serialize}; 17 use std::collections::{BTreeMap, HashMap}; 21 /// Define Object behaviors, implemented by Objects 23 fn encode(&self) -> Result<Vec<u8>> 27 Ok(bincode::serialize(self)?) 30 fn encode_into<W>(&self, writer: W) -> Result<()> 35 Ok(bincode::serialize_into(writer, self)?) 38 fn decode<'a>(bytes: &'a [u8]) -> Result<Self> 40 Self: Deserialize<'a>, 42 Ok(bincode::deserialize(bytes)?) 45 fn decode_from<R>(&self, rdr: R) -> Result<Self> 48 Self: DeserializeOwned, 50 Ok(bincode::deserialize_from(rdr)?) 53 fn to_ron_writer<W>(&self, writer: W) -> Result<()> 58 Ok(ron::ser::to_writer_pretty( 61 ron::ser::PrettyConfig::new() 62 .indentor(" ".to_owned()) 63 .extensions(Extensions::all()), 67 fn to_ron_string(&self) -> Result<String> 71 Ok(ron::ser::to_string_pretty( 73 ron::ser::PrettyConfig::new().indentor(" ".to_owned()), 77 fn from_ron_reader<R>(&self, mut rdr: R) -> Result<Self> 80 Self: DeserializeOwned, 82 let mut bytes = Vec::new(); 83 rdr.read_to_end(&mut bytes)?; 84 Ok(ron::de::from_bytes(&bytes)?) 87 fn from_ron_str<'a>(s: &'a str) -> Result<Self> 89 Self: Deserialize<'a>, 91 Ok(ron::de::from_bytes(s.as_bytes())?) 94 fn to_json_writer<W>(&self, writer: W) -> Result<()> 99 // let formatter = serde_json::ser::PrettyFormatter::with_indent(b" "); 100 Ok(serde_json::ser::to_writer_pretty(writer, &self)?) 103 fn to_json_string(&self) -> Result<String> 107 Ok(serde_json::ser::to_string_pretty(&self)?) 110 fn from_json_reader<R>(&self, mut rdr: R) -> Result<Self> 113 Self: DeserializeOwned, 115 let mut bytes = Vec::new(); 116 rdr.read_to_end(&mut bytes)?; 117 Ok(serde_json::de::from_slice(&bytes)?) 120 fn from_json_str<'a>(s: &'a str) -> Result<Self> 122 Self: Deserialize<'a>, 124 Ok(serde_json::de::from_slice(s.as_bytes())?) 128 impl<T> Objective for Vec<T> {} 129 impl<K, V> Objective for HashMap<K, V> {} 130 impl<K, V> Objective for BTreeMap<K, V> {} 131 impl Objective for std::path::PathBuf {} 132 impl Objective for std::path::Path {} 133 impl Objective for std::string::String {} 134 impl Objective for std::any::TypeId {} 135 impl Objective for u8 {} 136 impl Objective for u16 {} 137 impl Objective for u32 {} 138 impl Objective for u64 {} 139 impl Objective for u128 {} 140 impl Objective for i8 {} 141 impl Objective for i16 {} 142 impl Objective for i32 {} 143 impl Objective for i64 {} 144 impl Objective for i128 {} 145 impl Objective for isize {} 146 impl Objective for usize {} 147 impl Objective for f32 {} 148 impl Objective for f64 {}