Skip to content
This repository was archived by the owner on Dec 17, 2025. It is now read-only.

Treat empty struct fields differently to a unit#49

Merged
VictorKoenders merged 2 commits intobincode-org:trunkfrom
mkj:empty-fields
Apr 7, 2023
Merged

Treat empty struct fields differently to a unit#49
VictorKoenders merged 2 commits intobincode-org:trunkfrom
mkj:empty-fields

Conversation

@mkj
Copy link
Contributor

@mkj mkj commented Mar 30, 2023

A Struct { } should have Some(Fields::Struct(vec![])) rather than None.

This partially reverts cfb17c0 (#45)

@VictorKoenders
Copy link
Contributor

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 None == struct but 0 fields == enum but 0 fields

Can you come up with a use-case where a developer who is using virtue wants to differentiate between struct Foo;, struct Foo { } and struct Foo();?

@codecov
Copy link

codecov bot commented Mar 30, 2023

Codecov Report

Patch coverage: 94.28% and project coverage change: +0.94 🎉

Comparison is base (eb18f25) 49.13% compared to head (930d8d6) 50.07%.

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     
Impacted Files Coverage Δ
src/parse/body.rs 78.41% <93.33%> (+5.28%) ⬆️
src/parse/generics.rs 71.10% <100.00%> (-0.14%) ⬇️

... 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.
📢 Do you have feedback about the report comment? Let us know in this issue.

@mkj
Copy link
Contributor Author

mkj commented Mar 31, 2023

At the moment I'm using empty-brace syntax for my empty structs, like NewKeys below. No particular reason other than consistency with my non-empty structs (they're a set of packet formats).

#[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, Self {} vs Self. I could just change all of them to be unit structs, but it seems Virtue should be able to express either?

// 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)
    }
}
52 | #[derive(Debug, SSHEncode, SSHDecode)]
   |                            ^^^^^^^^^ help: use curly brackets: `Self { /* fields */ }`

@mkj
Copy link
Contributor Author

mkj commented Mar 31, 2023

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 None fields variant can use Self {}.

    struct A;
    struct B();
    struct C {};
    let a = A {};
    let b = B {};
    let c = C {};

@VictorKoenders
Copy link
Contributor

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

mkj added 2 commits April 5, 2023 22:25
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()
@mkj
Copy link
Contributor Author

mkj commented Apr 5, 2023

It turns out .names() wasn't getting called at all in tests so I've added that too. Thanks!

Copy link
Contributor

@VictorKoenders VictorKoenders left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

@VictorKoenders VictorKoenders merged commit 789c14a into bincode-org:trunk Apr 7, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants