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

Add libc_bitflags convenience macro #315

Merged
merged 2 commits into from
Mar 16, 2016
Merged

Conversation

kamalmarhubi
Copy link
Member

We define many bitflags types with values from the libc crate. Currently
these look like this:

bitflags!{
    flags ProtFlags: libc::c_int {
        const PROT_NONE      = libc::PROT_NONE,
        const PROT_READ      = libc::PROT_READ,
        const PROT_WRITE     = libc::PROT_WRITE,
        const PROT_EXEC      = libc::PROT_EXEC,
        #[cfg(any(target_os = "linux", target_os = "android"))]
        const PROT_GROWSDOWN = libc::PROT_GROWSDOWN,
        #[cfg(any(target_os = "linux", target_os = "android"))]
        const PROT_GROWSUP   = libc::PROT_GROWSUP,
    }
}

There's some repetition which is tedious. With the new macro, the above
can instead be written

libc_bitflags!{
    flags ProtFlags: libc::c_int {
        PROT_NONE,
        PROT_READ,
        PROT_WRITE,
        PROT_EXEC,
        #[cfg(any(target_os = "linux", target_os = "android"))]
        PROT_GROWSDOWN,
        #[cfg(any(target_os = "linux", target_os = "android"))]
        PROT_GROWSUP,
    }
}

Thanks to Daniel Keep for the Little Book of Rust Macros, and for
helping with this macro.

Refs #264

@kamalmarhubi
Copy link
Member Author

r? @fiveop @posborne

///
/// # Example
/// ```
/// # #[macro_use] extern crate nix;
Copy link
Contributor

Choose a reason for hiding this comment

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

I would not make this publicly available.

Copy link
Member Author

Choose a reason for hiding this comment

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

ah this was me misunderstanding how macro visibility worked. good catch!

@fiveop
Copy link
Contributor

fiveop commented Mar 16, 2016

I like this change.

Can you change all occurences instead of just one, right away. It should not be so many.

We define many bitflags types with values from the libc crate. Currently
these look like this:

    bitflags!{
        flags ProtFlags: libc::c_int {
            const PROT_NONE      = libc::PROT_NONE,
            const PROT_READ      = libc::PROT_READ,
            const PROT_WRITE     = libc::PROT_WRITE,
            const PROT_EXEC      = libc::PROT_EXEC,
            #[cfg(any(target_os = "linux", target_os = "android"))]
            const PROT_GROWSDOWN = libc::PROT_GROWSDOWN,
            #[cfg(any(target_os = "linux", target_os = "android"))]
            const PROT_GROWSUP   = libc::PROT_GROWSUP,
        }
    }

There's some repetition which is tedious. With the new macro, the above
can instead be written

    libc_bitflags!{
        flags ProtFlags: libc::c_int {
            PROT_NONE,
            PROT_READ,
            PROT_WRITE,
            PROT_EXEC,
            #[cfg(any(target_os = "linux", target_os = "android"))]
            PROT_GROWSDOWN,
            #[cfg(any(target_os = "linux", target_os = "android"))]
            PROT_GROWSUP,
        }
    }

Thanks to Daniel Keep for the Little Book of Rust Macros, and for
helping with this macro.

Refs nix-rust#264
@kamalmarhubi
Copy link
Member Author

Going through the rest now.

@posborne
Copy link
Member

Got my approval as well, although I have not gone through the macros with a fine-tooth comb (but agreed with @fiveop on mot making the macro public). I would be fine with merging this even if we don't have 100% of libc flags converted.

Once you are happy with things, r=me is fine.

@kamalmarhubi
Copy link
Member Author

I changed visibility of the macro, and added the use of the macro to the conventions doc.

I would be fine with merging this even if we don't have 100% of libc flags converted.

Having spent most of the morning today trying to convert ~60 uses with medium success, I'd much rather merge this and do the rest incrementally. The conversion should happen but isn't high priority.

Leaving this to one of you to r+ taking into account the conventions change.

