Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid using std dependency #5

Closed
jmi2k opened this issue Apr 1, 2017 · 16 comments
Closed

Avoid using std dependency #5

jmi2k opened this issue Apr 1, 2017 · 16 comments

Comments

@jmi2k
Copy link

jmi2k commented Apr 1, 2017

Removing std would allow its usage in embedded programming, or in environments where it isn't available.

@MaikKlein
Copy link
Owner

I myself don't do embedded programming but I guess there shouldn't a problem with switching from std to core. I'll look into it tomorrow.

@MaikKlein
Copy link
Owner

I currently use a vector for the debug output https://github.com/MaikKlein/enumflags/blob/master/enumflags_derive/src/lib.rs#L229

I also want to generalize that behavior so that you can create a flag from a vector of flags, and that you can create a vector of flags from a single flag. I do this to generate a better debug output, eg BitFlags { 0b1111, Flags::[A, B, C, D] }. But this is quite unnecessary for the debug output, it should be possible to avoid the heap usage here by directly writing to the fmt instead of collecting into a vector.

I think that it should be possible to feature gate the std usage.

@jmi2k
Copy link
Author

jmi2k commented Apr 2, 2017

I took care of removing std-dependent code in my fork, but I need your help with some things:

I think these changes are enough. I'll make a (new) PR if I make it work...

@MaikKlein
Copy link
Owner

MaikKlein commented Apr 2, 2017

  1. and 3.: It probably breaks because I still rely on the std. I didn't realize that I already had implemented from_bitflag. This is useful if you ever want to use enumflags in a config file. For example
[Input.Jump]
Key = Left
Modifiers = [Shift, Ctrl] // <- enumflags

Which means I need a way to convert from a list of flags into a BitFlags<Flag> and the other way around, this is essentially what ``from_bitflag` is currently for.

I think we should just feature gate the std usage like here https://github.com/BurntSushi/byteorder/blob/master/src/lib.rs#L53

#[cfg(feature = "std")]
pub fn from_bitflag(bitflag: ::enumflags::BitFlags<#ident>) -> Vec<#ident> {
  1. This is something that shouldn't be there.

I really need to add some documentation and tests.

@MaikKlein
Copy link
Owner

MaikKlein commented Apr 2, 2017

Regarding extern crate core. I implement something called

pub struct #inner_name(#ty); which is the inner type of the bitflag. This is necessary because it is not possible to implement code between two crates (enumflags and enumflags_derive are two separate crates).

Currently this inner type is in user space, which seems to cause problems with core. I am not completely sure what the best cause of action would be.

The problem here is that because the inner_type is in user space which errors if the user doesn't have extern crate core. One solution would be do implement the inner_type two times for core and std, and then feature gate the implementation.

        impl #std::fmt::Debug for #inner_name{
            ...
        } 

where #std could be ::std or ::core. Otherwise I could try to find a different solution that doesn't require the inner type at all.

@MaikKlein
Copy link
Owner

Actually the bitflags create also creates everything in user space(macros) and it doesn't require the std. https://doc.rust-lang.org/bitflags/src/bitflags/lib.rs.html#250-286

I'll look into it. Sorry for the back and forth, I haven't really done anything without the std.

@MaikKlein
Copy link
Owner

The trick here is

#[allow(private_in_public)]
#[doc(hidden)]
pub use core as __core;

And then use ::__core instead of ::core. This solves your 1.

@jmi2k
Copy link
Author

jmi2k commented Apr 2, 2017

I think we can't do that because it uses no_std itself. It cannot be used in enumflags_derive because we are using some std features for code generation, even if they don't show up in the final code.

Look at my last commit, I resolved the issue with a feature. The only problem left is from_bitflag, which honestly I have no idea of what to do. I've never used quote! nor procedural macros though.

@MaikKlein
Copy link
Owner

MaikKlein commented Apr 2, 2017

I don't meant that we should use it inside enumflags_derive but in enumflags. Then you can access it like this (because it is just a public re-export).

let i = enumflags::__core::option::Option::Some(42);

Which means that we can do something like this inside enumflags_derive.

        impl enumflags::__core::ops::BitOr for #inner_name{
            type Output = Self;
            fn bitor(self, other: Self) -> Self{
                #inner_name(self.0 | other.0)
            }
        }

I got it working with the first version of your fork. Or at least it appears to be working. Do you want me to push the changes?

@jmi2k
Copy link
Author

jmi2k commented Apr 2, 2017

Go ahead 👍 !

@jmi2k
Copy link
Author

jmi2k commented Apr 2, 2017

I replaced bitflags with it and it works perfect!

@jmi2k jmi2k closed this as completed Apr 2, 2017
@jmi2k
Copy link
Author

jmi2k commented Apr 2, 2017

Sorry, I think it's better to keep it open until it's merged.

@jmi2k jmi2k reopened this Apr 2, 2017
@MaikKlein
Copy link
Owner

I pushed the changes to https://github.com/MaikKlein/enumflags/tree/no_std

You can use it like this

enumflags_derive = {path = "../enumflags_derive", default-features=false}

I want to test it a bit more before I merge it with master.

@aoowweenn
Copy link

aoowweenn commented Jan 2, 2018

Any progress here?
I just don't want println! appeared in from_bits.
Thx

@MaikKlein
Copy link
Owner

@aoowweenn Sorry for the delay, I refactored almost everything. The println should be gone now.

@MaikKlein
Copy link
Owner

Should be fixed in the latest released

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants