Commit Graph

68 Commits

Author SHA1 Message Date
Martin Habovstiak 64f7d2549e Fix wrong newtype APIs
This fixes several API bugs:

* Use `TryFrom` instead of `From` for fallible conversions
* Move byte conversion methods from `impl_array_newtype` to
  `impl_bytes_newtype`
* Add missing trait impls like `AsRef`, `Borrow`, their mutable versions
  and infallible conversions from arrays

Closes #1336
2022-12-23 01:28:17 +01:00
Martin Habovstiak 3e520f9094 Use hex from internals rather than hashes
`bitcoin-internals` contains a more performant implementation of hex
encoding than what `bitcoin_hashes` uses internally. This switches the
implementations for formatting trait implementations as a step towards
moving over completely.

The public macros are also changed to delegate to inner type which is
technically a breaking change but we will break the API anyway and the
consuers should only call the macro on the actual hash newtypes where
the inner types already have the appropriate implementations.

Apart from removing reliance on internal hex from public API this
reduces duplicated code generated and compiled. E.g. if you created 10
hash newtypes of SHA256 the formatting implementation would be
instantiated 11 times despite being the same.

To do all this some other changes were required to the hex
infrastructure. Mainly modifying `put_bytes` to accept iterator (so that
`iter().rev()` can be used) and adding a new `DisplayArray` type. The
iterator idea was invented by Tobin C. Harding, this commit just adds a
bound check and generalizes over `u8` and `&u8` returning iterators.

While it may seem that `DisplayByteSlice` would suffice it'd create and
initialize a large array even for small arrays wasting performance.
Knowing the exact length `DisplayArray` fixes this.

Another part of refactoring is changing from returning `impl Display` to
return `impl LowerHex + UpperHex`. This makes selecting casing less
annoying since the consumer no longer needs to import `Case` without
cluttering the API with convenience methods.
2022-12-22 10:44:27 +01:00
Andrew Poelstra 1b15a13e5a
run cargo clippy and fmt 2022-12-13 14:52:43 +00:00
Tobin C. Harding fb708ca74b Add newline after brief description
rustdocs should contain a newline to separate the brief description from
the rest of long description.
2022-12-06 12:25:04 +11:00
Martin Habovstiak a6ecc58a5e Add `put_bytes_min` and `space_remaining` methods
Pnicking on oversized slice is useful to catch errors in code that's
supposed to know the exact sizes but this is undesirable in code that
doesn't. These two methods help with handling the case when `buf.len()`
is not known upfront.
2022-12-02 10:48:05 +01:00
Tobin C. Harding 580feab3f9 internals: Add CHANGELOG file
The changelog file is a per crate, per release, thing. Add one to the
`internals` crate.
2022-11-08 08:40:44 +11:00
Tobin C. Harding 8c4fa5ec3d Remove into_bytes from impl_array_newtype
All the types that we define with `impl_array_newtype` are
`Copy` so the correct conversion method to get the underlying byte array
is `to_bytes`. We currently provide `into_bytes` as well as `to_bytes`,
with one of them calling `clone` - this is unnecessary and against
convention.

- Remove `into_bytes` and for `to_bytes` just return the inner field.
- Add a method that causes build to fail if `Copy` is not implemented.
2022-11-05 06:57:40 +11:00
Tobin C. Harding 53b681b838 Move const_assert to bitcoin_internals
This is an internal macro, now that we have the `internals` crate put
`const_assert` in it.
2022-10-20 06:15:44 +11:00
Tobin C. Harding 5a8a5ff6c9 Move debug_from_display to bitcoin_internals
This is an internal macro, now that we have the `internals` crate put
`debug_from_display` in it.
2022-10-20 06:15:23 +11:00
Tobin C. Harding a2f08f2bc6 Improve docs on impl_array_newtype macro
Trivial cleanup of docs.
2022-10-20 06:15:13 +11:00
Tobin C. Harding 771cdde282 Move impl_array_newtype to bitcoin_internals
`impl_array_newtype` is an internal macro, move it to a new, ever so
meaningfully named, `macros` module.

Use `#[macro_export]`, no other changes to the macro.
2022-10-20 06:14:59 +11:00
Tobin C. Harding ee19a1633d internals: Run formatter
Add one `rustfmt::skip` statement and run `cargo +nightly fmt`.
2022-09-20 14:24:47 +10:00
Andrew Poelstra f41ec20ee4
Merge rust-bitcoin/rust-bitcoin#1273: Redesign `hex::BufEncoder` to accept owned arrays
1bf885550e Redesign `hex::BufEncoder` to accept owned arrays (Martin Habovstiak)

Pull request description:

  Not being able to create an owned `BufEncoder` prevented returning it from functions which need to allocate the buffer on stack. Such is the case in WIP serde via consensus serialzation.

  This change refactors `OutBytes` to be unsized, adds an `AsOutBytes` trait and uses that one instead of `Into` to perform the conversion.

  Closes #1270

  This is meant as potentially mergeable demonstration. Interestingly, it was easier than I expected.

ACKs for top commit:
  tcharding:
    Except for the question about the `out_bytes` module, ACK 1bf885550e
  apoelstra:
    ACK 1bf885550e

Tree-SHA512: 39bc48e54ae0e66b988ba5ad6ea7ecbe03e5f4c71792df0f8e2b03aa2e97e2c0fac1cb03e84ecac12ec6f13649554b57e1000710c34c638d17d9bb575d0ac0a1
2022-09-15 13:17:36 +00:00
Martin Habovstiak 1bf885550e Redesign `hex::BufEncoder` to accept owned arrays
Not being able to create an owned `BufEncoder` prevented returning it
from functions which need to allocate the buffer on stack. Such is the
case in WIP serde via consensus serialzation.

This change refactors `OutBytes` to be unsized, adds an `AsOutBytes`
trait and uses that one instead of `Into` to perform the conversion.
2022-09-14 19:41:58 +02:00
Martin Habovstiak 6b96050d1f Document `cfg`
This adds previously-forgotten documentation of items only available on
`alloc` feature.
2022-09-14 09:06:25 +02:00
Martin Habovstiak 6fc4860813 Activate `rust_v_1_46` when on high-enough rustc
This adds a build script that automatically activates the cfg if the
version is sufficient. We don't make API changes here, just improve
debugging by enabling `track_caller`.
2022-09-14 08:55:02 +02:00
Martin Habovstiak 040b14ef1a Implement basic support for fast hex encoding
There's a `hex` module in `bitcoin_hashes` which is a bit out of place
and not very fast - it passes each *digit* through dynamic dispatch not
only adding overhead but also not allowing `String` to make better
allocations.

This change adds basic support for hex encoding using a stack-allocated
buffer which minimizes the overhead of dynamic dispatch. It also
provides a new `DisplayHex` trait designed to replace `ToHex` found in
`bitcoin_hashes`.
2022-09-13 22:17:31 +02:00
Tobin C. Harding 834bbf461f Introduce bitcoin-internals crate
Add a new crate `bitcoin-internals` to be used for internal code needed
by multiple soon-to-be-created crates.

Add the `write_err` macro to `bitcoin-internals`, nothing else.

This patch uses a `path` dependency which means `rust-bitcoin` cannot be
released in its current state, will need to be changed once we release
the `bitcoin-internals` crate on `crates.io`.
2022-09-13 08:59:57 +10:00