Commit Graph

36 Commits

Author SHA1 Message Date
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
Tobin C. Harding 9187bf3a65
Fix new nightly warnings/errors
The latest nightly toolchain introduced a whole bunch of new warnings
and errors, mostly to do with import statements - fix them all.
2024-02-21 14:13:49 +11:00
Tobin C. Harding db888fa4cc
io: Remove blanket trait impls
Remove the blanket impls of `Read`, `BufRead`, and `Write`. Replace them
with a set of sane impls.

Note, we add code to the `impl_write` macro to implement both
`crate::Write` and `std::io::Write` when "std" feature is
enabled.

Fix: #2432
2024-02-11 06:46:27 +11:00
Tobin C. Harding f317d87ee6
io: Enable "alloc" from "std"
As is customary in our crates, enable the "alloc" feature from the "std"
feature. This allows simplifying the feature gating.
2024-02-05 11:58:10 +11:00
Tobin C. Harding 83397c465c
io: Add documentation to all public types and functions
Add docs for all public types and add a lint to enforce this going
forwards.

Use `allow` attribute in macro, will be fixed as a subsequent patch.
2024-02-05 11:58:10 +11:00
Tobin C. Harding 2810b08b0d
io: Add code comment to feature gate
This feature gate requires a little thought to understand, add a code
comment to save the next guy some clock cycles.
2024-02-05 11:58:10 +11:00
Tobin C. Harding 4cf2bf4b40
io: Make Take::read_to_end public
Currently `Take::read_to_end` is private forcing users to use our
"custom" `read_to_limit`, for seasoned Rust hackers
`foo.take(16).read_to_end(buf)` make be more unsurprising.

Make `read_to_end` public.
2024-02-05 11:58:10 +11:00
Tobin C. Harding 0997382772
io: Enable alloc from std
It is less surprising if the "alloc" feature is enabled from the "std"
feature.

Enable "alloc" in "std" and simplify the feature gating.
2024-01-31 11:32:46 +11:00
Tobin C. Harding ba1166a63b
Make crate level attributes uniform
Make the trait level attributes uniform across all released crates in
the repo. Excludes things that are obviously not needed, eg, bench stuff
if there is not bench code.

- Remove `uninhabited_references` - this is allow by default now.
- Remove `unconditional_recursion` and mark the single false positive we
  have with an `allow`.

Note, this does not add `missing_docs` to the `io` crate. There is an
open PR at the moment to add that along with the required docs.
2024-01-31 11:32:46 +11:00
Tobin C. Harding f29da57ef6
io: Add functions to read to the end of a reader
The `std::io::Read` trait includes `read_to_end` but that method
provides a denial of service attack vector since an unbounded reader
will exhaust all system memory.

Add a method to our `Read` trait called `read_to_limit` that does the
same as `std::io::Read::read_to_end` but with memory exhaustion
protection.

Add a `read_to_end` method on our `Take` trait and call through to it
from the new method on our `Read` trait called `read_to_limit`.
2024-01-17 11:23:06 +11:00
Andrew Poelstra 2073a40c50
Merge rust-bitcoin/rust-bitcoin#2240: Require `BufRead` instead of `Read`
263a8b3603 Require BufRead instead of Read (Tobin C. Harding)
32d68fd1fa io: Add BufRead trait (Tobin C. Harding)

Pull request description:

  Require `BufRead` instead of `Read` for consensus decode trait.

ACKs for top commit:
  Kixunil:
    ACK 263a8b3603
  apoelstra:
    ACK 263a8b3603

Tree-SHA512: 58ad04c7267f9091738463331473bd22b61e6b06a13aec38b3602a369cd8e571d7d1388fd81dd7a0a05f2e8d5a9c35270cd8a918a4fafe636506591ed06a4cb2
2024-01-16 15:16:54 +00:00
Tobin C. Harding 32d68fd1fa
io: Add BufRead trait
Add a `BufRead` trait for types that perform buffered reading.

Implement it for:
- `Take`
- `Cursor`
- `std::io::BufRead` readers
- (in no-std builds) for slice of u8s
2024-01-16 09:11:20 +11:00
Tobin C. Harding d480adaf25
io: Simplify crate docs and add README
Simplify the docs in `lib.rs` and copy them into a minimal README file.
2024-01-06 08:22:36 +11:00
Fmt Bot 5af7727250 2023-12-17 automated rustfmt nightly 2023-12-17 00:59:05 +00:00
Tobin C. Harding 063dac03bd
Import core::cmp 2023-12-13 14:22:01 +11:00
Tobin C. Harding c038d00bd1
io: Move use statement
Use the newly settled upon policy governing layout for imports.
2023-12-13 14:22:01 +11:00
Tobin C. Harding 21c44df76e
inline sink function 2023-12-13 14:22:00 +11:00
Tobin C. Harding 5610b9a6b6
Remove unnecessary rustdoc
This doc does not document any type, remove it.
2023-12-13 14:22:00 +11:00
Fmt Bot 8e7afe5d4a 2023-12-10 automated rustfmt nightly 2023-12-10 00:58:56 +00:00
Tobin C. Harding b1870656c9
Combine the two Error impl blocks together
We have two impl blocks for `Error`, just squash them together into a
single one.
2023-12-04 06:48:27 +11:00
Tobin C. Harding 82ea4ff31d
Move error code to submodule
Move error code to a private `error` submodule and re-export it at the
crate root.

(Puts private `sealed` module at the bottom of the file i.e., this patch
is strictly a code move but we re-order the code while moving it.)
2023-12-04 06:47:14 +11:00
Tobin C. Harding 67511ed03f
Move no_std above comment
Currently the no_std attribute is below a comment that does not relate
to it. For slightly improved clarity move the attribute above the
comment.
2023-12-04 06:37:46 +11:00
Tobin C. Harding f83d68f246
Add vertical whitespace
Make the `io` crate conform to `rust-bitcoin` code style in regards to
whitespace between functions/types etc.

