| 1 |
use std::{ |
| 2 |
fs, |
| 3 |
io::{Error, Result, Write}, |
| 4 |
}; |
| 5 |
|
| 6 |
use log::info; |
| 7 |
|
| 8 |
use super::{ffi_type::ffi_type, fields, functions, statics}; |
| 9 |
use crate::{ |
| 10 |
codegen::general::{self, cfg_condition, version_condition}, |
| 11 |
config::constants, |
| 12 |
env::Env, |
| 13 |
file_saver::*, |
| 14 |
library::*, |
| 15 |
nameutil::*, |
| 16 |
traits::*, |
| 17 |
}; |
| 18 |
|
| 19 |
pub fn generate(env: &Env) { |
| 20 |
info!("Generating sys for {}", env.config.library_name); |
| 21 |
|
| 22 |
let path = env.config.auto_path.join(file_name_sys("lib")); |
| 23 |
|
| 24 |
info!("Generating file {path:?}"); |
| 25 |
save_to_file(&path, env.config.make_backup, |w| generate_lib(w, env)); |
| 26 |
} |
| 27 |
|
| 28 |
fn generate_lib(w: &mut dyn Write, env: &Env) -> Result<()> { |
| 29 |
general::start_comments(w, &env.config)?; |
| 30 |
statics::begin(w)?; |
| 31 |
|
| 32 |
generate_extern_crates(w, env)?; |
| 33 |
include_custom_modules(w, env)?; |
| 34 |
statics::after_extern_crates(w)?; |
| 35 |
|
| 36 |
if env.config.library_name != "GLib" { |
| 37 |
statics::use_glib(w)?; |
| 38 |
} |
| 39 |
match &*env.config.library_name { |
| 40 |
"GLib" => statics::only_for_glib(w)?, |
| 41 |
"GObject" => statics::only_for_gobject(w)?, |
| 42 |
"Gtk" => statics::only_for_gtk(w)?, |
| 43 |
_ => (), |
| 44 |
} |
| 45 |
writeln!(w)?; |
| 46 |
|
| 47 |
let ns = env.library.namespace(MAIN_NAMESPACE); |
| 48 |
let records = prepare(ns); |
| 49 |
let classes = prepare(ns); |
| 50 |
let interfaces = prepare(ns); |
| 51 |
let bitfields = prepare(ns); |
| 52 |
let enums = prepare(ns); |
| 53 |
let unions = prepare(ns); |
| 54 |
|
| 55 |
generate_aliases(w, env, &prepare(ns))?; |
| 56 |
generate_enums(w, env, &enums)?; |
| 57 |
generate_constants(w, env, &ns.constants)?; |
| 58 |
generate_bitfields(w, env, &bitfields)?; |
| 59 |
generate_unions(w, env, &unions)?; |
| 60 |
functions::generate_callbacks(w, env, &prepare(ns))?; |
| 61 |
generate_records(w, env, &records)?; |
| 62 |
generate_classes_structs(w, env, &classes)?; |
| 63 |
generate_interfaces_structs(w, env, &interfaces)?; |
| 64 |
|
| 65 |
if env.namespaces.main().shared_libs.is_empty() |
| 66 |
&& !(records.iter().all(|x| x.functions.is_empty()) |
| 67 |
&& classes.iter().all(|x| x.functions.is_empty()) |
| 68 |
&& interfaces.iter().all(|x| x.functions.is_empty()) |
| 69 |
&& bitfields.iter().all(|x| x.functions.is_empty()) |
| 70 |
&& enums.iter().all(|x| x.functions.is_empty()) |
| 71 |
&& unions.iter().all(|x| x.functions.is_empty())) |
| 72 |
{ |
| 73 |
return Err(Error::other( |
| 74 |
"No shared library found, but functions were found", |
| 75 |
)); |
| 76 |
} |
| 77 |
|
| 78 |
if !env.namespaces.main().shared_libs.is_empty() { |
| 79 |
writeln!(w, "unsafe extern \"C\" {{")?; |
| 80 |
functions::generate_enums_funcs(w, env, &enums)?; |
| 81 |
functions::generate_bitfields_funcs(w, env, &bitfields)?; |
| 82 |
functions::generate_unions_funcs(w, env, &unions)?; |
| 83 |
functions::generate_records_funcs(w, env, &records)?; |
| 84 |
functions::generate_classes_funcs(w, env, &classes)?; |
| 85 |
functions::generate_interfaces_funcs(w, env, &interfaces)?; |
| 86 |
functions::generate_other_funcs(w, env, &ns.functions)?; |
| 87 |
|
| 88 |
writeln!(w, "\n}}")?; |
| 89 |
} |
| 90 |
|
| 91 |
Ok(()) |
| 92 |
} |
| 93 |
|
| 94 |
fn generate_extern_crates(w: &mut dyn Write, env: &Env) -> Result<()> { |
| 95 |
for library in &env.config.external_libraries { |
| 96 |
w.write_all( |
| 97 |
format!( |
| 98 |
"use {}_sys as {};\n", |
| 99 |
library.crate_name.replace('-', "_"), |
| 100 |
crate_name(&library.namespace) |
| 101 |
) |
| 102 |
.as_bytes(), |
| 103 |
)?; |
| 104 |
} |
| 105 |
|
| 106 |
Ok(()) |
| 107 |
} |
| 108 |
|
| 109 |
fn include_custom_modules(w: &mut dyn Write, env: &Env) -> Result<()> { |
| 110 |
let modules = find_modules(env)?; |
| 111 |
if !modules.is_empty() { |
| 112 |
writeln!(w)?; |
| 113 |
for module in &modules { |
| 114 |
writeln!(w, "mod {module};")?; |
| 115 |
} |
| 116 |
writeln!(w)?; |
| 117 |
for module in &modules { |
| 118 |
writeln!(w, "pub use {module}::*;")?; |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
Ok(()) |
| 123 |
} |
| 124 |
|
| 125 |
fn find_modules(env: &Env) -> Result<Vec<String>> { |
| 126 |
let mut vec = Vec::<String>::new(); |
| 127 |
for entry in fs::read_dir(&env.config.auto_path)? { |
| 128 |
let path = entry?.path(); |
| 129 |
let Some(ext) = path.extension() else { |
| 130 |
continue; |
| 131 |
}; |
| 132 |
if ext != "rs" { |
| 133 |
continue; |
| 134 |
} |
| 135 |
let file_stem = path.file_stem().expect("No file name"); |
| 136 |
if file_stem == "lib" { |
| 137 |
continue; |
| 138 |
} |
| 139 |
let file_stem = file_stem |
| 140 |
.to_str() |
| 141 |
.expect("Can't convert file name to string") |
| 142 |
.to_owned(); |
| 143 |
vec.push(file_stem); |
| 144 |
} |
| 145 |
vec.sort(); |
| 146 |
|
| 147 |
Ok(vec) |
| 148 |
} |
| 149 |
|
| 150 |
fn prepare<T: Ord>(ns: &Namespace) -> Vec<&T> |
| 151 |
where |
| 152 |
Type: MaybeRef<T>, |
| 153 |
{ |
| 154 |
let mut vec: Vec<&T> = Vec::with_capacity(ns.types.len()); |
| 155 |
for typ in ns.types.iter().filter_map(Option::as_ref) { |
| 156 |
if let Some(x) = typ.maybe_ref() { |
| 157 |
vec.push(x); |
| 158 |
} |
| 159 |
} |
| 160 |
vec.sort(); |
| 161 |
vec |
| 162 |
} |
| 163 |
|
| 164 |
fn generate_aliases(w: &mut dyn Write, env: &Env, items: &[&Alias]) -> Result<()> { |
| 165 |
if !items.is_empty() { |
| 166 |
writeln!(w, "// Aliases")?; |
| 167 |
} |
| 168 |
for item in items { |
| 169 |
let full_name = format!("{}.{}", env.namespaces.main().name, item.name); |
| 170 |
if !env.type_status_sys(&full_name).need_generate() { |
| 171 |
continue; |
| 172 |
} |
| 173 |
let (comment, c_type) = match ffi_type(env, item.typ, &item.target_c_type) { |
| 174 |
Ok(x) => ("", x.into_string()), |
| 175 |
x @ Err(..) => ("//", x.into_string()), |
| 176 |
}; |
| 177 |
let cfg_condition_ = env |
| 178 |
.config |
| 179 |
.objects |
| 180 |
.get(&full_name) |
| 181 |
.and_then(|obj| obj.cfg_condition.as_ref()); |
| 182 |
cfg_condition(w, cfg_condition_, false, 0)?; |
| 183 |
writeln!(w, "{}pub type {} = {};", comment, item.c_identifier, c_type)?; |
| 184 |
} |
| 185 |
if !items.is_empty() { |
| 186 |
writeln!(w)?; |
| 187 |
} |
| 188 |
|
| 189 |
Ok(()) |
| 190 |
} |
| 191 |
|
| 192 |
fn generate_bitfields(w: &mut dyn Write, env: &Env, items: &[&Bitfield]) -> Result<()> { |
| 193 |
if !items.is_empty() { |
| 194 |
writeln!(w, "// Flags")?; |
| 195 |
} |
| 196 |
for item in items { |
| 197 |
let full_name = format!("{}.{}", env.namespaces.main().name, item.name); |
| 198 |
let config = env.config.objects.get(&full_name); |
| 199 |
if let Some(false) = config.map(|c| c.status.need_generate()) { |
| 200 |
continue; |
| 201 |
} |
| 202 |
writeln!(w, "pub type {} = c_uint;", item.c_type)?; |
| 203 |
for member in &item.members { |
| 204 |
let member_config = config |
| 205 |
.as_ref() |
| 206 |
.map_or_else(Vec::new, |c| c.members.matched(&member.name)); |
| 207 |
let version = member_config |
| 208 |
.iter() |
| 209 |
.find_map(|m| m.version) |
| 210 |
.or(member.version); |
| 211 |
|
| 212 |
if member_config.iter().any(|m| m.status.ignored()) { |
| 213 |
continue; |
| 214 |
} |
| 215 |
|
| 216 |
let val: i64 = member.value.parse().unwrap(); |
| 217 |
|
| 218 |
version_condition(w, env, None, version, false, 0)?; |
| 219 |
writeln!( |
| 220 |
w, |
| 221 |
"pub const {}: {} = {};", |
| 222 |
member.c_identifier, item.c_type, val as u32, |
| 223 |
)?; |
| 224 |
} |
| 225 |
writeln!(w)?; |
| 226 |
} |
| 227 |
|
| 228 |
Ok(()) |
| 229 |
} |
| 230 |
|
| 231 |
fn generate_constant_cfg_configure( |
| 232 |
w: &mut dyn Write, |
| 233 |
configured_constants: &[&constants::Constant], |
| 234 |
commented: bool, |
| 235 |
) -> Result<()> { |
| 236 |
let cfg_condition_ = configured_constants |
| 237 |
.iter() |
| 238 |
.find_map(|f| f.cfg_condition.as_ref()); |
| 239 |
cfg_condition(w, cfg_condition_, commented, 1)?; |
| 240 |
Ok(()) |
| 241 |
} |
| 242 |
|
| 243 |
fn generate_constants(w: &mut dyn Write, env: &Env, constants: &[Constant]) -> Result<()> { |
| 244 |
if !constants.is_empty() { |
| 245 |
writeln!(w, "// Constants")?; |
| 246 |
} |
| 247 |
for constant in constants { |
| 248 |
let full_name = format!("{}.{}", env.namespaces.main().name, constant.name); |
| 249 |
let config = env.config.objects.get(&full_name); |
| 250 |
if let Some(false) = config.map(|c| c.status.need_generate()) { |
| 251 |
continue; |
| 252 |
} |
| 253 |
let (comment, mut type_) = match ffi_type(env, constant.typ, &constant.c_type) { |
| 254 |
Ok(x) => ("", x.into_string()), |
| 255 |
x @ Err(..) => ("//", x.into_string()), |
| 256 |
}; |
| 257 |
let mut value = constant.value.clone(); |
| 258 |
if type_ == "*mut c_char" || type_ == "*const c_char" { |
| 259 |
type_ = "&[u8]".into(); |
| 260 |
value = format!("b\"{}\\0\"", general::escape_string(&value)); |
| 261 |
} else if type_ == "gboolean" { |
| 262 |
value = if value == "true" { |
| 263 |
use_glib_if_needed(env, "GTRUE") |
| 264 |
} else { |
| 265 |
use_glib_if_needed(env, "GFALSE") |
| 266 |
}; |
| 267 |
} else if env |
| 268 |
.library |
| 269 |
.type_(constant.typ) |
| 270 |
.maybe_ref_as::<Bitfield>() |
| 271 |
.is_some() |
| 272 |
{ |
| 273 |
let val: i64 = constant.value.parse().unwrap(); |
| 274 |
value = (val as u32).to_string(); |
| 275 |
} |
| 276 |
|
| 277 |
if let Some(obj) = config { |
| 278 |
let configured_constants = obj.constants.matched(&full_name); |
| 279 |
generate_constant_cfg_configure(w, &configured_constants, !comment.is_empty())?; |
| 280 |
} |
| 281 |
|
| 282 |
writeln!( |
| 283 |
w, |
| 284 |
"{}pub const {}: {} = {};", |
| 285 |
comment, constant.c_identifier, type_, value |
| 286 |
)?; |
| 287 |
} |
| 288 |
if !constants.is_empty() { |
| 289 |
writeln!(w)?; |
| 290 |
} |
| 291 |
|
| 292 |
Ok(()) |
| 293 |
} |
| 294 |
|
| 295 |
fn generate_enums(w: &mut dyn Write, env: &Env, items: &[&Enumeration]) -> Result<()> { |
| 296 |
if !items.is_empty() { |
| 297 |
writeln!(w, "// Enums")?; |
| 298 |
} |
| 299 |
for item in items { |
| 300 |
let full_name = format!("{}.{}", env.namespaces.main().name, item.name); |
| 301 |
let config = env.config.objects.get(&full_name); |
| 302 |
if let Some(false) = config.map(|c| c.status.need_generate()) { |
| 303 |
continue; |
| 304 |
} |
| 305 |
let cfg_condition_ = env |
| 306 |
.config |
| 307 |
.objects |
| 308 |
.get(&full_name) |
| 309 |
.and_then(|obj| obj.cfg_condition.as_ref()); |
| 310 |
cfg_condition(w, cfg_condition_, false, 0)?; |
| 311 |
writeln!(w, "pub type {} = c_int;", item.c_type)?; |
| 312 |
for member in &item.members { |
| 313 |
let member_config = config |
| 314 |
.as_ref() |
| 315 |
.map_or_else(Vec::new, |c| c.members.matched(&member.name)); |
| 316 |
let version = member_config |
| 317 |
.iter() |
| 318 |
.find_map(|m| m.version) |
| 319 |
.or(member.version); |
| 320 |
|
| 321 |
if member_config.iter().any(|m| m.status.ignored()) { |
| 322 |
continue; |
| 323 |
} |
| 324 |
|
| 325 |
cfg_condition(w, cfg_condition_, false, 0)?; |
| 326 |
version_condition(w, env, None, version, false, 0)?; |
| 327 |
writeln!( |
| 328 |
w, |
| 329 |
"pub const {}: {} = {};", |
| 330 |
member.c_identifier, item.c_type, member.value, |
| 331 |
)?; |
| 332 |
} |
| 333 |
writeln!(w)?; |
| 334 |
} |
| 335 |
|
| 336 |
Ok(()) |
| 337 |
} |
| 338 |
|
| 339 |
fn generate_unions(w: &mut dyn Write, env: &Env, unions: &[&Union]) -> Result<()> { |
| 340 |
if !unions.is_empty() { |
| 341 |
writeln!(w, "// Unions")?; |
| 342 |
} |
| 343 |
for union in unions { |
| 344 |
if union.c_type.is_none() { |
| 345 |
continue; |
| 346 |
} |
| 347 |
let full_name = format!("{}.{}", env.namespaces.main().name, union.name); |
| 348 |
let config = env.config.objects.get(&full_name); |
| 349 |
|
| 350 |
if let Some(false) = config.map(|c| c.status.need_generate()) { |
| 351 |
continue; |
| 352 |
} |
| 353 |
|
| 354 |
let align = config.and_then(|c| c.align); |
| 355 |
let fields = fields::from_union(env, union); |
| 356 |
generate_from_fields(w, &fields, align)?; |
| 357 |
} |
| 358 |
Ok(()) |
| 359 |
} |
| 360 |
|
| 361 |
fn generate_debug_impl(w: &mut dyn Write, name: &str, impl_content: &str) -> Result<()> { |
| 362 |
writeln!( |
| 363 |
w, |
| 364 |
"impl ::std::fmt::Debug for {name} {{\n\ |
| 365 |
\tfn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {{\n\ |
| 366 |
\t\t{impl_content}\n\ |
| 367 |
\t}}\n\ |
| 368 |
}}\n" |
| 369 |
) |
| 370 |
} |
| 371 |
|
| 372 |
fn generate_classes_structs(w: &mut dyn Write, env: &Env, classes: &[&Class]) -> Result<()> { |
| 373 |
if !classes.is_empty() { |
| 374 |
writeln!(w, "// Classes")?; |
| 375 |
} |
| 376 |
for class in classes { |
| 377 |
let full_name = format!("{}.{}", env.namespaces.main().name, class.name); |
| 378 |
let config = env.config.objects.get(&full_name); |
| 379 |
|
| 380 |
if let Some(false) = config.map(|c| c.status.need_generate()) { |
| 381 |
continue; |
| 382 |
} |
| 383 |
|
| 384 |
let align = config.and_then(|c| c.align); |
| 385 |
let fields = fields::from_class(env, class); |
| 386 |
generate_from_fields(w, &fields, align)?; |
| 387 |
} |
| 388 |
Ok(()) |
| 389 |
} |
| 390 |
|
| 391 |
fn generate_opaque_type(w: &mut dyn Write, name: &str) -> Result<()> { |
| 392 |
writeln!( |
| 393 |
w, |
| 394 |
r#"#[repr(C)] |
| 395 |
#[allow(dead_code)] |
| 396 |
pub struct {name} {{ |
| 397 |
_data: [u8; 0], |
| 398 |
_marker: core::marker::PhantomData<(*mut u8, core::marker::PhantomPinned)>, |
| 399 |
}} |
| 400 |
"# |
| 401 |
) |
| 402 |
} |
| 403 |
|
| 404 |
fn generate_interfaces_structs( |
| 405 |
w: &mut dyn Write, |
| 406 |
env: &Env, |
| 407 |
interfaces: &[&Interface], |
| 408 |
) -> Result<()> { |
| 409 |
if !interfaces.is_empty() { |
| 410 |
writeln!(w, "// Interfaces")?; |
| 411 |
} |
| 412 |
for interface in interfaces { |
| 413 |
let full_name = format!("{}.{}", env.namespaces.main().name, interface.name); |
| 414 |
if !env.type_status_sys(&full_name).need_generate() { |
| 415 |
continue; |
| 416 |
} |
| 417 |
let cfg_condition_ = env |
| 418 |
.config |
| 419 |
.objects |
| 420 |
.get(&full_name) |
| 421 |
.and_then(|obj| obj.cfg_condition.as_ref()); |
| 422 |
cfg_condition(w, cfg_condition_, false, 0)?; |
| 423 |
generate_opaque_type(w, &interface.c_type)?; |
| 424 |
cfg_condition(w, cfg_condition_, false, 0)?; |
| 425 |
generate_debug_impl( |
| 426 |
w, |
| 427 |
&interface.c_type, |
| 428 |
&format!( |
| 429 |
"write!(f, \"{name} @ {{self:p}}\")", |
| 430 |
name = interface.c_type |
| 431 |
), |
| 432 |
)?; |
| 433 |
} |
| 434 |
if !interfaces.is_empty() { |
| 435 |
writeln!(w)?; |
| 436 |
} |
| 437 |
|
| 438 |
Ok(()) |
| 439 |
} |
| 440 |
|
| 441 |
fn generate_records(w: &mut dyn Write, env: &Env, records: &[&Record]) -> Result<()> { |
| 442 |
if !records.is_empty() { |
| 443 |
writeln!(w, "// Records")?; |
| 444 |
} |
| 445 |
for record in records { |
| 446 |
let full_name = format!("{}.{}", env.namespaces.main().name, record.name); |
| 447 |
let config = env.config.objects.get(&full_name); |
| 448 |
|
| 449 |
if let Some(false) = config.map(|c| c.status.need_generate()) { |
| 450 |
continue; |
| 451 |
} |
| 452 |
|
| 453 |
if record.c_type == "GHookList" { |
| 454 |
// 1. GHookList is useful. |
| 455 |
// 2. GHookList contains bitfields. |
| 456 |
// 3. Bitfields are unrepresentable in Rust. |
| 457 |
// 4. ... |
| 458 |
// 5. Thus, we use custom generated GHookList. |
| 459 |
// Hopefully someone will profit from all this. |
| 460 |
generate_ghooklist(w)?; |
| 461 |
} else if record.disguised || record.pointer { |
| 462 |
generate_disguised(w, env, record)?; |
| 463 |
} else { |
| 464 |
let align = config.and_then(|c| c.align); |
| 465 |
let fields = fields::from_record(env, record); |
| 466 |
generate_from_fields(w, &fields, align)?; |
| 467 |
} |
| 468 |
} |
| 469 |
Ok(()) |
| 470 |
} |
| 471 |
|
| 472 |
fn generate_ghooklist(w: &mut dyn Write) -> Result<()> { |
| 473 |
w.write_all( |
| 474 |
br#"#[repr(C)] |
| 475 |
#[derive(Copy, Clone)] |
| 476 |
pub struct GHookList { |
| 477 |
pub seq_id: c_ulong, |
| 478 |
#[cfg(any(not(windows), not(target_pointer_width = "64")))] |
| 479 |
pub hook_size_and_setup: gpointer, |
| 480 |
#[cfg(all(windows, target_pointer_width = "64"))] |
| 481 |
pub hook_size_and_setup: c_ulong, |
| 482 |
pub hooks: *mut GHook, |
| 483 |
pub dummy3: gpointer, |
| 484 |
pub finalize_hook: GHookFinalizeFunc, |
| 485 |
pub dummy: [gpointer; 2], |
| 486 |
} |
| 487 |
|
| 488 |
impl ::std::fmt::Debug for GHookList { |
| 489 |
fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { |
| 490 |
write!(f, "GHookList @ {self:p}") |
| 491 |
} |
| 492 |
} |
| 493 |
|
| 494 |
"#, |
| 495 |
) |
| 496 |
} |
| 497 |
|
| 498 |
fn generate_disguised(w: &mut dyn Write, env: &Env, record: &Record) -> Result<()> { |
| 499 |
let full_name = format!("{}.{}", env.namespaces.main().name, record.name); |
| 500 |
let cfg_condition_ = env |
| 501 |
.config |
| 502 |
.objects |
| 503 |
.get(&full_name) |
| 504 |
.and_then(|obj| obj.cfg_condition.as_ref()); |
| 505 |
cfg_condition(w, cfg_condition_, false, 0)?; |
| 506 |
generate_opaque_type(w, &format!("_{}", record.c_type))?; |
| 507 |
cfg_condition(w, cfg_condition_, false, 0)?; |
| 508 |
if record.pointer { |
| 509 |
writeln!(w, "pub type {name} = *mut _{name};", name = record.c_type)?; |
| 510 |
} else { |
| 511 |
writeln!(w, "pub type {name} = _{name};", name = record.c_type)?; |
| 512 |
} |
| 513 |
writeln!(w) |
| 514 |
} |
| 515 |
|
| 516 |
fn generate_from_fields( |
| 517 |
w: &mut dyn Write, |
| 518 |
fields: &fields::Fields, |
| 519 |
align: Option<u32>, |
| 520 |
) -> Result<()> { |
| 521 |
cfg_condition(w, fields.cfg_condition.as_ref(), false, 0)?; |
| 522 |
if let Some(align) = align { |
| 523 |
writeln!(w, "#[repr(align({align}))]")?; |
| 524 |
} |
| 525 |
let traits = fields.derived_traits().join(", "); |
| 526 |
if !traits.is_empty() { |
| 527 |
writeln!(w, "#[derive({traits})]")?; |
| 528 |
} |
| 529 |
if fields.external { |
| 530 |
// It would be nice to represent those using extern types |
| 531 |
// from RFC 1861, once they are available in stable Rust. |
| 532 |
// https://github.com/rust-lang/rust/issues/43467 |
| 533 |
generate_opaque_type(w, &fields.name)?; |
| 534 |
} else { |
| 535 |
writeln!(w, "#[repr(C)]")?; |
| 536 |
|
| 537 |
if fields.truncated.is_some() { |
| 538 |
writeln!(w, "#[allow(dead_code)]")?; |
| 539 |
} |
| 540 |
|
| 541 |
writeln!( |
| 542 |
w, |
| 543 |
"pub {kind} {name} {{", |
| 544 |
kind = fields.kind, |
| 545 |
name = fields.name |
| 546 |
)?; |
| 547 |
for field in &fields.fields { |
| 548 |
writeln!( |
| 549 |
w, |
| 550 |
"\tpub {field_name}: {field_type},", |
| 551 |
field_name = field.name, |
| 552 |
field_type = field.typ |
| 553 |
)?; |
| 554 |
} |
| 555 |
if let Some(ref reason) = fields.truncated { |
| 556 |
writeln!(w, "\t_truncated_record_marker: c_void,")?; |
| 557 |
writeln!(w, "\t// {reason}")?; |
| 558 |
} |
| 559 |
writeln!(w, "}}\n")?; |
| 560 |
} |
| 561 |
|
| 562 |
cfg_condition(w, fields.cfg_condition.as_ref(), false, 0)?; |
| 563 |
writeln!( |
| 564 |
w, |
| 565 |
"impl ::std::fmt::Debug for {name} {{", |
| 566 |
name = fields.name |
| 567 |
)?; |
| 568 |
writeln!( |
| 569 |
w, |
| 570 |
"\tfn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {{" |
| 571 |
)?; |
| 572 |
writeln!( |
| 573 |
w, |
| 574 |
"\t\tf.debug_struct(&format!(\"{name} @ {{self:p}}\"))", |
| 575 |
name = fields.name |
| 576 |
)?; |
| 577 |
for field in fields.fields.iter().filter(|f| f.debug) { |
| 578 |
// TODO: We should generate debug for field manually if automatic one is not |
| 579 |
// available. |
| 580 |
writeln!( |
| 581 |
w, |
| 582 |
"\t\t .field(\"{field_name}\", {field_get})", |
| 583 |
field_name = field.name, |
| 584 |
field_get = field.access_str() |
| 585 |
)?; |
| 586 |
} |
| 587 |
writeln!(w, "\t\t .finish()")?; |
| 588 |
writeln!(w, "\t}}")?; |
| 589 |
writeln!(w, "}}")?; |
| 590 |
writeln!(w) |
| 591 |
} |