Skip to content

Commit 1d9f9a6

Browse files
committed
const_generic to option
1 parent 552dcea commit 1d9f9a6

File tree

8 files changed

+106
-107
lines changed

8 files changed

+106
-107
lines changed

.github/workflows/ci.yml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ jobs:
2525

2626
FEATURES: [""]
2727

28+
OPTIONS: [""]
29+
2830
include:
2931
# Test MSRV
3032
- rust: 1.40.0
@@ -37,7 +39,8 @@ jobs:
3739
VENDOR: RISC-V
3840
TARGET: x86_64-unknown-linux-gnu
3941
TRAVIS_OS_NAME: linux
40-
FEATURES: "strict,const-generic"
42+
FEATURES: "strict"
43+
OPTIONS: "--const_generic"
4144

4245
# Use nightly for architectures which don't support stable
4346
- rust: nightly
@@ -73,4 +76,4 @@ jobs:
7376
override: true
7477
components: rustfmt
7578
- name: Run CI script for ${{ matrix.VENDOR }} under ${{ matrix.rust }}
76-
run: TARGET=${{ matrix.TARGET }} VENDOR=${{ matrix.VENDOR }} TRAVIS_OS_NAME=${{ matrix.TRAVIS_OS_NAME }} FEATURES=${{ matrix.FEATURES }} bash ci/script.sh
79+
run: TARGET=${{ matrix.TARGET }} VENDOR=${{ matrix.VENDOR }} TRAVIS_OS_NAME=${{ matrix.TRAVIS_OS_NAME }} FEATURES=${{ matrix.FEATURES }} OPTIONS=${{ matrix.OPTIONS }} bash ci/script.sh

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
99

1010
### Changed
1111

12+
- [breaking-change] move `const_generic` from features to options
1213
- use `Config` to pass options over all render levels
1314
- Use register iterator from `svd-parser`
1415
- rm unneeded `core::convert::` prefix on `From`

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,3 @@ features = ["full","extra-traits"]
5252

5353
[features]
5454
strict = ["svd-parser/strict"]
55-
const-generic = []

ci/script.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ test_svd() {
1010
# NOTE we care about errors in svd2rust, but not about errors / warnings in rustfmt
1111
local cwd=$(pwd)
1212
pushd $td
13-
RUST_BACKTRACE=1 $cwd/target/$TARGET/release/svd2rust -i ${1}.svd
13+
RUST_BACKTRACE=1 $cwd/target/$TARGET/release/svd2rust -i $OPTIONS ${1}.svd
1414

1515
mv lib.rs src/lib.rs
1616

src/generate/register.rs

Lines changed: 41 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ pub fn fields(
269269
mod_items: &mut TokenStream,
270270
r_impl_items: &mut TokenStream,
271271
w_impl_items: &mut TokenStream,
272-
_config: &Config,
272+
config: &Config,
273273
) -> Result<()> {
274274
let span = Span::call_site();
275275
let can_read = [Access::ReadOnly, Access::ReadWriteOnce, Access::ReadWrite].contains(&access);
@@ -569,7 +569,6 @@ pub fn fields(
569569
if can_write {
570570
let new_pc_aw = Ident::new(&(name_pc.clone() + "_AW"), span);
571571
let name_pc_w = Ident::new(&(name_pc.clone() + "_W"), span);
572-
#[cfg(feature = "const-generic")]
573572
let name_pc_cgw = Ident::new(&(name_pc.clone() + "_CGW"), span);
574573

575574
let mut proxy_items = TokenStream::new();
@@ -648,7 +647,6 @@ pub fn fields(
648647
}
649648

650649
let mut proxy_items_fa = TokenStream::new();
651-
#[cfg(feature = "const-generic")]
652650
let mut proxy_items_cg = TokenStream::new();
653651
if field_dim.is_some() {
654652
proxy_items_fa.extend(quote! {
@@ -659,15 +657,16 @@ pub fn fields(
659657
self.w
660658
}
661659
});
662-
#[cfg(feature="const-generic")]
663-
proxy_items_cg.extend(quote! {
664-
///Writes raw bits to the field
665-
#inline
666-
pub #unsafety fn #bits(self, value: #fty) -> &'a mut W {
667-
self.w.bits = (self.w.bits & !(#hexmask << O)) | ((value as #rty & #hexmask) << O);
668-
self.w
669-
}
670-
});
660+
if config.const_generic {
661+
proxy_items_cg.extend(quote! {
662+
///Writes raw bits to the field
663+
#inline
664+
pub #unsafety fn #bits(self, value: #fty) -> &'a mut W {
665+
self.w.bits = (self.w.bits & !(#hexmask << O)) | ((value as #rty & #hexmask) << O);
666+
self.w
667+
}
668+
});
669+
}
671670
} else {
672671
proxy_items.extend(if offset != 0 {
673672
let offset = &util::unsuffixed(offset);
@@ -691,11 +690,9 @@ pub fn fields(
691690
});
692691
}
693692

694-
#[cfg(feature = "const-generic")]
695693
let mut cgdoc = String::new();
696694
let doc = if let Some((_, _, _, _, suffixes_str)) = &field_dim {
697-
#[cfg(feature = "const-generic")]
698-
{
695+
if config.const_generic {
699696
cgdoc = format!(
700697
"Fields `{}` const generic writer - {}",
701698
util::replace_suffix(&f.name, suffixes_str),
@@ -725,18 +722,19 @@ pub fn fields(
725722
}
726723
});
727724

728-
#[cfg(feature = "const-generic")]
729-
mod_items.extend(quote! {
730-
#[doc = #cgdoc]
731-
pub struct #name_pc_cgw<'a, const O: usize> {
732-
w: &'a mut W,
733-
}
725+
if config.const_generic {
726+
mod_items.extend(quote! {
727+
#[doc = #cgdoc]
728+
pub struct #name_pc_cgw<'a, const O: usize> {
729+
w: &'a mut W,
730+
}
734731

735-
impl<'a, const O: usize> #name_pc_cgw<'a, O> {
736-
#proxy_items
737-
#proxy_items_cg
738-
}
739-
});
732+
impl<'a, const O: usize> #name_pc_cgw<'a, O> {
733+
#proxy_items
734+
#proxy_items_cg
735+
}
736+
});
737+
}
740738
} else {
741739
mod_items.extend(quote! {
742740
#[doc = #doc]
@@ -771,22 +769,23 @@ pub fn fields(
771769
&suffix,
772770
);
773771
let sub_offset = util::unsuffixed(sub_offset as u64);
774-
#[cfg(not(feature = "const-generic"))]
775-
w_impl_items.extend(quote! {
776-
#[doc = #doc]
777-
#inline
778-
pub fn #name_sc_n(&mut self) -> #name_pc_w {
779-
#name_pc_w { w: self, offset: #sub_offset }
780-
}
781-
});
782-
#[cfg(feature = "const-generic")]
783-
w_impl_items.extend(quote! {
784-
#[doc = #doc]
785-
#inline
786-
pub fn #name_sc_n(&mut self) -> #name_pc_cgw<#sub_offset> {
787-
#name_pc_cgw { w: self }
788-
}
789-
});
772+
if !config.const_generic {
773+
w_impl_items.extend(quote! {
774+
#[doc = #doc]
775+
#inline
776+
pub fn #name_sc_n(&mut self) -> #name_pc_w {
777+
#name_pc_w { w: self, offset: #sub_offset }
778+
}
779+
});
780+
} else {
781+
w_impl_items.extend(quote! {
782+
#[doc = #doc]
783+
#inline
784+
pub fn #name_sc_n(&mut self) -> #name_pc_cgw<#sub_offset> {
785+
#name_pc_cgw { w: self }
786+
}
787+
});
788+
}
790789
}
791790
} else {
792791
let doc = description_with_bits(&description, offset, width);

src/lib.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -510,17 +510,11 @@ pub enum SvdError {
510510
}
511511

512512
/// Generates rust code for the specified svd content.
513-
pub fn generate(xml: &str, target: Target, nightly: bool) -> Result<Generation> {
513+
pub fn generate(xml: &str, config: &Config) -> Result<Generation> {
514514
use std::fmt::Write;
515515

516516
let device = svd::parse(xml)?;
517517
let mut device_x = String::new();
518-
let config = Config {
519-
target,
520-
nightly,
521-
generic_mod: false,
522-
make_mod: false,
523-
};
524518
let items =
525519
generate::device::render(&device, &config, &mut device_x).or(Err(SvdError::Render))?;
526520

src/main.rs

Lines changed: 56 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -18,55 +18,59 @@ use crate::util::{build_rs, Config, Target};
1818
fn run() -> Result<()> {
1919
use std::io::Read;
2020

21-
let matches = App::new("svd2rust")
22-
.about("Generate a Rust API from SVD files")
23-
.arg(
24-
Arg::with_name("input")
25-
.help("Input SVD file")
26-
.short("i")
27-
.takes_value(true)
28-
.value_name("FILE"),
29-
)
30-
.arg(
31-
Arg::with_name("target")
32-
.long("target")
33-
.help("Target architecture")
34-
.takes_value(true)
35-
.value_name("ARCH"),
36-
)
37-
.arg(
38-
Arg::with_name("nightly_features")
39-
.long("nightly")
40-
.help("Enable features only available to nightly rustc"),
41-
)
42-
.arg(
43-
Arg::with_name("generic_mod")
44-
.long("generic_mod")
45-
.short("g")
46-
.help("Push generic mod in separate file"),
47-
)
48-
.arg(
49-
Arg::with_name("make_mod")
50-
.long("make_mod")
51-
.short("m")
52-
.help("Create mod.rs instead of lib.rs, without inner attributes"),
53-
)
54-
.arg(
55-
Arg::with_name("log_level")
56-
.long("log")
57-
.short("l")
58-
.help(&format!(
59-
"Choose which messages to log (overrides {})",
60-
env_logger::DEFAULT_FILTER_ENV
61-
))
62-
.takes_value(true)
63-
.possible_values(&["off", "error", "warn", "info", "debug", "trace"]),
64-
)
65-
.version(concat!(
66-
env!("CARGO_PKG_VERSION"),
67-
include_str!(concat!(env!("OUT_DIR"), "/commit-info.txt"))
68-
))
69-
.get_matches();
21+
let matches =
22+
App::new("svd2rust")
23+
.about("Generate a Rust API from SVD files")
24+
.arg(
25+
Arg::with_name("input")
26+
.help("Input SVD file")
27+
.short("i")
28+
.takes_value(true)
29+
.value_name("FILE"),
30+
)
31+
.arg(
32+
Arg::with_name("target")
33+
.long("target")
34+
.help("Target architecture")
35+
.takes_value(true)
36+
.value_name("ARCH"),
37+
)
38+
.arg(
39+
Arg::with_name("nightly_features")
40+
.long("nightly")
41+
.help("Enable features only available to nightly rustc"),
42+
)
43+
.arg(Arg::with_name("const_generic").long("const_generic").help(
44+
"Use const generics to generate writers for same fields with different offsets",
45+
))
46+
.arg(
47+
Arg::with_name("generic_mod")
48+
.long("generic_mod")
49+
.short("g")
50+
.help("Push generic mod in separate file"),
51+
)
52+
.arg(
53+
Arg::with_name("make_mod")
54+
.long("make_mod")
55+
.short("m")
56+
.help("Create mod.rs instead of lib.rs, without inner attributes"),
57+
)
58+
.arg(
59+
Arg::with_name("log_level")
60+
.long("log")
61+
.short("l")
62+
.help(&format!(
63+
"Choose which messages to log (overrides {})",
64+
env_logger::DEFAULT_FILTER_ENV
65+
))
66+
.takes_value(true)
67+
.possible_values(&["off", "error", "warn", "info", "debug", "trace"]),
68+
)
69+
.version(concat!(
70+
env!("CARGO_PKG_VERSION"),
71+
include_str!(concat!(env!("OUT_DIR"), "/commit-info.txt"))
72+
))
73+
.get_matches();
7074

7175
setup_logging(&matches);
7276

@@ -94,16 +98,14 @@ fn run() -> Result<()> {
9498

9599
let device = svd::parse(xml)?;
96100

97-
let nightly = matches.is_present("nightly_features");
98-
99-
let generic_mod = matches.is_present("generic_mod");
100101
let make_mod = matches.is_present("make_mod");
101102

102103
let config = Config {
103104
target,
104-
nightly,
105-
generic_mod,
105+
nightly: matches.is_present("nightly_features"),
106+
generic_mod: matches.is_present("generic_mod"),
106107
make_mod,
108+
const_generic: matches.is_present("const_generic"),
107109
};
108110

109111
let mut device_x = String::new();

src/util.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ pub struct Config {
1919
pub nightly: bool,
2020
pub generic_mod: bool,
2121
pub make_mod: bool,
22+
pub const_generic: bool,
2223
}
2324

2425
#[allow(clippy::upper_case_acronyms)]

0 commit comments

Comments
 (0)