rust: bindings: rename const binding using sed
Gary Guo
reviewerPATCH 1/1 rust: bindings: rename const binding using sed
Current for consts that bindgen don't recognise, we define a helper constant with
const <TYPE> BINDINGS_<NAME> = <NAME>;
in bindings_helper.h and then we put
pub const <NAME>: <TYPE> = BINDINGS_<NAME>;
in bindings/lib.rs. This is fine that we currently only have 3 constants that are defined this way, but is going to be more annoying when more constants are added since every new constant needs to be defined in two places.
This patch changes the way we define constant helpers to
const <TYPE> RUST_BINDING_<NAME> = <NAME>;
and then use sed to postprocess Rust code by generated by bindgen to remove the distinct prefix, so user of the binding crate can refer to the name directly.
--- rust/Makefile | 2 ++ rust/bindings/bindings_helper.h | 6 +++--- rust/bindings/lib.rs | 3 --- rust/kernel/allocator.rs | 2 +- 4 files changed, 6 insertions(+), 7 deletions(-)
rust/Makefile +2 -0
@@ -339,6 +339,8 @@ quiet_cmd_bindgen = BINDGEN $@ $(obj)/bindings/bindings_generated.rs: private bindgen_target_flags = \ $(shell grep -v '^#\|^$$' $(srctree)/$(src)/bindgen_parameters) +$(obj)/bindings/bindings_generated.rs: private bindgen_target_extra = ; \ + sed -Ei 's/pub const RUST_BINDING_([a-zA-Z0-9_]*)/pub const \1/g' $@ $(obj)/bindings/bindings_generated.rs: $(src)/bindings/bindings_helper.h \ $(src)/bindgen_parameters FORCE $(call if_changed_dep,bindgen)
rust/bindings/bindings_helper.h +3 -3
@@ -15,6 +15,6 @@#include <linux/workqueue.h> /* `bindgen` gets confused at certain things. */ -const size_t BINDINGS_ARCH_SLAB_MINALIGN =ARCH_SLAB_MINALIGN ; -const gfp_t BINDINGS_GFP_KERNEL =GFP_KERNEL ; -const gfp_t BINDINGS___GFP_ZERO = __GFP_ZERO; +const size_t RUST_BINDING_ARCH_SLAB_MINALIGN =ARCH_SLAB_MINALIGN ; +const gfp_t RUST_BINDING_GFP_KERNEL =GFP_KERNEL ; +const gfp_t RUST_BINDING___GFP_ZERO = __GFP_ZERO;
rust/bindings/lib.rs +0 -3
@@ -48,6 +48,3 @@ mod bindings_helper { }pub use bindings_raw::*; - -pub const GFP_KERNEL :gfp_t =BINDINGS_GFP_KERNEL ; -pub const __GFP_ZERO:gfp_t =BINDINGS___GFP_ZERO ;
rust/kernel/allocator.rs +1 -1
@@ -21,7 +21,7 @@ unsafe fn krealloc_aligned(ptr: *mut u8, new_layout: Layout, flags: bindings::gflet mut size = layout.size (); -if layout.align () > bindings::BINDINGS_ARCH_SLAB_MINALIGN { +if layout.align () > bindings::ARCH_SLAB_MINALIGN {// The alignment requirement exceeds the slab guarantee, thus try to enlarge the size // to use the "power-of-two" size/alignment guarantee (see comments in `kmalloc()` for // more information).
Signature · 1 line
2.40.1
Quoted 18 lines · Gary Guo
Current for consts that bindgen don't recognise, we define a helper constant with
const <TYPE> BINDINGS_<NAME> = <NAME>;in
bindings_helper.hand then we putpub const <NAME>: <TYPE> = BINDINGS_<NAME>;in
bindings/lib.rs. This is fine that we currently only have 3 constants that are defined this way, but is going to be more annoying when more constants are added since every new constant needs to be defined in two places.This patch changes the way we define constant helpers to
const <TYPE> RUST_BINDING_<NAME> = <NAME>;
Quoted 7 lines
and then use
sedto postprocess Rust code by generated by bindgen to remove the distinct prefix, so user of the binding crate can refer to the name directly.Signed-off-by Gary Guo <[email protected]>---
[...]
This one is a must have.
Quoted 14 lines · Gary Guo
Current for consts that bindgen don't recognise, we define a helper constant with
const <TYPE> BINDINGS_<NAME> = <NAME>;in
bindings_helper.hand then we putpub const <NAME>: <TYPE> = BINDINGS_<NAME>;in
bindings/lib.rs. This is fine that we currently only have 3 constants that are defined this way, but is going to be more annoying when more constants are added since every new constant needs to be defined in two places.
Quoted 7 lines
This patch changes the way we define constant helpers to
const <TYPE> RUST_BINDING_<NAME> = <NAME>;and then use
sedto postprocess Rust code by generated by bindgen to remove the distinct prefix, so user of the binding crate can refer to the name directly.
Maybe it would make sense to use a less generic name for this sed replacement? E.g., maybe RUST_CONST_HELPER_ or something that's less likely to overlap with things that are not constants.
Alice
Quoted 35 lines · Gary Guo
Current for consts that bindgen don't recognise, we define a helper constant with
const <TYPE> BINDINGS_<NAME> = <NAME>;in
bindings_helper.hand then we putpub const <NAME>: <TYPE> = BINDINGS_<NAME>;in
bindings/lib.rs. This is fine that we currently only have 3 constants that are defined this way, but is going to be more annoying when more constants are added since every new constant needs to be defined in two places.This patch changes the way we define constant helpers to
const <TYPE> RUST_BINDING_<NAME> = <NAME>;and then use
sedto postprocess Rust code by generated by bindgen to remove the distinct prefix, so user of the binding crate can refer to the name directly.Signed-off-by Gary Guo <[email protected]>--- rust/Makefile | 2 ++ rust/bindings/bindings_helper.h | 6 +++--- rust/bindings/lib.rs | 3 --- rust/kernel/allocator.rs | 2 +- 4 files changed, 6 insertions(+), 7 deletions(-)rust/Makefile +0
-0
rust/Makefile quiet_cmd_bindgen = BINDGEN $@ · line 343
Quoted 5 lines
rust/Makefile +2
-0@@ -339,6 +339,8 @@ quiet_cmd_bindgen = BINDGEN $@ $(obj)/bindings/bindings_generated.rs: private bindgen_target_flags = \ $(shell grep -v '^#\|^$$' $(srctree)/$(src)/bindgen_parameters) +$(obj)/bindings/bindings_generated.rs: private bindgen_target_extra = ; \ + sed -Ei 's/pub const RUST_BINDING_([a-zA-Z0-9_]*)/pub const \1/g' $@
Would it make sense to use ^pub const...?
I also agree with Alice that we could use a less generic name as the prefix.
With those things fixed:
Signature · 2 lines
Cheers, Benno
Quoted 44 lines
rust/Makefile +0
-0$(obj)/bindings/bindings_generated.rs: $(src)/bindings/bindings_helper.h \ $(src)/bindgen_parameters FORCE $(call if_changed_dep,bindgen)rust/bindings/bindings_helper.h +3
-3@@ -15,6 +15,6 @@#include <linux/workqueue.h> /* `bindgen` gets confused at certain things. */ -const size_t BINDINGS_ARCH_SLAB_MINALIGN =ARCH_SLAB_MINALIGN ; -const gfp_t BINDINGS_GFP_KERNEL =GFP_KERNEL ; -const gfp_t BINDINGS___GFP_ZERO = __GFP_ZERO; +const size_t RUST_BINDING_ARCH_SLAB_MINALIGN =ARCH_SLAB_MINALIGN ; +const gfp_t RUST_BINDING_GFP_KERNEL =GFP_KERNEL ; +const gfp_t RUST_BINDING___GFP_ZERO = __GFP_ZERO;rust/bindings/lib.rs +0
-3@@ -48,6 +48,3 @@ mod bindings_helper { }pub use bindings_raw::*; - -pub const GFP_KERNEL :gfp_t =BINDINGS_GFP_KERNEL ; -pub const __GFP_ZERO:gfp_t =BINDINGS___GFP_ZERO ;rust/kernel/allocator.rs +1
-2@@ -21,7 +21,7 @@ unsafe fn krealloc_aligned(ptr: *mut u8, new_layout: Layout, flags: bindings::gflet mut size = layout.size (); -if layout.align () > bindings::BINDINGS_ARCH_SLAB_MINALIGN { +if layout.align () > bindings::ARCH_SLAB_MINALIGN {// The alignment requirement exceeds the slab guarantee, thus try to enlarge the size // to use the "power-of-two" size/alignment guarantee (see comments in `kmalloc()` for // more information). --2.40.1
Quoted 16 lines · Gary Guo
Current for consts that bindgen don't recognise, we define a helper constant with
const <TYPE> BINDINGS_<NAME> = <NAME>;in
bindings_helper.hand then we putpub const <NAME>: <TYPE> = BINDINGS_<NAME>;in
bindings/lib.rs. This is fine that we currently only have 3 constants that are defined this way, but is going to be more annoying when more constants are added since every new constant needs to be defined in two places.This patch changes the way we define constant helpers to
Quoted 7 lines
const <TYPE> RUST_BINDING_<NAME> = <NAME>;and then use
sedto postprocess Rust code by generated by bindgen to remove the distinct prefix, so user of the binding crate can refer to the name directly.Signed-off-by Gary Guo <[email protected]>
Quoted 58 lines
--- rust/Makefile | 2 ++ rust/bindings/bindings_helper.h | 6 +++--- rust/bindings/lib.rs | 3 --- rust/kernel/allocator.rs | 2 +- 4 files changed, 6 insertions(+), 7 deletions(-)rust/Makefile +2
-0@@ -339,6 +339,8 @@ quiet_cmd_bindgen = BINDGEN $@ $(obj)/bindings/bindings_generated.rs: private bindgen_target_flags = \ $(shell grep -v '^#\|^$$' $(srctree)/$(src)/bindgen_parameters) +$(obj)/bindings/bindings_generated.rs: private bindgen_target_extra = ; \ + sed -Ei 's/pub const RUST_BINDING_([a-zA-Z0-9_]*)/pub const \1/g' $@ $(obj)/bindings/bindings_generated.rs: $(src)/bindings/bindings_helper.h \ $(src)/bindgen_parameters FORCE $(call if_changed_dep,bindgen)rust/bindings/bindings_helper.h +3
-3@@ -15,6 +15,6 @@#include <linux/workqueue.h> /* `bindgen` gets confused at certain things. */ -const size_t BINDINGS_ARCH_SLAB_MINALIGN =ARCH_SLAB_MINALIGN ; -const gfp_t BINDINGS_GFP_KERNEL =GFP_KERNEL ; -const gfp_t BINDINGS___GFP_ZERO = __GFP_ZERO; +const size_t RUST_BINDING_ARCH_SLAB_MINALIGN =ARCH_SLAB_MINALIGN ; +const gfp_t RUST_BINDING_GFP_KERNEL =GFP_KERNEL ; +const gfp_t RUST_BINDING___GFP_ZERO = __GFP_ZERO;rust/bindings/lib.rs +0
-3@@ -48,6 +48,3 @@ mod bindings_helper { }pub use bindings_raw::*; - -pub const GFP_KERNEL :gfp_t =BINDINGS_GFP_KERNEL ; -pub const __GFP_ZERO:gfp_t =BINDINGS___GFP_ZERO ;rust/kernel/allocator.rs +1
-1@@ -21,7 +21,7 @@ unsafe fn krealloc_aligned(ptr: *mut u8, new_layout: Layout, flags: bindings::gflet mut size = layout.size (); -if layout.align () > bindings::BINDINGS_ARCH_SLAB_MINALIGN { +if layout.align () > bindings::ARCH_SLAB_MINALIGN {// The alignment requirement exceeds the slab guarantee, thus try to enlarge the size // to use the "power-of-two" size/alignment guarantee (see comments in `kmalloc()` for // more information).