generated from ewpratten/rust-template
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
170 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
pub mod raylib_api; | ||
pub mod type_xlat; | ||
pub mod enums; | ||
pub mod colors; | ||
pub mod colors; | ||
pub mod structs; | ||
pub mod profiling; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
use std::{fs::File, io::{Read, Write}}; | ||
|
||
use regex::Regex; | ||
|
||
/// Finds all `pub fn` in a file and wraps them with `#[profiling::function]` | ||
pub fn auto_profile_exported_fns(rust_file_path: &str) { | ||
Check warning on line 6 in build/wrap/profiling.rs GitHub Actions / Rust project (ubuntu-latest)
|
||
// Open the file | ||
let mut file = File::open(rust_file_path).unwrap(); | ||
let mut contents = String::new(); | ||
file.read_to_string(&mut contents).unwrap(); | ||
|
||
// Find all `pub fn` and wrap them with `#[profiling::function]` | ||
let exported_fn_re = Regex::new(r"\s{4}pub fn").unwrap(); | ||
let mut new_contents = String::new(); | ||
for line in contents.lines() { | ||
if exported_fn_re.is_match(line) { | ||
new_contents.push_str(&format!(" #[profiling::function]\n{}\n", line)); | ||
} else { | ||
new_contents.push_str(&format!("{}\n", line)); | ||
} | ||
} | ||
|
||
// Re-write the file | ||
let mut file = File::create(rust_file_path).unwrap(); | ||
file.write_all(new_contents.as_bytes()).unwrap(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
use super::{raylib_api::RayLibApiDefinition, type_xlat::translate_c_type_to_rust}; | ||
|
||
|
||
pub fn wrap_exposed_structs(api_defs: RayLibApiDefinition) { | ||
// Allocate an output buffer for lines | ||
let mut lines = Vec::new(); | ||
|
||
// Handle each struct | ||
for st in api_defs.structs { | ||
// Write a doc comment with raylib's provided struct description | ||
lines.push("".to_string()); | ||
lines.push(format!("/// {}", st.description)); | ||
|
||
// Write the struct definition | ||
lines.push(format!("#[repr(C)]")); | ||
lines.push(format!("pub struct {} {{", st.name)); | ||
|
||
// Write each field | ||
for field in st.fields { | ||
// Write a doc comment with raylib's provided field description | ||
lines.push(format!(" /// {}", field.description)); | ||
|
||
// Write the field definition | ||
lines.push(format!(" pub {}: {},", field.name, translate_c_type_to_rust(&field.kind))); | ||
} | ||
|
||
// Close the struct definition | ||
lines.push(format!("}}")); | ||
} | ||
|
||
// Write the output file | ||
let out_dir = std::env::var("OUT_DIR").unwrap(); | ||
let out_path = format!("{}/structs.rs", out_dir); | ||
std::fs::write(out_path, lines.join("\n")).unwrap(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
pub fn translate_c_type_to_rust(c_type: &str) -> String { | ||
// match c_type { | ||
// "float" => "f32".to_string(), | ||
// "float *" => "std::ptr::null_mut()".to_string(), | ||
// "int" => "i32".to_string(), | ||
// "unsigned int" => "u32".to_string(), | ||
// "unsigned char *" => "std::ptr::null_mut()".to_string(), | ||
// "unsigned short *" => "std::ptr::null_mut()".to_string(), | ||
// "bool" => "bool".to_string(), | ||
// "void *" => "std::ptr::null_mut()".to_string(), | ||
// "Texture" => "crate::structs::Texture".to_string(), | ||
// "Rectangle" => "crate::structs::Rectangle".to_string(), | ||
// "Rectangle *" => "std::ptr::null_mut()".to_string(), | ||
// "Image" => "crate::structs::Image".to_string(), | ||
// "Texture2D" => "crate::structs::Texture2D".to_string(), | ||
// "GlyphInfo *" => "std::ptr::null_mut()".to_string(), | ||
// "Vector3" => "crate::structs::Vector3".to_string(), | ||
// "Vector2" => "crate::structs::Vector2".to_string(), | ||
// "Color" => "crate::structs::Color".to_string(), | ||
// "float[4]" => "[f32; 4]".to_string(), | ||
// "MaterialMap *" => "std::ptr::null_mut()".to_string(), | ||
// "Quaternion" => "crate::structs::Quaternion".to_string(), | ||
// "char[32]" => "[u8; 32]".to_string(), | ||
// _ => {panic!("Unknown C type: {}", c_type)} | ||
// } | ||
|
||
// Translate basic C types to their Rust equivalents | ||
if let Some(ty) = match c_type { | ||
"char" => Some("i8"), | ||
"unsigned char" => Some("u8"), | ||
"short" => Some("i16"), | ||
"unsigned short" => Some("u16"), | ||
"int" => Some("i32"), | ||
"unsigned int" => Some("u32"), | ||
"long" => Some("i64"), | ||
"unsigned long" => Some("u64"), | ||
"float" => Some("f32"), | ||
"double" => Some("f64"), | ||
"bool" => Some("bool"), | ||
"char *" => Some("String"), | ||
"void *" => Some("*mut libc::c_void"), | ||
_ => None, | ||
} { | ||
return ty.to_string(); | ||
} | ||
|
||
// For some reason, an internal data type is exposed. | ||
if c_type == "rAudioBuffer *" || c_type == "rAudioProcessor *" { | ||
return "std::ptr::null_mut()".to_string(); | ||
} | ||
|
||
|
||
// If the type ends with a *, it's a pointer to an array of the type without the * | ||
if c_type.ends_with("*") { | ||
let ty = &c_type[..c_type.len() - 1].trim(); | ||
return format!("Vec<{}>", translate_c_type_to_rust(ty)); | ||
} | ||
|
||
// If the type ends with `[N]`, it's an array of N elements of the type without the `[N]` | ||
let arr_len_re = regex::Regex::new(r"\[(\d+)\]$").unwrap(); | ||
if let Some(caps) = arr_len_re.captures(c_type) { | ||
let ty = &c_type[..c_type.len() - caps[0].len()].trim(); | ||
let len = &caps[1]; | ||
return format!("[{}; {}]", translate_c_type_to_rust(ty), len); | ||
} | ||
|
||
// Uppercase types are assumed to be structs | ||
if c_type.chars().next().unwrap().is_uppercase() { | ||
return format!("crate::structs::{}", c_type); | ||
} | ||
|
||
panic!("Unknown C type: {}", c_type) | ||
} |