@@ -32,48 +32,95 @@ def koharu_enums_path() -> Path:
3232path = koharu_enums_path ()
3333source = path .read_text (encoding = "utf-8" )
3434
35- if "type FfiEnumRepr" not in source :
36- anchor = "use crate::{Error, Result, ffi::NativeCall, sys};\n "
37- replacement = anchor + """
38-
39- // Clang uses an unsigned C enum representation for these non-negative values,
40- // while MSVC uses a signed representation. Match bindgen's target ABI.
41- #[cfg(target_env = "msvc")]
42- type FfiEnumRepr = i32;
43- #[cfg(not(target_env = "msvc"))]
44- type FfiEnumRepr = u32;
45- """
46- if source .count (anchor ) != 1 :
47- raise RuntimeError ("koharu-diffusion import anchor changed" )
48- source = source .replace (anchor , replacement )
35+ marker = "// Patched by Flint: preserve each bindgen enum's target-specific ABI."
36+ if marker in source :
37+ print (f"already patched { path } " )
38+ raise SystemExit (0 )
4939
5040replacements = {
51- " #[repr(i32)]\n " : (
52- " #[cfg_attr(target_env = \" msvc\" , repr(i32))]\n "
53- " #[cfg_attr(not(target_env = \" msvc\" ), repr(u32))]\n "
41+ "use crate::{Error, Result, ffi::NativeCall, sys};\n " : (
42+ "use crate::{Error, Result, ffi::NativeCall, sys};\n \n " + marker + "\n "
43+ ),
44+ "pub enum $name:ident, $kind:literal, $name_fn:path, $parse_fn:path, $invalid:path;" : (
45+ "pub enum $name:ident, $kind:literal, $raw_ty:ty, $name_fn:path, "
46+ "$parse_fn:path, $invalid:path;"
5447 ),
55- "pub const fn as_raw(self) -> i32" : "pub const fn as_raw(self) -> FfiEnumRepr" ,
56- "impl TryFrom<i32> for $name" : "impl TryFrom<FfiEnumRepr> for $name" ,
57- "fn try_from(value: i32) -> Result<Self>" : (
58- "fn try_from(value: FfiEnumRepr) -> Result<Self>"
48+ "pub enum $name:ident, $kind:literal {" : (
49+ "pub enum $name:ident, $kind:literal, $raw_ty:ty {"
5950 ),
51+ "$($variant = $raw),+" : "$($variant = $raw as i32),+" ,
52+ "pub const fn as_raw(self) -> i32" : "pub const fn as_raw(self) -> $raw_ty" ,
53+ "self as i32" : "self as i32 as $raw_ty" ,
54+ "impl TryFrom<i32> for $name" : "impl TryFrom<$raw_ty> for $name" ,
55+ "fn try_from(value: i32) -> Result<Self>" : "fn try_from(value: $raw_ty) -> Result<Self>" ,
6056 "value => Err(Error::InvalidEnum { kind: $kind, value })," : (
6157 "value => Err(Error::InvalidEnum { kind: $kind, value: value as i32 }),"
6258 ),
59+ 'pub enum WeightType, "weight type",' : (
60+ 'pub enum WeightType, "weight type", sys::sd_type_t,'
61+ ),
62+ 'pub enum RngType, "RNG type",' : (
63+ 'pub enum RngType, "RNG type", sys::rng_type_t,'
64+ ),
65+ 'pub enum SampleMethod, "sample method",' : (
66+ 'pub enum SampleMethod, "sample method", sys::sample_method_t,'
67+ ),
68+ 'pub enum Scheduler, "scheduler",' : (
69+ 'pub enum Scheduler, "scheduler", sys::scheduler_t,'
70+ ),
71+ 'pub enum Prediction, "prediction",' : (
72+ 'pub enum Prediction, "prediction", sys::prediction_t,'
73+ ),
74+ 'pub enum PreviewMode, "preview mode",' : (
75+ 'pub enum PreviewMode, "preview mode", sys::preview_t,'
76+ ),
77+ 'pub enum LoraApplyMode, "LoRA apply mode",' : (
78+ 'pub enum LoraApplyMode, "LoRA apply mode", sys::lora_apply_mode_t,'
79+ ),
80+ 'pub enum HiresUpscaler, "high-resolution upscaler",' : (
81+ 'pub enum HiresUpscaler, "high-resolution upscaler", '
82+ 'sys::sd_hires_upscaler_t,'
83+ ),
84+ 'pub enum VaeFormat, "VAE format" {' : (
85+ 'pub enum VaeFormat, "VAE format", sys::sd_vae_format_t {'
86+ ),
87+ 'pub enum CacheMode, "cache mode" {' : (
88+ 'pub enum CacheMode, "cache mode", sys::sd_cache_mode_t {'
89+ ),
90+ 'pub enum LogLevel, "log level" {' : (
91+ 'pub enum LogLevel, "log level", sys::sd_log_level_t {'
92+ ),
93+ 'pub enum CancelMode, "cancel mode" {' : (
94+ 'pub enum CancelMode, "cancel mode", sys::sd_cancel_mode_t {'
95+ ),
6396}
6497
6598expected_counts = {
66- " #[repr(i32)]\n " : 2 ,
99+ "use crate::{Error, Result, ffi::NativeCall, sys};\n " : 1 ,
100+ "pub enum $name:ident, $kind:literal, $name_fn:path, $parse_fn:path, $invalid:path;" : 1 ,
101+ "pub enum $name:ident, $kind:literal {" : 1 ,
102+ "$($variant = $raw),+" : 2 ,
67103 "pub const fn as_raw(self) -> i32" : 2 ,
104+ "self as i32" : 2 ,
68105 "impl TryFrom<i32> for $name" : 2 ,
69106 "fn try_from(value: i32) -> Result<Self>" : 2 ,
70107 "value => Err(Error::InvalidEnum { kind: $kind, value })," : 2 ,
108+ 'pub enum WeightType, "weight type",' : 1 ,
109+ 'pub enum RngType, "RNG type",' : 1 ,
110+ 'pub enum SampleMethod, "sample method",' : 1 ,
111+ 'pub enum Scheduler, "scheduler",' : 1 ,
112+ 'pub enum Prediction, "prediction",' : 1 ,
113+ 'pub enum PreviewMode, "preview mode",' : 1 ,
114+ 'pub enum LoraApplyMode, "LoRA apply mode",' : 1 ,
115+ 'pub enum HiresUpscaler, "high-resolution upscaler",' : 1 ,
116+ 'pub enum VaeFormat, "VAE format" {' : 1 ,
117+ 'pub enum CacheMode, "cache mode" {' : 1 ,
118+ 'pub enum LogLevel, "log level" {' : 1 ,
119+ 'pub enum CancelMode, "cancel mode" {' : 1 ,
71120}
72121
73122for old , new in replacements .items ():
74123 count = source .count (old )
75- if count == 0 and new in source :
76- continue
77124 if count != expected_counts [old ]:
78125 raise RuntimeError (f"unexpected occurrence count for { old !r} : { count } " )
79126 source = source .replace (old , new )
0 commit comments