Skip to content

Conversation

@shymega
Copy link

@shymega shymega commented May 5, 2022

This is a draft PR that adds no_std support to the crate, thereby enabling embedded hardware to use the crate

It is a collaborative effort with @birkenfeld and @shymega (I), and so the discussion will be moved here.

I had made a grave error in originally opening a PR for this, in that I opened it here - Cosmo-CoDiOS#2 - a mistake on my part.

Hopefully, this can now be reviewed.

@shymega shymega marked this pull request as draft May 5, 2022 19:28
@liranringel
Copy link
Owner

Great!
I’m quite busy these days, I hope I’ll find time this week to review this PR.

@shymega
Copy link
Author

shymega commented May 8, 2022

Thanks. It doesn't pass tests quite yet when you use core2 in the macro, I haven't committed those. Should have mentioned it. It still needs work.

@liranringel
Copy link
Owner

Ah, ok.
Do you prefer some feedback now or should I wait?

@shymega
Copy link
Author

shymega commented May 12, 2022

Some feedback now would be good. The problem with adding core2 in the macro is that it breaks all tests. I suspect I'm doing it wrong.

@shymega
Copy link
Author

shymega commented Aug 17, 2022

Hey @liranringel, sorry to prod again, but I'm stuck with the PR so far, and need some feedback on its current state. Thanks!

@liranringel
Copy link
Owner

@shymega it's looking good.
For now, you just upgraded quote and moved from proc-macro-hack to proc-macro2 which is great!
Do you want to merge these changes now, and make further changes later?

@shymega
Copy link
Author

shymega commented Aug 23, 2022

Well, it's not complete yet. It doesn't run on no_std, because the macro itself still uses std. I need some help figuring out the macro. Can you help?

@liranringel
Copy link
Owner

@shymega I'm sorry I don't have much time these days...

@shymega
Copy link
Author

shymega commented Sep 14, 2022

No worries. Been working on it some more. @birkenfeld I've pushed with my latest change, do you have a moment to look through? I'm new to macros with Rust, I barely use them, so I'm blindly coding. Thanks! Specifically, this commit: e0e4c81

I'm also going to squash all these commits into one once tests pass and it works on no_std.

Signed-off-by: Dom Rodriguez <shymega@shymega.org.uk>
@shymega shymega marked this pull request as ready for review November 18, 2022 19:10
@shymega
Copy link
Author

shymega commented Nov 18, 2022

Hi @liranringel, I've fixed the commits, so this should merge nicely. I would be grateful if you could review my work, as I'm not certain it's clean. But I hope it works.

@liranringel
Copy link
Owner

Thank you @shymega!
While I'm a bit busy these days, I'll try to review your changes as soon as I can.

@shymega
Copy link
Author

shymega commented Nov 26, 2022

@liranringel Thanks. Shall we see if @birkenfeld has any thoughts on it too?

@birkenfeld
Copy link
Contributor

I promise I'll have a look next week.

pub extern crate core2;

#[doc(hidden)]
pub use core2::io::{Cursor, Error, ErrorKind, Read, Result, Write};
Copy link
Contributor

Choose a reason for hiding this comment

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

IMO this should only be used from core2 if std is not active. Otherwise import from std::io

Copy link
Author

Choose a reason for hiding this comment

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

So, I've since squashed this PR into two commits, but it seems this review is out of date, as on my branch I can't see this line. I've since changed it to only use core2 if std isn't enabled, as-per your review.

@shymega
Copy link
Author

shymega commented Dec 2, 2022

OK, so after running tests with no_std enabled, looks like there's a lot more to fix. Vec breakages and such.

@birkenfeld
Copy link
Contributor

Yeah, byteorder's WriteBytesExt and ReadBytesExt are not defined on no_std (in hindsight, makes sense, since they use std::io too).

@shymega
Copy link
Author

shymega commented Jan 22, 2023

@birkenfeld Sorry, family stuff got hold of me. I'm just thinking of the best way to handle these edge cases. We could open an issue with byteorder and request for no_std support.

@shymega
Copy link
Author

shymega commented Feb 2, 2023

I've just enquired about the byteorder crate repository. Whilst my newer firmware doesn't need to pack data for serial, the older firmware is still heavily reliant on a similar approach to Python's struct.

I will keep this PR updated.

@shymega
Copy link
Author

shymega commented Feb 2, 2023

The comment was replied to, it's an interesting problem. We can use u64::to_le_bytes in std, but I suppose the question is, can we do that in no_std? Do we want to reimplement {Read,Write}BytesExt in the structure crate?

I'm also wondering if it's just "easier":tm: to implement the packing methods into the firmware transfer protocol on my end, and close this PR...

@shymega
Copy link
Author

shymega commented Feb 26, 2023

Just wanted to follow this up with both of you (and anyone else watching). I think the easiest solution is just to copy io.rs from byteorder and modify it to use the byteorder crate for the ByteOrder trait exposed by that crate, plus core and core2, depending on the feature flag.

OR we could implement the *Ext traits in structure, and do it that way.

The simplest solution is usually the easiest one, but I'm conscious of licensing, and making sure we don't end up with spaghetti code.

Signed-off-by: Dom Rodriguez <shymega@shymega.org.uk>
As per @birkenfeld's review of this branch, proc-macros can use `std`
during compile-time - there's no issues with `no-std`.
@shymega
Copy link
Author

shymega commented Aug 5, 2023

OK, so tests now pass on std. I need to squash the remaining commits when ready, but we're just left with byteorder not working without std.

I'm lost with the proc-macro - I haven't really dipped my toes in macros before, so it's very much new to me. I think the main change we need to make for this PR to be merged is to use to_le_bytes() and such from core when std isn't enabled, and vice versa.

Keen to get this PR in. It would mean I could then communicate over UART on a no-std environment.

shymega added 2 commits August 6, 2023 22:53
This adds a slightly modified module from `byteorder`, which should run
on `no_std`. The `src/lib.rs` has been modified in light of this change.
@liranringel
Copy link
Owner

@shymega sorry for not being here for a long time.
What is the PR status? Is it ready for review?

@shymega
Copy link
Author

shymega commented Dec 10, 2023

@liranringel:

@shymega sorry for not being here for a long time. What is the PR status? Is it ready for review?

So, currently, it works with the std feature flag.

I did attempt to copy the ByteorderExt traits into the PR, and make them work with core2. However, the proc-macro didn't like it.

I'm not sure how to proceed, and it would be great to get external thoughts on the PR as it is now.

@shymega
Copy link
Author

shymega commented Mar 21, 2024

@shymega sorry for not being here for a long time. What is the PR status? Is it ready for review?

Hey @liranringel - were you able to look into the macro?

No rush :-)

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

Successfully merging this pull request may close these issues.

3 participants