Commit Graph

28 Commits

Author SHA1 Message Date
Tobin C. Harding fd8d563b87
Remove macro debug_from_display
Rust macros, while at times useful, are a maintenance nightmare. And
we have been bitten by calling macros from other crates multiple times
in the past.

In a push to just use less macros remove the `debug_from_display`
macro and just write the code.

This is an API breaking change to `internals` but an internal change
only to any of the _real_ crates.
2025-01-02 07:31:13 +11:00
merge-script 4c8347a7ac
Merge rust-bitcoin/rust-bitcoin#3544: Favour `to_vec` over `to_bytes`
a51768af3f key: Deprecate to_bytes (Tobin C. Harding)
3af3239ad0 script: Re-order functions (Tobin C. Harding)
db40297f87 script: deprecate to_bytes (Tobin C. Harding)
c5cd0db493 Revert the change to to_bytes (Tobin C. Harding)
dc2ca785d2 Add to_vec and deprecate to_bytes for array types (Tobin C. Harding)
a6b7ab32a8 Move impl_array_newtype to internal_macros (Tobin C. Harding)

Pull request description:

  Use `to_vec` and deprecate `to_bytes`, the opposite of what we did in #2585.

  For functions that return a `Vec` by first allocating use function name `to_vec`. This explicitly excludes:

  - Functions that return an array (`CompressedPublicKey::to_bytes`)
  - Functions that consume self and return a `Vec` without allocating (`ScriptBuf::into_bytes`)

  See #3025 for discussion and consensus.

  Close: #3025

ACKs for top commit:
  apoelstra:
    ACK a51768af3f3d4c8e138e1ded250800810bedc903; successfully ran local tests

Tree-SHA512: ee932c13ad2e09c2b76a7833b23c859df175aa307f56e673921f3ae8b5d865518c6f999749e3b627594457b3ca33301b777177ada3520cf006acc0f14e5dacf8
2024-11-01 00:55:15 +00:00
Tobin C. Harding a6b7ab32a8
Move impl_array_newtype to internal_macros
The current feature gating is wrong, this bug is unreleased because it
was introduced #2585.

The `impl_array_newtype` macro is only used in the `bitcoin` crate, it
does not need to be in `internals`. Also, other crates have an `alloc`
feature which `bitcoin` does not have so if we ever need it in other
places we'll need a duplicate with the correct feature gating anyways.

Move the macro to `bitcoin::internal_macros` and remove the incorrect
`alloc` feature gating.
2024-10-31 14:15:41 +11:00
Tobin C. Harding bafe11d7e4
Correctly feature gate impl_to_hex_from_lower_hex
Currently we feature gate code within the `impl_to_hex_from_lower_hex`
 macro on "alloc" but `bitcoin` does not have the "alloc" feature so
 this code is never built in. This can be seen by the lack of a
 `to_hex` function on `LeafVersion`.

Remove the feature gate from the macro and put it on the individual
call sites as needed.
2024-10-31 13:43:47 +11:00
Jamil Lambert, PhD f6abdcc001
Allow unused in `macros.rs` docs
The example code in macros.rs is much clearer as it is than changing
it to remove the warnings created by adding the warn attribute to the
`lib.rs` file.

The example code was enclosed within a function block and
`#[allow(unused)]` added.  And a warn attribute added to `lib.rs`.
2024-09-18 16:38:19 +01:00
Shing Him Ng 9459739457 Move feature flag inside impl_to_hex_from_lower_hex macro 2024-09-16 15:00:54 -05:00
Tobin C. Harding 514cc5500f
internals: Fix lint warnings on macro
The new `impl_to_hex_from_lower_hex` macro causes build warnings when
`internals` is built without the `alloc` feature.

There are two macro implementations depending on feature gates,
improve the docs and duplicate them ont both macro definitions.
2024-09-11 08:57:50 +10:00
Fmt Bot 9a5ba9b6df 2024-09-08 automated rustfmt nightly 2024-09-08 01:17:12 +00:00
merge-script 3b057ad2f5
Merge rust-bitcoin/rust-bitcoin#3150: Create a macro that implements `to_hex` for types that have `core::fmt::LowerHex` implemented
30bb93c676 Implement impl_to_hex_from_lower_hex macro for types that implement fmt::LowerHex (Shing Him Ng)

Pull request description:

  Created a macro that implements `to_hex` for types that currently have `core::fmt::LowerHex` and called it on types that have `core::fmt::LowerHex` implemented. I put the macro in the `internals` crate since there are types across the whole project that can potentially use this.

  Resolves #2869

ACKs for top commit:
  Kixunil:
    ACK 30bb93c676
  apoelstra:
    ACK 30bb93c676 successfully ran local tests

