Skip to content
This repository has been archived by the owner on Nov 30, 2022. It is now read-only.

Read/Write traits with no_std #120

Closed
wants to merge 3 commits into from

Conversation

RCasatta
Copy link
Contributor

@RCasatta RCasatta commented Apr 12, 2021

on top of #119 (to be rebased)

AFAIK
"It's not possible to move std::io::{Read,Write} into alloc or core because the API depends on OS specific bits (e.g. last_os_error). An option may be to add a new set of Read / Write traits to core, maybe with associated error types, and then try to bridge these to std::io::{Read,Write} using blanket implementations / super traits, but this needs more research."

So, to include read/write traits the trait core2 has been used

the feature std has been replaced by the inverse no_std because it was needed to include the optional core2 dep

Computing hash from the embedded example

@RCasatta RCasatta changed the title Write traits Read/Write traits with no_std Apr 12, 2021
@@ -0,0 +1,34 @@
MEMORY
Copy link
Member

Choose a reason for hiding this comment

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

Is there not an easier way to test embedded stuff than four config files just for a test?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, like #119 (comment)

however, this way is suggested in the rust embedded book from the rust embedded team, so maybe more common for embedded devs?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think .cargo/config could be easy removed in favor of command parameters and .github/workflows/embedded.yml could be mixed with the other github actions. Would that be ok?

Copy link
Member

Choose a reason for hiding this comment

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

I don't know that we need to squash the workflows files down, but if its possible to remove some of the config files it would be nice, especially given a few of them look like half-comments.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

#119 is updated without comments and fewer config files. Could you please review it there?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

superseded by #122

@@ -186,7 +186,6 @@ impl ToHex for [u8] {
}
}

#[cfg(any(test, feature = "std"))]
Copy link
Member

Choose a reason for hiding this comment

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

Wouldn't it be easier to just have a pub function that converts these objects to hex and then only have the FromHex/etc traits on std-mode, that way its usable either way, but we don't take a new dependency just for hex?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If you are referring to the new dependency core2, that is for Read/Write traits used in impls.rs

If you are referring to alloc, yes it may be worthy to avoid using alloc just for this traits

Copy link
Member

Choose a reason for hiding this comment

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

I'm referring to core2 (indeed, alloc isn't really a "new" dependency, as its part of std anyway) - an alternative approach to avoid core2 would be to reimplement the io::Write/Read traits as functions, with versions which expose Write/Read if we're building with std. Ultimately a user not using std isn't going to have Write or Read anyway, they just want a byte buffer (or a string, in the case of hex), which we can just give them, instead of exposing the Write or Read traits (which they're unlikely to use anyway - they don't have std so probably no files, just writing into Vecs).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

#122 is not using core2 and it is simply using input function from the Engine

Copy link
Member

Choose a reason for hiding this comment

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

@TheBlueMatt I don't think it's realistic to expect people not to use Read or Write, these are universal traits used by all sorts of things, and it looks like core2 is the "standard" way to do this without std. So I think we have to bite the bullet and just use it, even though it bumps our MSRV for nostd and even though it's an extra dep.

We could plausibly get away without it here, or maybe feature-gate it even, but for rust-bitcoin I think we'll need it no matter what, unless we want to either disable all the decoding/encoding stuff (i.e. most of the library) or rewrite it.

Copy link
Member

Choose a reason for hiding this comment

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

Ah, yeah, I see what you're saying. If some 3rd party writes a fn myfunc<W: Write>(thing: W) and we have elsewhere impl<T: OurCustomNoStdWrite> Write for T { ... } then people can use our types when calling myfunc and not care that we did our own weird thing.

@RCasatta I've changed my mind to agree with Matt now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

rust-bitcoin would need to replace every std::io::Write with our OurCustomNoStdWrite from bitcoin-hashes, is it correct?

(rust-bech32 doesn't use Write, otherwise, it would be somehow out of context here)

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, this is correct. Similar to what we did had with Encodable prior to replacing that with io::Write

Copy link
Contributor

Choose a reason for hiding this comment

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

Interesting discussion. I didn't see this before opening the following PR in rust-bitcoin with a similar approach:

https://github.com/rust-bitcoin/rust-bitcoin/pull/603/files#diff-76866598ce8fd16261a27ac58a84b2825e6e77fc37c163a6afa60f0f4477e569R28

Copy link
Contributor

Choose a reason for hiding this comment

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

(note that I rename it to just Write when I import it into the root - in case you are looking at the rest of the PR)

@RCasatta
Copy link
Contributor Author

superseded by #122

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.

4 participants