Skip to content

Commit

Permalink
add docs
Browse files Browse the repository at this point in the history
  • Loading branch information
nickbabcock committed Nov 18, 2023
1 parent ecfd67d commit 7fb7cd9
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 1 deletion.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,34 @@ let actual: MyStruct = OndemandBinaryDeserializer::builder_flavor(BinaryTestFlav
assert_eq!(actual, MyStruct { field1: "ENG".to_string() });
```

### Direct identifier deserialization with `token` attribute

There may be some performance loss during binary deserialization as
tokens are resolved to strings via a `TokenResolver` and then matched against the
string representations of a struct's fields.

We can fix this issue by directly encoding the expected token value into the struct:

```rust
#[derive(JominiDeserialize, PartialEq, Debug)]
struct MyStruct {
#[jomini(token = 0x2d82)]
field1: String,
}

// Empty token to string resolver
let map = HashMap::<u16, String>::new();

let actual: MyStruct = BinaryDeserializer::builder_flavor(BinaryTestFlavor)
.deserialize_slice(&data[..], &map)?;
assert_eq!(actual, MyStruct { field1: "ENG".to_string() });
```

Couple notes:

- This does not obviate need for the token to string resolver as tokens may be used as values.
- If the `token` attribute is specified on one field on a struct, it must be specified on all fields of that struct.

## Caveats

Caller is responsible for:
Expand Down
1 change: 0 additions & 1 deletion jomini_derive/tests/11-last.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,4 @@ fn test_options() {
assert_eq!(m.checksum, m2.checksum);
assert_eq!(m.human, m2.human);
assert_eq!(m.fourth, m2.fourth);

}
55 changes: 55 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,61 @@ assert_eq!(actual, MyStruct { field1: "ENG".to_string() });
# Ok::<(), Box<dyn std::error::Error>>(())
```
### Direct identifier deserialization with `token` attribute
There may be some performance loss during binary deserialization as
tokens are resolved to strings via a `TokenResolver` and then matched against the
string representations of a struct's fields.
We can fix this issue by directly encoding the expected token value into the struct:
```rust
# #[cfg(feature = "derive")] {
# use jomini::{Encoding, JominiDeserialize, Windows1252Encoding, BinaryDeserializer};
# use std::{borrow::Cow, collections::HashMap};
#
# #[derive(Debug, Default)]
# pub struct BinaryTestFlavor;
#
# impl jomini::binary::BinaryFlavor for BinaryTestFlavor {
# fn visit_f32(&self, data: [u8; 4]) -> f32 {
# f32::from_le_bytes(data)
# }
#
# fn visit_f64(&self, data: [u8; 8]) -> f64 {
# f64::from_le_bytes(data)
# }
# }
#
# impl Encoding for BinaryTestFlavor {
# fn decode<'a>(&self, data: &'a [u8]) -> Cow<'a, str> {
# Windows1252Encoding::decode(data)
# }
# }
#
# let data = [ 0x82, 0x2d, 0x01, 0x00, 0x0f, 0x00, 0x03, 0x00, 0x45, 0x4e, 0x47 ];
#
#[derive(JominiDeserialize, PartialEq, Debug)]
struct MyStruct {
#[jomini(token = 0x2d82)]
field1: String,
}
// Empty token to string resolver
let map = HashMap::<u16, String>::new();
let actual: MyStruct = BinaryDeserializer::builder_flavor(BinaryTestFlavor)
.deserialize_slice(&data[..], &map)?;
assert_eq!(actual, MyStruct { field1: "ENG".to_string() });
# }
# Ok::<(), Box<dyn std::error::Error>>(())
```
Couple notes:
- This does not obviate need for the token to string resolver as tokens may be used as values.
- If the `token` attribute is specified on one field on a struct, it must be specified on all fields of that struct.
## Caveats
Caller is responsible for:
Expand Down

0 comments on commit 7fb7cd9

Please sign in to comment.