Whitespace only, no other changes.
2023-12-04 06:37:43 +11:00
apoelstra b7604dd768 2023-12-03 automated rustfmt nightly 2023-12-03 00:58:10 +00:00
Tobin C. Harding 5c0759a390
Inline io module in io crate root
Its not immediately obvious why we nest the whole `io` code in an `io`
submodule within `lib.rs`. As far as I can tell we can inline it and
re-export from `rust-bitcoin` same as we do for our other dependencies.

This change would effect other users of the crate but since the `io`
crate is unreleased this effects no-one except us.
2023-11-28 14:17:14 +11:00
Tobin C. Harding 80fe9b99b2
Move public macros to a separate module
In preparation for inlining the `io` molule, move the public macros to a
private `macros` module.

Includes removal of the public re-export of `std` as `_std` - flaggin
this because I do not understand why it is here in the first place, we
can use `std::io::Write` in code that is feature gated on "std".
2023-11-28 11:43:35 +11:00
apoelstra 1c89e07537 2023-11-26 automated rustfmt nightly 2023-11-26 00:57:53 +00:00
Matt Corallo b7dd16da99 [IO] Use our own io::Error type
In order to move towards our own I/O traits in the `rust-bitcoin`
ecosystem, we have to slowly replace our use of the `std` and
`core2` traits.

This is the final step in removing the explicit `core2` dependency
for I/O in no-std - replacing the `io::Error` type with our own.

Sadly the `std::io::Error` type requires `std::error::Error` as a
bound on the inner error, which is rather difficult to duplicate in
a way that allows for mapping to `std` and back.

To take a more general approach, we use bound on any `Debug`
instead.
2023-11-07 17:35:18 +00:00
Matt Corallo 3caaadf9bb [IO] Replace the `io::Cursor` re-export with our own `Cursor` 2023-11-07 17:35:17 +00:00
Matt Corallo 141343edb4 [IO] Move to custom `Read` trait mirroring `std::io::Read`
In order to move towards our own I/O traits in the `rust-bitcoin`
ecosystem, we have to slowly replace our use of the `std` and
`core2` traits.

Here we take the second big step, replacing
`{std,core2}::io::Read` with our own `bitcoin_io::io::Read`. We
provide a blanket impl for our trait for all `std::io::Read`, if
the `std` feature is enabled, allowing users who use their own
streams or `std` streams to call `rust-bitcoin` methods directly.
2023-11-07 05:51:39 +00:00
Matt Corallo 6aa7ccf841 [IO] Replace `std::io::Sink` usage with our own trivial impl 2023-11-07 05:51:36 +00:00
Matt Corallo 7eb5d65bda [IO] Provide a macro which implements `io::Write` for types
With the new `bitcoin_io` library, implementing `io::Write`
manually is somewhat tricky - for `std` users we really want to
provide an `std::io::Write` implementation, however for `no-std`
users we want to implement against our internal trait.

Sadly we cannot provide a blanket implementation of
`std::io::Write` for all types whcih implement our `io::Write`
trait as its an out-of-crate impl.

Instead, we provide a macro which will either implement
`std::io::Write` or our `io::Write` depending on the feature flags
set on `bitcoin_io`.
2023-11-07 05:51:34 +00:00
Matt Corallo ac678bb435 [IO] Move to custom `Write` trait mirroring `std::io::Write`
In order to move towards our own I/O traits in the `rust-bitcoin`
ecosystem, we have to slowly replace our use of the `std` and
`core2` traits.

Here we take the first real step, replacing
`{std,core2}::io::Write` withour own `bitcoin_io::io::Write`. We
provide a blanket impl for our trait for all `std::io::Write`, if
the `std` feature is enabled, allowing users who use their own
streams or `std` streams to call `rust-bitcoin` methods directly.
2023-11-07 05:51:31 +00:00
Matt Corallo 5e0209569c Use `io::sink` rather than our custom `EmptyWrite` utility 2023-11-07 05:50:45 +00:00
Matt Corallo a0ade883b6 [IO] Move io module into selected re-exports
In the coming commits we'll move our `io` module to having its own
implementations of the usual I/O traits and structs. Here we first
move to re-exporting only exactly what we need, allowing us to
whittle the list down from a fixed set.
2023-11-07 05:50:42 +00:00
Matt Corallo 27c7c4e26a Add a `bitcoin_io` crate
In order to support standard (de)serialization of structs, the
`rust-bitcoin` ecosystem uses the standard `std::io::{Read,Write}`
traits. This works great for environments with `std`, however sadly
the `std::io` module has not yet been added to the `core` crate.

Thus, in `no-std`, the `rust-bitcoin` ecosystem has historically
used the `core2` crate to provide copies of the `std::io` module
without any major dependencies. Sadly, its one dependency,
`memchr`, recently broke our MSRV.

Worse, because we didn't want to take on any excess dependencies
for `std` builds, `rust-bitcoin` has had to have
mutually-exclusive `std` and `no-std` builds. This breaks general
assumptions about how features work in Rust, causing substantial
pain for applications far downstream of `rust-bitcoin` crates.

Here, we add a new `bitcoin_io` crate, making it an unconditional
dependency and using its `io` module in the in-repository crates
in place of `std::io` and `core2::io`. As it is not substantial
additional code, the `hashes` io implementations are no longer
feature-gated.

This doesn't actually accomplish anything on its own, only adding
the new crate which still depends on `core2`.
2023-11-07 05:50:42 +00:00