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

physx-sys: Emit default values in FFI bindings for literal values #221

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
FFI default values: nicer float formatting
  • Loading branch information
slvmnd committed Nov 15, 2023
commit 062565063d500b170db5fbef7abe4400e98e867f
23 changes: 14 additions & 9 deletions physx-sys/pxbind/src/consumer/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct Function {
pub struct Param<'ast> {
pub name: Cow<'ast, str>,
pub kind: QualType<'ast>,
pub default_value: Option<&'ast str>,
pub default_value: Option<String>,
}

impl<'ast> Param<'ast> {
Expand Down Expand Up @@ -189,16 +189,21 @@ impl<'ast> super::AstConsumer<'ast> {
.filter_map(|inn| {
if let Item::ParmVarDecl(param) = &inn.kind {
if inn.inner.len() > 0 {
let default_value: Option<&str> = match &inn.inner[0].kind {
Item::IntegerLiteral { value, kind: _ } => Some(value),
let default_value: Option<String> = match &inn.inner[0].kind {
Item::IntegerLiteral { value, kind: _ } => Some(value.to_owned()),
Item::FloatingLiteral { value, kind: _ } => {
Some(if value == "1.00999999" { "1.01" } else { value })
}
Item::StringLiteral { value, kind: _ } => Some(value),
Item::UserDefinedLiteral { value, kind: _ } => Some(value),
Item::CXXBoolLiteralExpr { value, kind: _ } => {
Some(if *value { "true" } else { "false" })
// Clang ast-dump float formatting is not human-friendly
// i.e. "1.01" formats as "1.00999999"
// rust format! fixes this
value.parse::<f32>().ok().map(|value| format!("{value}"))
}
Item::StringLiteral { value, kind: _ } => Some(value.to_owned()),
Item::UserDefinedLiteral { value, kind: _ } => Some(value.to_owned()),
Item::CXXBoolLiteralExpr { value, kind: _ } => Some(if *value {
"true".to_owned()
} else {
"false".to_owned()
}),
_ => None,
};

Expand Down