Treat empty struct fields differently to a unit#49
Treat empty struct fields differently to a unit#49VictorKoenders merged 2 commits intobincode-org:trunkfrom
Conversation
|
I'm not sure if this is the right way to go. I'm more inclined to use a library like https://docs.rs/non-empty-vec/latest/non_empty_vec/ to make sure the fields always have a value, and Can you come up with a use-case where a developer who is using virtue wants to differentiate between |
Codecov ReportPatch coverage:
Additional details and impacted files@@ Coverage Diff @@
## trunk #49 +/- ##
==========================================
+ Coverage 49.13% 50.07% +0.94%
==========================================
Files 19 19
Lines 1911 1945 +34
==========================================
+ Hits 939 974 +35
+ Misses 972 971 -1
... and 3 files with indirect coverage changes Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. ☔ View full report in Codecov by Sentry. |
|
At the moment I'm using empty-brace syntax for my empty structs, like #[derive(Debug, SSHEncode, SSHDecode)]
pub struct NewKeys {}
// alternative using a unit struct
#[derive(Debug, SSHEncode, SSHDecode)]
pub struct Ignore;From what I can tell with the new Virtue there isn't a way to distinguish between these two cases in the parser. If I want to generate a constructor for them they need different syntax, // generated code
impl<'de> crate::sshwire::SSHDecode<'de> for NewKeys {
fn dec<S: crate::sshwire::SSHSource<'de>>(
s: &mut S,
) -> crate::sshwire::WireResult<Self> {
Ok(Self)
}
} |
|
Oh, I only just realised the empty brace constructor is fine for any of unit/empty-struct/empty-tuple. So perhaps the current behaviour is fine to leave as-is, any struct A;
struct B();
struct C {};
let a = A {};
let b = B {};
let c = C {}; |
|
Thinking about it more I think it's probably beneficial to determine between the delimiter even if there's no fields. There is also an assert here that should be removed. If you can remove that and fix the linting issues I think this is good to merge |
A Struct { } should have Some(Fields::Struct(vec![])) rather than None.
This partially reverts cfb17c0 (bincode-org#45)
New tests for empty struct members, and test Fields::names()
|
It turns out |
A Struct { } should have Some(Fields::Struct(vec![])) rather than None.
This partially reverts cfb17c0 (#45)