Closed v1gnesh closed 3 years ago
What you describe is deserialization, which is more commonly done with the excellent serde crate. A single struct can derive both Builder
and Deserialize
, so you wouldn't need derive_builder
if data
is in a serde-compatible format.
You have three pretty good options, depending on your particular use-case:
serde::Deserialize
to read from data
into Channel
nom
to parse the bytes of data
into Channel
TryFrom<TheTypeThatDataIs> for Channel
All of those are better fits conceptually and practically for this use-case, so it's not something we'll add to derive_builder
.
Why is the
::default()
bit there?
The ChannelBuilder
is a type, not a value. The special_info
method takes a value self
(or &mut self
or &self
, depending on the builder pattern) as its first parameter so that it has a place to store the field value it was just passed. The initial call to default
creates the self
that is then passed to special_info
.
Some people (myself included) like to add a method like this:
impl Channel {
pub fn builder() -> ChannelBuilder {
ChannelBuilder::default()
}
}
That way, someone can use my_crate::Channel;
and make a value of the builder type without an additional import.
Hi,
Using the below example code as reference
Can a struct field have a type of
enum
, in addition to supporting numbers or strings?Can the builder be updated to allow reading/consuming from a slice input
&mut R
whereR: BufRead
or&mut &[u8]
Rather than specifying values directly in the setter functions, can support be added to do something like:
where the field type of
special_info
etc., from thestruct
definition will decide how many bytes to read. Also, can there be a way to avoid repeateddata.read_be()?
as input parms to the setter functions?read_be()
is a reference from https://crates.io/crates/eioAnother possibly dumb question: Why is the
::default()
bit there? Is it just to allow adding our owndefault
functions?Can this
default
function be used to support the above use case of reading from a mutable input slice? Allowing non-hard-coded setter parms, so that they're set from the input slice/source that's being parsed.