Tree-SHA512: d3ebc7b5c0c23f1a8f8eef4379c1b475e8c23845e18ce514cb1e98eb63fc4f215e6bc4425f97c7303053df13374ef931ae9d9373badd7ca1975a55b0d00d0e40
2024-09-02 21:18:44 +00:00
Tobin C. Harding c71b23d81d
Remove bool from cont_assert
It was correctly pointed out during review of #3215 (when we made
`const_assert` panic) that using a `bool` added no additional
information.

Remove the `bool` and just use unit.
2024-09-02 08:21:53 +10:00
Shing Him Ng 30bb93c676 Implement impl_to_hex_from_lower_hex macro for types that implement fmt::LowerHex 2024-08-31 22:41:40 -05:00
Tobin C. Harding 3c7c8c44b6
Improve const_assert
Now that we can panic in const context we can improve the `const_assert`
macro by adding a message string.

Original idea by Kix:

  https://github.com/rust-bitcoin/rust-bitcoin/pull/2972#discussion_r1726328228
2024-08-30 06:33:31 +10:00
Tobin C. Harding 49a6acc1a0
internals: Remove double parenthesis in const_assert
The current `const_assert` macro is unused in the code base. We would
like to use it differently to how it was initially designed, remove the
parenthesis so it can be called directly in a module.
2024-08-05 05:34:48 +10:00
Andrew Poelstra 65a5dfcd6b
Merge rust-bitcoin/rust-bitcoin#2585: Improve array macros
6ba7758b30 Improve array macros (Tobin C. Harding)

Pull request description:

  Currently we have two macros used when creating array wrapper types, one is in `internals` and the other in `bitcoin::internal_macros`. It is not immediately obvious what is what and why there are two.

  Improve the macros by:

  - Move the inherent functions to `impl_array_newtype`
  - Use `*_byte_array` for the names instead of `*_bytes`
  - Re-name the other macro to match what it now does

ACKs for top commit:
  apoelstra:
    ACK 6ba7758b30

Tree-SHA512: 36ed0fae0d28f24d29287062eb05bbc1e9e8b565f4ff41fd893503a25404ed8e185a34d75e398a8a660923ffda3b832b6157011598d5a75a5c4aafdffc74af2a
2024-05-28 16:25:59 +00:00
jamil.lambert d219ceb68e Standardize function doc Examples
Changed the function docs to have the same format of
///
/// # Examples
///
/// description
2024-05-24 09:59:42 +01:00
jamil.lambert 6c1a5401a7 Removed //! spare line at end of headers
Some of the headers had a //! at the end but most didn't. They have all been removed in internals/src/ to make the files consistent
2024-05-22 12:23:57 +01:00
Tobin C. Harding 6ba7758b30
Improve array macros
Currently we have two macros used when creating array wrapper types,
one is in `internals` and the other in `bitcoin::internal_macros`. It
is not immediately obvious what is what and why there are two.

Improve the macros by:

- Move the inherent functions to `impl_array_newtype`
- Use `*_byte_array` for the names instead of `*_bytes` for functions
  that return arrays
- Add `as_bytes` to return a slice
- Add `to_bytes` to return a vector
- Re-name the other macro to match what it now does
2024-05-14 10:42:40 +10:00
Fmt Bot a565db9fdd 2024-03-31 automated rustfmt nightly 2024-03-31 01:03:18 +00:00
Liam Aharon b9f7462958
Implement infallible for errors
Creates a new macro `impl_from_infallible`, and applies it to custom
error types in the codebase.

Closes #1222.
2024-03-08 16:48:34 +11:00
Steven Roose aa6e5cd342
Use full path in all macro usage of Result 2024-01-18 13:20:19 +00:00
Tobin C. Harding 1c3bbd4bf2
internals: Remove attribution from all files
As we did for the `bitcoin` crate, remove attribution from all files in
the `internals` crate.

While we are at it add an SPDX line to the few files missing it, whether
this license nonsense is even needed is left as an argument for another
day.

Justification:

Currently we have a mishmash of attribution lines accompanying the SPDX
identifier. These lines are basically meaningless because:

    - The date is often wrong
    - The original author attributed is not the only contributor to a file
    - The term "rust bitcoin developers" is basically just noise

Just remove all the attribution lines and be done with it.
2023-05-01 09:31:42 +10:00
Tobin C. Harding a11cf07501
Run the formatter
Various formatting issues have crept into the codebase because we do not
run the formatter in CI.

In preparation for enabling formatting checks in CI run `cargo +nightly
fmt` to fix current formatting issues. No changes other than those
create by the formatter.
2023-03-06 10:22:29 +11:00
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
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