Skip to content

Commit 7967f1e

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 7967f1e

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

bindgen/codegen/mod.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3901,6 +3901,53 @@ 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+
}
3926+
.fmt(f)
3927+
}
3928+
}
3929+
3930+
impl FromStr for RustEdition {
3931+
type Err = std::io::Error;
3932+
3933+
/// Create a `RustEdition` from a string.
3934+
fn from_str(s: &str) -> Result<Self, Self::Err> {
3935+
match s {
3936+
"2015" => Ok(RustEdition::Rust2015),
3937+
"2018" => Ok(RustEdition::Rust2018),
3938+
"2021" => Ok(RustEdition::Rust2021),
3939+
"2024" => Ok(RustEdition::Rust2024),
3940+
_ => Err(std::io::Error::new(
3941+
std::io::ErrorKind::InvalidInput,
3942+
concat!(
3943+
"Got an invalid language edition. Accepted values ",
3944+
"are '2015', '2018', '2021', and '2024'"
3945+
),
3946+
)),
3947+
}
3948+
}
3949+
}
3950+
39043951
/// Enum for how aliases should be translated.
39053952
#[derive(Copy, Clone, PartialEq, Eq, Debug, Default)]
39063953
pub enum AliasVariation {

bindgen/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ use ir::item::Item;
6464
use options::BindgenOptions;
6565
use parse::ParseError;
6666

67+
use crate::codegen::RustEdition;
6768
use std::borrow::Cow;
6869
use std::collections::hash_map::Entry;
6970
use std::env;
@@ -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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pub(crate) mod cli;
1010
use crate::callbacks::ParseCallbacks;
1111
use crate::codegen::{
1212
AliasVariation, EnumVariation, MacroTypeVariation, NonCopyUnionStyle,
13+
RustEdition,
1314
};
1415
use crate::deps::DepfileSpec;
1516
use crate::features::{RustFeatures, RustTarget};
@@ -1594,6 +1595,22 @@ options! {
15941595
as_args: |value, args| (!value).as_args(args, "--no-prepend-enum-name"),
15951596
},
15961597
/// Version of the Rust compiler to target.
1598+
rust_edition: RustEdition {
1599+
methods: {
1600+
/// Specify the Rust edition version.
1601+
///
1602+
/// The default edition is the latest stable Rust version.
1603+
pub fn rust_edition(mut self, rust_edition: RustEdition) -> Self {
1604+
self.options.set_rust_edition(rust_edition);
1605+
self
1606+
}
1607+
},
1608+
as_args: |rust_edition, args| {
1609+
args.push("--rust-edition".to_owned());
1610+
args.push(rust_edition.to_string());
1611+
},
1612+
},
1613+
/// Version of the Rust compiler to target.
15971614
rust_target: RustTarget {
15981615
methods: {
15991616
/// Specify the Rust target version.

0 commit comments

Comments
 (0)