Releases: stephenberry/structio
Release list
structio 0.6.0
Two new declarations: a field or a variant that answers to more than one name, and a one-field struct written as that field alone. Plus ReadOwned, the bound for a read out of a buffer you own, and a json::Raw that can be built from a String you already hold. No breaking changes.
[dependencies]
structio = "0.6"
# The derive is off by default:
structio = { version = "0.6", features = ["derive"] }Added
-
A field or a variant may answer to more than one name.
object!(Settings { timeout | "timeout_ms" })andtagged_enum!(Mode { Idle | "idle" }), repeatable, with#[structio(alias = "..")]as the derive's spelling. The declared name is the one written and an alias is only ever read, so adding one cannot change a byte the program writes: it is how a key or a variant is renamed without breaking the documents already written under the old spelling. A case rule leaves an alias alone, as it leaves an explicit key alone; a#[required]member is satisfied by any of its names; awrite_onlydeclaration refuses one, never reading.Keys::ALIASESandVariants::ALIASESare the new associated consts that carry it, both defaulted to empty, so a hand-written impl is unchanged. docs/schemas.md has the rest. -
transparent!, a one-field struct written as that field alone.UserId(7)is7, not[7]and not{"0":7}: reading and writing delegate to the field, with no object, no keys and no array around it. That is the declaration for the newtype that exists to keep twou64s apart in Rust and means nothing to either format, whicharray!writes as a one-element array and an object cannot write at all, having no name for its key to be.json_transparent!andbeve_transparent!narrow it to one format andwrite_onlyto one direction; it takes an adapter,transparent!(Timeout { 0 as Millis }), which is the whole newtype-around-a-foreign-type case in one line;#[structio(transparent)]is the derive's spelling.is_nullis forwarded in both formats, and BEVE's typed-array path deliberately is not, that one being a layout claim a declaration cannot make. docs/schemas.md has the rest. -
ReadOwned, the bound for parsing a value out of a buffer you own. At the crate root and per format, asReadWrite's read-side counterpart. A function that holds the document and hands aTback needsfor<'de> Read<'de> + Default: higher-ranked because a type may borrow out of the input and a caller owning the buffer cannot allow that, andDefaultbecause reading fills a value rather than constructing one. The crate spelled that pair out in ten of its own signatures, and a downstream extractor had to work it out from scratch;ReadOwnedis the name for it.from_strstill takesRead<'de>, tied to the input's lifetime so a borrowing type can be read, exactly as serde keepsDeserializeOwnedtofrom_reader. docs/schemas.md has the rest. -
json::Raw::from_stringandfrom_string_unchecked, the way in from aString. Text this program produced rather than read had no entry point:Raw::new_unchecked(&text).into_owned()copied a buffer the caller already owned, there being no way to hand theStringover. These take it, so the buffer becomes the span with nothing reallocated and the span never copied out of it,from_stringtrimming by shifting bytes inside it. The check and the trimming arenew's, being the same walk. TheRawthen holds the caller's whole allocation rather than just the span, sonew(&s)?.into_owned()is still the call for a long-lived value whittled out of a much larger buffer. A rejected value is dropped rather than handed back, so check withnewfirst where the text has to survive its own rejection. There is still noFrom<String>, for the reason there is noFrom<&str>. -
Displayforjson::Rawandjson::JsonStr.Rawdisplays its span, which is whatas_strgives and what a compact write emits, escapes and quotes included, since the type is about the spelling.{:#}is that same text rather than a laid-out one: a spannew_uncheckedaccepted may have no layout, andDisplayhas nowhere to report that, soprettifystays the named way to ask.JsonStrdisplays the string the document meant, with its escapes already resolved. -
Debug,Clone,PartialEq,EqandHashforjson::JsonStr. It had none, so a test could notassert_eq!on one or print one, and the keyError::key_inhands back could not be looked up in a set of the names a schema knows. Equality and hashing are the text rather than the variant: a key written"a"and one written"\u0061"are the same key, which deriving either would have denied.
Fixed
ReadWrite's doc example was missing+ Defaultand could not compile. It was markedignore, so nothing caught it. Now compiled, and the same example in theobject!docs already had the bound.Write's example, the crate's only otherignore, is compiled too.
structio 0.5.0
Ordered objects, JSON carried through as its text, a write-only declaration, and Value comparisons that cover the whole table. Two breaking changes, both narrow: Value::Object is an OrderedMap<Value>, and the new comparison impls can make a bare .into() ambiguous.
[dependencies]
structio = "0.5"
# The derive is off by default:
structio = { version = "0.5", features = ["derive"] }Changed
-
Value::Objectkeeps its member order. It is anOrderedMap<Value>rather than aBTreeMap<String, Value>, so a document read into aValueand written back out lists its members in the order it arrived in, andto_valueyields a declared type's field order. Equality is unchanged in meaning and ignores order, so two values can compare equal and write different text.sort_keys()on the object gives back the sorted output. Breaking for code namingBTreeMapwherestructio::Objectis expected, or relying on sorted output. -
transparentis stage 2 of the derive, not stage 3. It describes the whole type rather than one field: stage 2 is for a shape the macros cannot declare, stage 3 for per-field policy. It is not implemented and the derive still refuses it, now naming stage 2.
Added
-
A
Valuecompares with the primitive it holds.doc["port"] == 8080and8080 == doc.get("port").unwrap()are comparisons now rather than avalue!(8080)to wrap the right-hand side in: every integer width,f32,f64,bool,str,&str,Stringand&String, on either side, with the value owned or behind either reference the accessors hand back. Onlystr,&strandboolhad an impl before, and only against an owned value, so most of the table was a compile error and which part was a matter of luck. A number is met at the comparand's width:value!(1) == 1.0andvalue!(1.0) != 1, being the different numbers this crate keeps them, and against anf32the stored number rounds to that width, so a document's0.1equals0.1f32while one too large or too small to round to anf32equals none. Breaking for a caller whose right-hand type was pinned by there being onePartialEqimpl within reach: withn: u64,n == w.into()is now ambiguous betweenu64andValueand needs the type named. -
Error::key_in, the name an unknown key had.UnknownKeyandUnknownVariantcarry no name, a name the document chose being no&'static str, so the error winds back to it and reports the offset. This reads it back out of the document at that offset, unescaping as the reader would, and hands aMissingKeythe static name it already carries. The lifetime is the document argument's, not the error's, soErrorstaysCopyand independent of the buffer. JSON only: a BEVE key's length lives in a prefix the offset is already past. -
read_map_located, onjson::Parserandbeve::Reader.read_mapcalls back after the colon, so a hand-written map reader could see a key's name but not its position, and hand-rolling the loop was no way out either, the depth counter that bounds nesting not being public. This reports the offset alongside the key: the same byte a generated reader'sUnknownKeynames, so an error raised by hand reads like one the crate raised. -
structio::OrderedMap<V>, a string-keyed map that keeps its insertion order. Entries live in one vector in the order they arrived; below nine keys a lookup scans it, above that a robin hood hash table indexes it. It reads and writes in both formats, so it works as a whole document or as a declared field whereBTreeMapwould, andObjectisOrderedMap<Value>. Equality ignores order;sort_keys()reorders by key. -
write_only, a declaration of the write half alone.object!(write_only ..), and the same token in front ofarray!,unit_enum!,tagged_enum!and the one-format macros, generate the writing impls and no read at all, so a field's type needs noReadimpl and noDefault.#[structio(write_only)]is the derive's spelling of it, and the bytes written are unchanged either way. A generic one bounds its type parameters by the newstructio::Writerather than bystructio::ReadWriteandDefault.#[required]is refused, being a rule about reading, and there is noread_only. docs/schemas.md has the rest. -
A failed
ReadorWritebound now explains the direction axis.Read,Write,ReadAsandWriteAs, in both formats, carry#[diagnostic::on_unimplemented]notes naming what discharges the bound:write_onlywhere the struct is only ever written, askip_value()stub where one field is, and on the write sidewrite_null()withis_nullreturningtrue, since a member that writes nothing truncates the object. -
json::Raw, one JSON value carried through as its text. A field that captures the exact bytes of a value on read and emits them unchanged on write, so a forwarded body keeps its key order, its number spellings and its escapes: whatValueis not, being a tree that respells its numbers and decodes its escapes. Reading borrows the span out of the document, and underALLOW_COMMENTSstrips the comments out of a span that carries any, owning that one; writing is one copy of those bytes, laid out again at the right depth underPRETTY. JSON only, so a struct with one is declared withjson_object!. docs/schemas.md has the rest. -
json::prettify_value_into. Lays one JSON value out into aWriterthat is already part-way through a document, at that writer's current depth and under its policy.json::Rawwrites through it underPRETTY, and it is what a passthrough type of your own needs so that a forwarded value is indented against its neighbours rather than emitted as a blob. -
Tuple structs, in
array!and in the derive. A positional declaration names a field by its position,array!(Entry [0, 1]), so the shape that has no field names is now the shape it takes most naturally:#[derive(Structio)]with#[structio(array)]accepts a tuple struct, with the order,.., an element type and generics all as they are for a named struct. The bytes are the tuple's, as they always were. Declared as an object it is still refused, an object having nothing for its keys to be, and the message now names#[structio(array)]. -
Writer::member_key, for an object key known only at run time.membertakes the key already prepared, quoted with its colon in JSON and length-prefixed in BEVE, because that is what a declaration assembles at compile time. A hand-writtenWriteObjectwhose keys come off a walk had to build those bytes itself, and it went wrong differently in each format: nothing escapes a JSON key on that path, so a key holding a"or a\wrote a document no reader takes, and a BEVE member laid down withsizeandrawis not counted, which a debug build catches against the header the object already committed to. This takes the key itself, escapes or length-prefixes it, counts the member, and honoursSkipNullexactly asmemberdoes;member_key_withis the adapter form. The policy's boundary is a struct's member against a map's entry, not a compile-time key against a computed one, andwrite_keyedis still the map. docs/schemas.md has the rest. -
json::Parser::rest_str. The&strcounterpart ofrest, for a hand-writtenReadimpl capturing a span: the input's UTF-8 validity is already known, so nothing has to establish it a second time.
Fixed
-
A
Matrixnames the unknown key it refused. It reads its three members by hand through a map callback, which runs after the colon, so itsUnknownKeyreported the offending member's value rather than its key: a caret under the value, andkey_inreading a name off it. It winds back to the key, so everyUnknownKeyin the crate now names a key. The reported offset for this one code on this one type moves. -
The whole-key hash now folds in the key length. It read whole 8-byte chunks and then an overlapping tail, so two keys differing only in the bytes between them, such as
field_name_10500_valueandfield_name_105000_value, hashed the same under every seed and cost the object its hash: it read under a linear scan instead. Keys shorter than 8 bytes were zero filled, so trailing NULs vanished the same way. Never a wrong field, since a candidate is always confirmed by a full comparison, only a slower read. -
A declared type does not need
Default. docs/derive.md said it did, flatly, contradicting docs/schemas.md.Defaultis required where a read constructs a value: the entry points that return one, anOption's payload, a growingVec's tail, a map's values, an enum variant's payload. A type that is only ever written needs none, and the derive's examples no longer imply otherwise. -
Where an error out of a declaration lands. The same file promised that a field whose type has no
Readimpl is reported at that field. One macro call covers every field, so it is reported at the declaration: the struct's name under the derive, the whole invocation under a hand-written one. What does land where it was written is the derive's own refusals and an adapter named bywith = "..". -
The same rule, in the README. It read as though reaching for
read_intolifted theDefaultrequirement. It lifts it for the value handed in and for nothing beneath it, soread_into(&mut Vec<T>, ..)still asksTfor one and no spelling of the read avoids it;Box<T>and[T; N]do escape it, having no element to bui...
structio 0.4.0
#[derive(Structio)] behind an optional feature, a Value tree, and a late enum tag. One breaking change: a raw identifier's r# is no longer part of its key.
[dependencies]
structio = "0.4"
# The derive is off by default:
structio = { version = "0.4", features = ["derive"] }Changed
-
A raw identifier's
r#is no longer part of its key. A field or variant writtenr#typehad the keyr#type, because that is whatstringify!hands the macro. It is nowtype, before any case rule runs, since the prefix is how Rust spells a name that collides with a keyword rather than part of the name:r#typeis how you write a field for a"type"key. Both formats, fields and variants, derived and declared. An explicit"r#type" => fieldis a literal and is unchanged. Breaking for a declaration with a raw identifier and no explicit key, which now reads and writes a different key. -
An internally tagged enum's tag no longer has to come first.
tagged_enum!(.. as tag "kind")used to refuse an object whose first member was not the tag withExpectedTag, which refused every document from a sorted-key writer the moment a member sorted before the tag. The reader now steps over the members before the tag, dispatches on it, reads the members after it, and then reads the ones it stepped over, nesting as deep as the payloads do. A tag that is first still costs one pass; the members before a late tag are walked twice, and a key on both sides of the tag keeps its earlier value. Required-field and unknown-key rules apply to the deferred members as to any other. An object with no tag at all is stillExpectedTag, reported against its first key.
Added
#[derive(Structio)], behind thederivefeature. A front end toobject!,array!,unit_enum!andtagged_enum!: it reads the type and emits the declaration, so a derived type and a declared type are the same impls.rename_all,tag,array,element,json,beveandcrateon the type;rename,skip,requiredandwithon a field;renameon a variant. Generics and their bounds are read off the type. The feature is off by default and the derive crate has no dependencies. docs/derive.md has the rest, including what later stages add.- BEVE containers reserve on the wire count.
Reader::read_seq_countedandread_map_countedhand the element count to the caller before the first element, clipped to what the input could hold, andbeve::cautious::<T>clips it again to a megabyte ofT.Vec,VecDeque,HashMapandHashSet, adapted or not, reserve once instead of doubling up; a hostile count can waste at most that megabyte. Value, a tree for a value with no declared type. Null, bool, number, string, array, object, withget,pointer/pointer_mut, theas_*/is_*accessors,Index/IndexMutby key or position, and thevalue!macro to build one. It reads and writes through both formats like any other type, so it can be a field of anobject!declaration or a whole document; a BEVE typed array, complex run or matrix reads into the same shapebeve_to_jsonwrites.Numberkeeps whether it was an unsigned integer, a negative integer or a float, and writes a whole-valued float as1.0so the kind survives a trip through text.to_valueandfrom_valuemove a declared type in and out, through JSON text. This is for the value nothing decodes, a register tree walked by path or a body forwarded unread, not a substitute for a declared type, and the crate's stance on that is unchanged.
Upgrading
The break is narrow. A declaration with a raw identifier and no explicit key changes the key it reads and writes: a field written r#type was keyed r#type and is now keyed type. Nothing else about a 0.3.2 declaration changes its bytes. If you were working around the old behaviour with "r#type" => r#type, that explicit key still means exactly what it says and will keep the old wire format.
The derive feature is additive and off by default, so a crate that does not enable it builds structio with no dependencies and no proc-macro, as before.
structio 0.3.2
Faster JSON reading and string writing. No API or output change.
[dependencies]
structio = "0.3"Changed
- Faster JSON reading and string writing. Against Glaze on the benchmark documents, reading doubles went from 58% to 87% of its speed, mixed documents from 78% to over 100%, and signed integers, bools and strings moved up with them; string writing went from 91% to 94%. The float reader is now inlined into the array loop rather than called per element, out-of-line helpers no longer pin the parser's cursor to the stack, signs and bools are read without a branch on the data, digits are folded a word at a time rather than one at a time, integers of up to fifteen digits stay on the inlined path, and strings are copied as they are scanned. docs/performance.md has the measurements and the mechanisms.
Output and accepted input are unchanged, and the float scanner is checked bit for bit against the standard library on 200,000 generated literals. A 0.3.1 declaration reads and writes exactly the bytes it did.
structio 0.3.1
Borrowing declarations accept any lifetime name, and the readers' nesting limit is exported.
[dependencies]
structio = "0.3"Changed
- A borrowing type names its lifetime as it likes.
object!(['a] Borrowed<'a> { .. })now works, as doarray!,tagged_enum!and their single-format forms: the first lifetime in the bracket is the input lifetime, whatever it is called. It had to be spelled'de, and any other name failed from inside the expansion with "lifetime may not live long enough" and no hint about why. Declarations written with'deare unchanged.
Added
json::MAX_DEPTHandbeve::MAX_DEPTH, the nesting limit each reader enforces, re-exported at the module root. They were reachable only throughjson::parserandbeve::reader.
Nothing existing changed shape; a 0.3.0 declaration reads and writes exactly the bytes it did.
structio 0.3.0
Internally tagged enums: the variant name goes inside the payload's object, the convention most JSON APIs use.
[dependencies]
structio = "0.3"Added
-
Internal tagging, a second convention for
tagged_enum!, asked for with a tag clause:tagged_enum!(Shape as tag "kind" { .. }). The variant name goes inside the payload's object as a member rather than wrapping it, giving{"kind":"Circle","radius":1}where the clause-free form writes{"Circle":{"radius":1}}. This is what most JSON APIs use, and the only form here that a C++ Glazestd::variantcan be made to agree with, external tagging having nowhere to put the payload's own keys. The clause works onjson_tagged_enum!andbeve_tagged_enum!too.The tag has to be the object's first member, and a document that puts it elsewhere is the new
ErrorCode::ExpectedTag, reported against the offending key. Reading is one pass with no lookahead, so a tag arriving after the members it gives meaning to could only be used by holding the object or walking it twice. Writing always emits the tag first, so this crate's own output round-trips unconditionally, as does any producer that emits its tag first - the conventional ordering. The refusal is loud and positioned rather than a misparse.A payload must be an object (a compile error naming
WriteObjectotherwise), since its members share the object with the tag. Everything else carries over: renaming, case rules, generics, borrowed payloads, reading into an existing value, and the policies. The result is an ordinary object, so pointers, validation and transcoding walk it with no knowledge of enums at all. See docs/enums.md. -
A tag that is also a field of a variant's payload is a compile error. The two share one object, so it would write the name twice; structio reads that back and a last-wins parser does not, keeping the field and losing the variant. The comparison is of wire names, so a collision that only appears after a case rule is caught too.
cargo checkrefuses a declaration with no generics; a generic one is refused when the crate is built, a generic payload having no keys until it is instantiated. -
Parser::read_object_restandParser::finish_internally_tagged, theirReadercounterparts, andWriter::write_internally_taggedin both formats, for hand-written impls of the two newReadInternallyTaggedtraits. A variant carrying nothing writes through the existingwrite_tagged, the bytes being the same object of one member.
Nothing existing changed shape: an externally tagged declaration reads and writes exactly the bytes it did in 0.2.2. The minor bump is for the added ErrorCode variant.
structio 0.2.2
Integer reading is 38-43% faster, and a streaming block read now accepts a complex array.
[dependencies]
structio = "0.2"Changed
- Reading arrays of integers is 38-43% faster, and the representative
mixeddocument 11%. The element loop was making a function call per element, which is dearer than it sounds: the parser's cursor spilled to the stack and reloaded on every return.parse_u64now keeps a small fast path for the common short number and hands the rare cases to an out-of-line one, which is enough for the whole read to inline. Separately, JSON whitespace is answered from a table rather than a bitmask, which needed a range guard in front of it because a byte of 64 or more would shift out of the word. Against Glaze, readinguintswent from 50% to 80% andintsfrom 53% to 86%. The measurements, the two rewrites of the digit conversion that were tried first and were both slower, and the disassembly that pointed at the call rather than the digits are in docs/performance.md. No API or output change.
Fixed
-
read_array_intoandfrom_reader_arrayread a complex array. They read the typed-array tag and stopped at the extension's, so the one shape that most needs a streaming block read - a buffer of IQ samples, which a consumer can least afford to hold twice - had to go throughfrom_readerand hold the encoded document alongside the vector. The payload was always a block: interleaved(re, im)components are the in-memory form of[Complex<T>]for the same reason a typed array's payload is the in-memory form of[T]. Only the preamble differed.The aligned complex form is byte-identical to the plain one, so both arrive by the same path.
COMPLEX_ONEis refused asInvalidHeader, being a lone value with no count rather than an array, as are the six undefined forms of the class byte. A generic array staysExpectedArrayand a boolean or string array staysElementTypeMismatch. -
The big-endian conversion in the same read reverses each component rather than each element. It could not have fired before, no complex array having reached it, but a
Complex<f32>is eight bytes and reversing all eight would have transposedreandimas well as swapping the bytes of each. For every other numeric type the component is the element, so one stride serves both.
No API was added, removed or changed. Nothing needs editing to move from 0.2.1.
structio 0.2.1
A read size that sizes the window, so a small document does not cost a 64 KiB buffer, and a hazard on read_seq written down.
[dependencies]
structio = "0.2"Changed
Documents::read_sizesizes the window as well as the read. The buffer is allocated on the first fill and holds one chunk, soDocuments::array(bytes).read_size(4096)costs 4 KiB rather than the 64 KiB it allocated up front before, whatever the read size said. That matters where streaming is not the point: a document already in memory can still be worth reading throughDocuments, which is what hands out the elements of a typed array one at a time with their headers installed, and there the default window was a thousand times the document. Applies to bothjson::Documentsandbeve::Documents;Feedis unchanged, having no chunk size to go by.beve::Reader::read_seqdocuments that element positions do not bound documents. A typed array's element headers, a complex array's, and a boolean run's are supplied by the reader rather than present in the input, so a span cut between twoposition()calls is not a valueReader::newcan read. A caller slicing spans out of a walk has to check the array's header type first;Documents::arrayis the way to take elements as documents of their own, and accepts every array shape.
No API was added, removed or changed. Nothing needs editing to move from 0.2.0.
structio 0.2.0
Writing a document after what a buffer already holds, in JSON as well as BEVE, and a buffer that survives a panic out of the value being written.
[dependencies]
structio = "0.2"Added
json::append(&T, &mut Vec<u8>)writes a document after what a buffer already holds, the counterpart ofbeve::append.write_intoreplaces a buffer's contents, so a value that has to sit behind a protocol header, or behind the entries already in a listing, needed a second buffer and a copy out of it.json::Writer::appendingis the same thing with the writer in hand.
Changed
json::append,beve::appendandbeve::append_alignedleave the buffer exactly as they found it if writing the value panics. The buffer moves into the writer, so an unwind used to drop it along with the bytes in front of the document. AWriteimpl may panic by design: an adapter whose target has values it cannot encode is told to substitute or panic.write_intostill leaves its buffer empty there, its contents being the call's to replace, and now says so.json::Writer::into_stringchecks the bytes handed toWriter::appending, and panics if they are not UTF-8. Every other byte in the buffer is UTF-8 by construction; those are the only ones the writer did not produce. Useinto_vecto append JSON behind a binary prefix.
The full list is in CHANGELOG.md.
structio 0.1.0
First release. JSON and BEVE read straight into your structs and written straight out of them: no Value enum, no token stream, no document model.
[dependencies]
structio = "0.1"- JSON and BEVE from one schema.
object!,array!,unit_enum!andtagged_enum!declare a type's fields once; both formats read and write against that declaration. Keys are hashed at compile time into a perfect hash chosen to fit the key set. - No dependencies and no proc-macros. Standard library only. Rust 2024 edition, MSRV 1.96.
- Declarations are checked against the type. Leaving out a field, or naming the same key twice, is a compile error that names what is wrong. End a declaration with
..where the omission is deliberate. - Case rules.
object!(Root as "camelCase" { .. })converts every key the declaration does not spell out, during compilation. - Reads reuse allocations.
read_intoandwrite_intorefill the buffers a value already holds, so a loop over records of one shape settles into no allocation. - Compile-time options. Indentation, inline arrays, skipping null members, refusing unknown keys, requiring declared keys, and JSONC comments. Unused settings cost nothing.
- BEVE beyond whole-document decoding. Read the one value a JSON Pointer names, validate without decoding, write numeric arrays a reader can borrow rather than copy.
- Streaming in both formats, both directions, and
beve_to_jsonrewrites a BEVE document as JSON in one walk with no schema and no tree. - Errors locate themselves. A byte offset, a line/column/caret rendering against the input, and a named key for a missing one.
The API is not frozen before 1.0. Full notes in CHANGELOG.md, documentation in docs/.