Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider not deriving serde impls from units types #2612

Closed
tcharding opened this issue Mar 18, 2024 · 12 comments
Closed

Consider not deriving serde impls from units types #2612

tcharding opened this issue Mar 18, 2024 · 12 comments
Milestone

Comments

@tcharding
Copy link
Member

tcharding commented Mar 18, 2024

Types in units are thin wrappers around integer values. If users of these types want to put them in structs they can, and possibly should, stipulate how they want them serialized (eg, amount could be as sats or as BTC).

Shall we remove serde::Deserialize and serde::Serialize impls from units types? Note this does not mean remove the serde module from Amount, just remove #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] from Amount (and from all other units types).

@tcharding tcharding added this to the 0.32.0 milestone Mar 18, 2024
@tcharding tcharding changed the title Consider removing serde feature/dependency from units Consider removing serde impls from units types Mar 18, 2024
@apoelstra
Copy link
Member

I don't understand. Right now we have e.g. the amount::serde module which lets them choose between sats and BTC. We should definitely keep that.

For the locktime types things are less clear (should we have serde on Height/Time or just on LockTime).

@tcharding
Copy link
Member Author

You were too quick. I've updated the issue

@sanket1729
Copy link
Member

Having an implementation is definitely valuable for auto-derive users. I can imagine something like

struct GetTransactionInfo {
    pub reciever: Address, 
    pub fee: Amount,
    pub sent_amount: Amount,
}

requiring an auto-derive.

@tcharding
Copy link
Member Author

tcharding commented Mar 19, 2024

We have that usecase covered already, one can do:

struct GetTransactionInfo {
    ...
    #[serde(with = "bitcoin::amount::serde::as_sat")]
    pub fee: Amount,
    ...
}

I haven't thought it through for other unit types but we want to provide some way to be able to derive them too.

@tcharding
Copy link
Member Author

I just had a play and if we remove the serde imples it is annoying to not be able to do

#[derive(Debug, Serialize, Deserialize)]
struct Foo {
    height: absolute::Height,
    time: absolute::Time,
}

And it is surprising that

#[derive(Debug, Serialize, Deserialize)]
struct Bar {
    lt: absolute::LockTime
}

is fine but using Height and Time is not (if we remove the impls).

Do we gain anything by not allowing Foo to be defined like that? Seems to me like we will just be annoying users.

@sanket1729
Copy link
Member

Amount is probably the most important one. If we have that covered, I am okay dropping the serde requirement from units. Users dealing with Height and Time are advanced users that can do their own serde.

@apoelstra
Copy link
Member

Agreed. I do not think we should change Amount. We spent forever implementing it, many people are using it (including me) and we have covered the multiple natural ways to implement it.

Meanwhile Height and Time have no natural ways to implement serde because they basically just exist to compensate for Rusts' lack of privacy on enum internals, and their serde implementations are confusingly inconsistent with that for LockTime.

I'm fine keeping them, but they are a weird bit of API surface that if we commit to, we're stuck with forever.

You were too quick. I've updated the issue

It still seems to imply that we should get rid of serde entirely in the units crate.

@15IITian
Copy link
Contributor

But It is possible that some users can just use Height struct for simply representing the height of a block -> so will require its serde implementation.

@tcharding tcharding changed the title Consider removing serde impls from units types Consider not deriving serde impls from units types Mar 19, 2024
@tcharding
Copy link
Member Author

It still seems to imply that we should get rid of serde entirely in the units crate.

I re-edited the description, third time lucky.

@tcharding
Copy link
Member Author

But It is possible that some users can just use Height struct for simply representing the height of a block -> so will require its serde implementation.

I agree, absolute::Height seems totally likely to be put into a struct that wants to derive serde traits.

@tcharding
Copy link
Member Author

Closing, this does not seem to be going anywhere. We can re-visit this at a later time if needed.

@tcharding
Copy link
Member Author

Note at this stage we do have serde traits implemented and the bug that started this conversation is fixed by: #2582

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants