Skip to content

Commit dc2df75

Browse files
committed
Add rust language editions support
While not used in this PR, this will allow different code generation depending on the language target, e.g. for rust-lang#2996 Introduce a new `--rust-edition` parameter with allowed values 2015, 2018, 2021, and 2024.
1 parent d2e30fb commit dc2df75

File tree

3 files changed

+69
-3
lines changed

3 files changed

+69
-3
lines changed

bindgen/codegen/mod.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3901,6 +3901,52 @@ impl std::str::FromStr for MacroTypeVariation {
39013901
}
39023902
}
39033903

3904+
/// Enum for the edition of Rust language to use.
3905+
#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
3906+
pub enum RustEdition {
3907+
/// Rust 2015 language edition
3908+
Rust2015,
3909+
/// Rust 2018 language edition
3910+
#[default]
3911+
Rust2018,
3912+
/// Rust 2021 language edition
3913+
Rust2021,
3914+
/// Rust 2024 language edition
3915+
Rust2024,
3916+
}
3917+
3918+
impl fmt::Display for RustEdition {
3919+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
3920+
match self {
3921+
Self::Rust2015 => "2015",
3922+
Self::Rust2018 => "2018",
3923+
Self::Rust2021 => "2021",
3924+
Self::Rust2024 => "2024",
3925+
}.fmt(f)
3926+
}
3927+
}
3928+
3929+
impl FromStr for RustEdition {
3930+
type Err = std::io::Error;
3931+
3932+
/// Create a `RustEdition` from a string.
3933+
fn from_str(s: &str) -> Result<Self, Self::Err> {
3934+
match s {
3935+
"2015" => Ok(RustEdition::Rust2015),
3936+
"2018" => Ok(RustEdition::Rust2018),
3937+
"2021" => Ok(RustEdition::Rust2021),
3938+
"2024" => Ok(RustEdition::Rust2024),
3939+
_ => Err(std::io::Error::new(
3940+
std::io::ErrorKind::InvalidInput,
3941+
concat!(
3942+
"Got an invalid language edition. Accepted values ",
3943+
"are '2015', '2018', '2021', and '2024'"
3944+
),
3945+
)),
3946+
}
3947+
}
3948+
}
3949+
39043950
/// Enum for how aliases should be translated.
39053951
#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
39063952
pub enum AliasVariation {

bindgen/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ use std::process::{Command, Stdio};
7575
use std::rc::Rc;
7676
use std::str::FromStr;
7777
use std::sync::{Arc, OnceLock};
78+
use crate::codegen::RustEdition;
7879

7980
// Some convenient typedefs for a fast hash map and hash set.
8081
type HashMap<K, V> = rustc_hash::FxHashMap<K, V>;
@@ -528,6 +529,11 @@ impl BindgenOptions {
528529
}
529530
}
530531

532+
/// Update rust edition version
533+
pub fn set_rust_edition(&mut self, rust_edition: RustEdition) {
534+
self.rust_edition = rust_edition;
535+
}
536+
531537
/// Update rust target version
532538
pub fn set_rust_target(&mut self, rust_target: RustTarget) {
533539
self.rust_target = rust_target;

bindgen/options/mod.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ mod as_args;
88
pub(crate) mod cli;
99

1010
use crate::callbacks::ParseCallbacks;
11-
use crate::codegen::{
12-
AliasVariation, EnumVariation, MacroTypeVariation, NonCopyUnionStyle,
13-
};
11+
use crate::codegen::{AliasVariation, EnumVariation, MacroTypeVariation, NonCopyUnionStyle, RustEdition};
1412
use crate::deps::DepfileSpec;
1513
use crate::features::{RustFeatures, RustTarget};
1614
use crate::regex_set::RegexSet;
@@ -1594,6 +1592,22 @@ options! {
15941592
as_args: |value, args| (!value).as_args(args, "--no-prepend-enum-name"),
15951593
},
15961594
/// Version of the Rust compiler to target.
1595+
rust_edition: RustEdition {
1596+
methods: {
1597+
/// Specify the Rust edition version.
1598+
///
1599+
/// The default edition is the latest stable Rust version.
1600+
pub fn rust_edition(mut self, rust_edition: RustEdition) -> Self {
1601+
self.options.set_rust_edition(rust_edition);
1602+
self
1603+
}
1604+
},
1605+
as_args: |rust_edition, args| {
1606+
args.push("--rust-edition".to_owned());
1607+
args.push(rust_edition.to_string());
1608+
},
1609+
},
1610+
/// Version of the Rust compiler to target.
15971611
rust_target: RustTarget {
15981612
methods: {
15991613
/// Specify the Rust target version.

0 commit comments

Comments
 (0)