structure icon indicating copy to clipboard operation
structure copied to clipboard

Add `no_std` support to structure

Open shymega opened this issue 2 years ago • 23 comments

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 - https://github.com/Cosmo-CoDiOS/structure/pull/2 - a mistake on my part.

Hopefully, this can now be reviewed.

shymega avatar May 05 '22 19:05 shymega

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

liranringel avatar May 08 '22 23:05 liranringel

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.

shymega avatar May 08 '22 23:05 shymega

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

liranringel avatar May 08 '22 23:05 liranringel

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 avatar May 12 '22 13:05 shymega

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

shymega avatar Aug 17 '22 17:08 shymega

@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?

liranringel avatar Aug 20 '22 14:08 liranringel

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?

shymega avatar Aug 23 '22 11:08 shymega

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

liranringel avatar Sep 11 '22 10:09 liranringel

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.

shymega avatar Sep 14 '22 22:09 shymega

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.

shymega avatar Nov 18 '22 19:11 shymega

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

liranringel avatar Nov 20 '22 20:11 liranringel

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

shymega avatar Nov 26 '22 21:11 shymega

I promise I'll have a look next week.

birkenfeld avatar Nov 27 '22 11:11 birkenfeld

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

shymega avatar Dec 02 '22 23:12 shymega

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

birkenfeld avatar Dec 03 '22 06:12 birkenfeld

@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 avatar Jan 22 '23 21:01 shymega

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 avatar Feb 02 '23 22:02 shymega

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 avatar Feb 02 '23 22:02 shymega

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.

shymega avatar Feb 26 '23 19:02 shymega

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 avatar Aug 05 '23 22:08 shymega

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

liranringel avatar Nov 21 '23 13:11 liranringel

@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 avatar Dec 10 '23 19:12 shymega

@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 :-)

shymega avatar Mar 21 '24 23:03 shymega