@posborne
Copy link
Member

Looks good to me. @homu r+

@homu
Copy link
Contributor

homu commented Mar 16, 2016

📌 Commit c9e5a15 has been approved by posborne

@homu
Copy link
Contributor

homu commented Mar 16, 2016

⌛ Testing commit c9e5a15 with merge 4715dde...

homu added a commit that referenced this pull request Mar 16, 2016
Add libc_bitflags convenience macro

We define many bitflags types with values from the libc crate. Currently
these look like this:

    bitflags!{
        flags ProtFlags: libc::c_int {
            const PROT_NONE      = libc::PROT_NONE,
            const PROT_READ      = libc::PROT_READ,
            const PROT_WRITE     = libc::PROT_WRITE,
            const PROT_EXEC      = libc::PROT_EXEC,
            #[cfg(any(target_os = "linux", target_os = "android"))]
            const PROT_GROWSDOWN = libc::PROT_GROWSDOWN,
            #[cfg(any(target_os = "linux", target_os = "android"))]
            const PROT_GROWSUP   = libc::PROT_GROWSUP,
        }
    }

There's some repetition which is tedious. With the new macro, the above
can instead be written

    libc_bitflags!{
        flags ProtFlags: libc::c_int {
            PROT_NONE,
            PROT_READ,
            PROT_WRITE,
            PROT_EXEC,
            #[cfg(any(target_os = "linux", target_os = "android"))]
            PROT_GROWSDOWN,
            #[cfg(any(target_os = "linux", target_os = "android"))]
            PROT_GROWSUP,
        }
    }

Thanks to Daniel Keep for the Little Book of Rust Macros, and for
helping with this macro.

Refs #264
@kamalmarhubi kamalmarhubi force-pushed the libc-bitflags branch 3 times, most recently from 18aa62f to c9a3dc8 Compare March 16, 2016 20:10
This serves as an example use of the libc_bitflags macro.
@kamalmarhubi
Copy link
Member Author

@homu retry 7038da0

@kamalmarhubi
Copy link
Member Author

@homu retry

@kamalmarhubi
Copy link
Member Author

One day I'll get the hang of @homu r+ 7038da0

@homu
Copy link
Contributor

homu commented Mar 16, 2016

⌛ Testing commit 7038da0 with merge 431e470...

homu added a commit that referenced this pull request Mar 16, 2016
Add libc_bitflags convenience macro

We define many bitflags types with values from the libc crate. Currently
these look like this:

    bitflags!{
        flags ProtFlags: libc::c_int {
            const PROT_NONE      = libc::PROT_NONE,
            const PROT_READ      = libc::PROT_READ,
            const PROT_WRITE     = libc::PROT_WRITE,
            const PROT_EXEC      = libc::PROT_EXEC,
            #[cfg(any(target_os = "linux", target_os = "android"))]
            const PROT_GROWSDOWN = libc::PROT_GROWSDOWN,
            #[cfg(any(target_os = "linux", target_os = "android"))]
            const PROT_GROWSUP   = libc::PROT_GROWSUP,
        }
    }

There's some repetition which is tedious. With the new macro, the above
can instead be written

    libc_bitflags!{
        flags ProtFlags: libc::c_int {
            PROT_NONE,
            PROT_READ,
            PROT_WRITE,
            PROT_EXEC,
            #[cfg(any(target_os = "linux", target_os = "android"))]
            PROT_GROWSDOWN,
            #[cfg(any(target_os = "linux", target_os = "android"))]
            PROT_GROWSUP,
        }
    }

Thanks to Daniel Keep for the Little Book of Rust Macros, and for
helping with this macro.

Refs #264
@homu
Copy link
Contributor

homu commented Mar 16, 2016

☀️ Test successful - status

@homu homu merged commit 7038da0 into nix-rust:master Mar 16, 2016
@kamalmarhubi
Copy link
Member Author

Oh I guess that should have been r=@posborne, ah well

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.

4 participants