Skip to content

Commit

Permalink
Made CWriter use assoc type for converter, moved all impls as default…
Browse files Browse the repository at this point in the history
… to Trait
  • Loading branch information
Pspritechologist authored and ralfbiedert committed Nov 27, 2024
1 parent f58e30e commit 937a45d
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 44 deletions.
58 changes: 16 additions & 42 deletions crates/backend_c/src/converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,42 +21,6 @@ impl Converter {
pub trait CTypeConverter {
fn config(&self) -> &Config;

/// Converts a primitive (Rust) type to a native C# type name, e.g., `f32` to `float`.
fn primitive_to_typename(&self, x: &PrimitiveType) -> String;

/// Converts a Rust enum name such as `Error` to a C# enum name `Error`.
fn enum_to_typename(&self, x: &EnumType) -> String;

fn enum_variant_to_name(&self, the_enum: &EnumType, x: &Variant) -> String;

/// TODO Converts an opaque Rust struct `Context` to a C# struct ``.
fn opaque_to_typename(&self, opaque: &OpaqueType) -> String;

/// Converts an Rust struct name `Vec2` to a C# struct name `Vec2`.
fn composite_to_typename(&self, x: &CompositeType) -> String;

/// Converts an Rust `fn()` to a C# delegate name such as `InteropDelegate`.
fn fnpointer_to_typename(&self, x: &FnPointerType) -> String;

fn named_callback_to_typename(&self, x: &NamedCallback) -> String {
format!("{}{}", self.config().prefix, x.name().to_naming_style(&self.config().type_naming))
}

/// Converts the `u32` part in a Rust paramter `x: u32` to a C# equivalent. Might convert pointers to `out X` or `ref X`.
fn to_type_specifier(&self, x: &CType) -> String;

fn const_name_to_name(&self, x: &Constant) -> String;

fn constant_value_to_value(&self, value: &ConstantValue) -> String;

fn function_name_to_c_name(&self, function: &Function) -> String;
}

impl CTypeConverter for Converter {
fn config(&self) -> &Config {
&self.config
}

fn primitive_to_typename(&self, x: &PrimitiveType) -> String {
match x {
PrimitiveType::Void => "void".to_string(),
Expand All @@ -75,32 +39,36 @@ impl CTypeConverter for Converter {
}

fn enum_to_typename(&self, x: &EnumType) -> String {
format!("{}{}", self.config().prefix, x.rust_name()).to_naming_style(&self.config.type_naming)
format!("{}{}", self.config().prefix, x.rust_name()).to_naming_style(&self.config().type_naming)
}

fn enum_variant_to_name(&self, the_enum: &EnumType, x: &Variant) -> String {
format!(
"{}{}_{}",
self.config().prefix,
the_enum.rust_name().to_naming_style(&self.config.type_naming),
the_enum.rust_name().to_naming_style(&self.config().type_naming),
x.name()
)
.to_naming_style(&self.config.enum_variant_naming)
.to_naming_style(&self.config().enum_variant_naming)
}

fn opaque_to_typename(&self, x: &OpaqueType) -> String {
format!("{}{}", self.config().prefix, x.rust_name()).to_naming_style(&self.config.type_naming)
format!("{}{}", self.config().prefix, x.rust_name()).to_naming_style(&self.config().type_naming)
}

fn composite_to_typename(&self, x: &CompositeType) -> String {
format!("{}{}", self.config().prefix, x.rust_name()).to_naming_style(&self.config.type_naming)
format!("{}{}", self.config().prefix, x.rust_name()).to_naming_style(&self.config().type_naming)
}

fn fnpointer_to_typename(&self, x: &FnPointerType) -> String {
let prefixed = format!("{}fptr", self.config().prefix);
[prefixed, safe_name(&x.internal_name())].join("_")
}

fn named_callback_to_typename(&self, x: &NamedCallback) -> String {
format!("{}{}", self.config().prefix, x.name().to_naming_style(&self.config().type_naming))
}

fn to_type_specifier(&self, x: &CType) -> String {
match x {
CType::Primitive(x) => self.primitive_to_typename(x),
Expand All @@ -119,7 +87,7 @@ impl CTypeConverter for Converter {
}

fn const_name_to_name(&self, x: &Constant) -> String {
format!("{}{}", self.config().prefix, x.name()).to_naming_style(&self.config.const_naming)
format!("{}{}", self.config().prefix, x.name()).to_naming_style(&self.config().const_naming)
}

fn constant_value_to_value(&self, value: &ConstantValue) -> String {
Expand All @@ -144,3 +112,9 @@ impl CTypeConverter for Converter {
function.name().to_string()
}
}

impl CTypeConverter for Converter {
fn config(&self) -> &Config {
&self.config
}
}
2 changes: 2 additions & 0 deletions crates/backend_c/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ impl Interop for Generator {
}

impl CWriter for Generator {
type Converter = Converter;

fn config(&self) -> &Config {
&self.config
}
Expand Down
5 changes: 3 additions & 2 deletions crates/backend_c/src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,20 @@ use interoptopus::{Error, Inventory};

use crate::config::{CDocumentationStyle, CFunctionStyle, CIndentationStyle, ToNamingStyle};
use crate::converter::CTypeConverter;
use crate::converter::Converter;
use crate::Config;

/// Writes the C file format, `impl` this trait to customize output.
pub trait CWriter {
type Converter: CTypeConverter;

/// Returns the user config.
fn config(&self) -> &Config;

/// Returns the library to produce bindings for.
fn inventory(&self) -> &Inventory;

/// Returns the library to produce bindings for.
fn converter(&self) -> &Converter;
fn converter(&self) -> &Self::Converter;

fn write_custom_defines(&self, w: &mut IndentWriter) -> Result<(), Error> {
indented!(w, "{}", &self.config().custom_defines)
Expand Down

0 comments on commit 937a45d

Please sign in to comment.