GithubHelp home page GithubHelp logo

Comments (16)

MaikKlein avatar MaikKlein commented on June 26, 2024 1

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.

from enumflags.

MaikKlein avatar MaikKlein commented on June 26, 2024 1

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

from enumflags.

MaikKlein avatar MaikKlein commented on June 26, 2024

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.

from enumflags.

MaikKlein avatar MaikKlein commented on June 26, 2024

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.

from enumflags.

jmi2k avatar jmi2k commented on June 26, 2024

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...

from enumflags.

MaikKlein avatar MaikKlein commented on June 26, 2024
  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.

from enumflags.

MaikKlein avatar MaikKlein commented on June 26, 2024

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.

from enumflags.

MaikKlein avatar MaikKlein commented on June 26, 2024

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.

from enumflags.

MaikKlein avatar MaikKlein commented on June 26, 2024

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.

from enumflags.

jmi2k avatar jmi2k commented on June 26, 2024

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.

from enumflags.

MaikKlein avatar MaikKlein commented on June 26, 2024

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?

from enumflags.

jmi2k avatar jmi2k commented on June 26, 2024

Go ahead 👍 !

from enumflags.

jmi2k avatar jmi2k commented on June 26, 2024

I replaced bitflags with it and it works perfect!

from enumflags.

jmi2k avatar jmi2k commented on June 26, 2024

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

from enumflags.

aoowweenn avatar aoowweenn commented on June 26, 2024

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

from enumflags.

MaikKlein avatar MaikKlein commented on June 26, 2024

Should be fixed in the latest released

from enumflags.

Related Issues (12)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.