diff options
| author | Weicao-CatilGrass <1992414357@qq.com> | 2026-05-22 07:58:02 +0800 |
|---|---|---|
| committer | Weicao-CatilGrass <1992414357@qq.com> | 2026-05-22 07:58:02 +0800 |
| commit | 16f1722894a5be67831a0382f48937118ddd176b (patch) | |
| tree | 1a23a6e82058966c206009358f5e6587c57b3d5b | |
| parent | e4a4e3e2b558d771537c7a4c0ba22f0d6b541b6e (diff) | |
Rename injected renderer parameter from `r` to `__renderer_inner_result`
| -rw-r--r-- | CHANGELOG.md | 15 | ||||
| -rw-r--r-- | docs/_zh_CN/pages/1-creating-your-first-program.md | 2 | ||||
| -rw-r--r-- | docs/pages/1-creating-your-first-program.md | 2 | ||||
| -rw-r--r-- | mingling_core/src/program.rs | 4 | ||||
| -rw-r--r-- | mingling_macros/src/dispatcher_clap.rs | 2 | ||||
| -rw-r--r-- | mingling_macros/src/help.rs | 12 | ||||
| -rw-r--r-- | mingling_macros/src/lib.rs | 6 | ||||
| -rw-r--r-- | mingling_macros/src/render.rs | 4 | ||||
| -rw-r--r-- | mingling_macros/src/renderer.rs | 18 | ||||
| -rw-r--r-- | mling/src/cli/list.rs | 20 |
10 files changed, 54 insertions, 31 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index bdfc71d..354c8fe 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,6 +44,21 @@ fn handle_path_pick(prev: PathPick) { 1. **\[core\]** Panic Unwind will not be supported when the `async` feature is enabled 2. **\[core\]** `modify_res` signature changed: now returns `Return` instead of `()` 3. **\[core\]** Renamed internal method `__modify_res_and_return_any` to `__modify_res_and_return_route` +4. **\[macros\]** Renamed the macro-internal function parameter `r` (used with the `r_` prefix) to `__renderer_inner_result` to reduce context pollution + +```rust +// Before +#[renderer] +fn render(prev: Previous) { // Implicitly introduces `r` + r_println!("{}", *prev); // Modifies `r` +} + +// After +#[renderer] +fn render(prev: Previous) { // Implicitly introduces `__renderer_inner_result` + r_println!("{}", *prev); // Modifies `__renderer_inner_result` +} +``` --- diff --git a/docs/_zh_CN/pages/1-creating-your-first-program.md b/docs/_zh_CN/pages/1-creating-your-first-program.md index 851eaa6..64c9cb2 100644 --- a/docs/_zh_CN/pages/1-creating-your-first-program.md +++ b/docs/_zh_CN/pages/1-creating-your-first-program.md @@ -146,7 +146,7 @@ fn renderer_name (_prev: PreviousType) { } ##### 关于 `r_println!()` 💡 - 您可能会注意到,在 `#[renderer]` 中使用的打印宏是 `r_println!` 而非 `println!`,这是因为框架的渲染逻辑并不在该函数内:在 `#[renderer]` 展开后,会向函数注入一个 `r: &mut RenderResult`;而 `r_println!` 将信息追加到 `RenderResult` 内,并在调度器关闭后,将最终的渲染数据交给 `Program::exec` 函数输出。 + 您可能会注意到,在 `#[renderer]` 中使用的打印宏是 `r_println!` 而非 `println!`,这是因为框架的渲染逻辑并不在该函数内:在 `#[renderer]` 展开后,会向函数注入一个 `__renderer_inner_result: &mut RenderResult`;而 `r_println!` 将信息追加到 `RenderResult` 内,并在调度器关闭后,将最终的渲染数据交给 `Program::exec` 函数输出。 diff --git a/docs/pages/1-creating-your-first-program.md b/docs/pages/1-creating-your-first-program.md index aaf3171..7348805 100644 --- a/docs/pages/1-creating-your-first-program.md +++ b/docs/pages/1-creating-your-first-program.md @@ -146,7 +146,7 @@ fn renderer_name (_prev: PreviousType) { } ##### About `r_println!()` 💡 - You might notice that the print macro used inside `#[renderer]` is `r_println!` instead of `println!`. This is because the framework's rendering logic doesn't happen inside that function: after `#[renderer]` expands, it injects a `r: &mut RenderResult` into the function; `r_println!` appends the message to the `RenderResult`, and after the dispatcher closes, the final rendered data is handed to `Program::exec` for output. + You might notice that the print macro used inside `#[renderer]` is `r_println!` instead of `println!`. This is because the framework's rendering logic doesn't happen inside that function: after `#[renderer]` expands, it injects a `__renderer_inner_result: &mut RenderResult` into the function; `r_println!` appends the message to the `RenderResult`, and after the dispatcher closes, the final rendered data is handed to `Program::exec` for output. diff --git a/mingling_core/src/program.rs b/mingling_core/src/program.rs index 96b2b1a..912975d 100644 --- a/mingling_core/src/program.rs +++ b/mingling_core/src/program.rs @@ -160,14 +160,14 @@ macro_rules! __dispatch_program_renderers { ( $( $render_ty:ty => $prev_ty:ident, )* ) => { - fn render(any: mingling::AnyOutput<Self::Enum>, r: &mut mingling::RenderResult) { + fn render(any: mingling::AnyOutput<Self::Enum>, __renderer_inner_result: &mut mingling::RenderResult) { match any.member_id { $( Self::$prev_ty => { // SAFETY: The `type_id` check ensures that `any` contains a value of type `$prev_ty`, // so downcasting to `$prev_ty` is safe. let value = unsafe { any.downcast::<$prev_ty>().unwrap_unchecked() }; - <$render_ty as mingling::Renderer>::render(value, r); + <$render_ty as mingling::Renderer>::render(value, __renderer_inner_result); } )* _ => (), diff --git a/mingling_macros/src/dispatcher_clap.rs b/mingling_macros/src/dispatcher_clap.rs index bb40404..42b5276 100644 --- a/mingling_macros/src/dispatcher_clap.rs +++ b/mingling_macros/src/dispatcher_clap.rs @@ -212,7 +212,7 @@ pub fn dispatcher_clap_attr(attr: TokenStream, item: TokenStream) -> TokenStream ::mingling::ClapHelpPrintBehaviour::WriteToRenderResult => { <#struct_name as ::clap::CommandFactory>::command() .color(ColorChoice::Always) - .write_help(r) + .write_help(__renderer_inner_result) .unwrap(); } ::mingling::ClapHelpPrintBehaviour::PrintDirectly => { diff --git a/mingling_macros/src/help.rs b/mingling_macros/src/help.rs index 7ce9e83..341f340 100644 --- a/mingling_macros/src/help.rs +++ b/mingling_macros/src/help.rs @@ -119,11 +119,11 @@ pub fn help_attr(item: TokenStream) -> TokenStream { impl ::mingling::HelpRequest for #struct_name { type Entry = #entry_type; - fn render_help(#prev_param: Self::Entry, r: &mut ::mingling::RenderResult) { - // Create a local wrapper function that includes r parameter - // This allows r_println! to access r + fn render_help(#prev_param: Self::Entry, __renderer_inner_result: &mut ::mingling::RenderResult) { + // Create a local wrapper function that includes `__renderer_inner_result` parameter + // This allows r_println! to access `__renderer_inner_result` #[allow(non_snake_case)] - fn help_wrapper(#prev_param: #entry_type, r: &mut ::mingling::RenderResult) { + fn help_wrapper(#prev_param: #entry_type, __renderer_inner_result: &mut ::mingling::RenderResult) { #fn_body } @@ -134,11 +134,11 @@ pub fn help_attr(item: TokenStream) -> TokenStream { ::mingling::macros::register_help!(#entry_type, #struct_name); - // Keep the original function for internal use (without r parameter) + // Keep the original function for internal use (without `__renderer_inner_result` parameter) #(#fn_attrs)* #vis fn #fn_name(#prev_param: #entry_type) { let mut dummy_r = ::mingling::RenderResult::default(); - let r = &mut dummy_r; + let __renderer_inner_result = &mut dummy_r; #fn_body } }; diff --git a/mingling_macros/src/lib.rs b/mingling_macros/src/lib.rs index 29c46b4..1733470 100644 --- a/mingling_macros/src/lib.rs +++ b/mingling_macros/src/lib.rs @@ -328,7 +328,7 @@ pub fn dispatcher(input: TokenStream) -> TokenStream { /// Prints formatted text to the current `RenderResult` buffer within a /// `#[renderer]`(macro.renderer.html) function. /// -/// This macro requires a mutable reference to a `RenderResult` named `r` +/// This macro requires a mutable reference to a `RenderResult` named `__renderer_inner_result` /// to be in scope, which is automatically provided inside `#[renderer]` /// functions. /// @@ -362,7 +362,7 @@ pub fn r_print(input: TokenStream) -> TokenStream { /// Prints formatted text followed by a newline to the current `RenderResult` /// buffer within a `#[renderer]`(macro.renderer.html) function. /// -/// This macro requires a mutable reference to a `RenderResult` named `r` +/// This macro requires a mutable reference to a `RenderResult` named `__renderer_inner_result` /// to be in scope, which is automatically provided inside `#[renderer]` /// functions. /// @@ -1452,7 +1452,7 @@ pub fn program_final_gen(input: TokenStream) -> TokenStream { ::mingling::__dispatch_program_chains!( #(#chain_tokens)* ); - fn render_help(any: ::mingling::AnyOutput<Self::Enum>, r: &mut ::mingling::RenderResult) { + fn render_help(any: ::mingling::AnyOutput<Self::Enum>, __renderer_inner_result: &mut ::mingling::RenderResult) { match any.member_id { #(#help_tokens)* _ => (), diff --git a/mingling_macros/src/render.rs b/mingling_macros/src/render.rs index 63ec49c..10fce7d 100644 --- a/mingling_macros/src/render.rs +++ b/mingling_macros/src/render.rs @@ -22,7 +22,7 @@ pub fn r_print(input: TokenStream) -> TokenStream { let expanded = quote! { { let formatted = #format_call; - ::mingling::RenderResult::print(r, &formatted) + ::mingling::RenderResult::print(__renderer_inner_result, &formatted) } }; @@ -48,7 +48,7 @@ pub fn r_println(input: TokenStream) -> TokenStream { let expanded = quote! { { let formatted = #format_call; - ::mingling::RenderResult::println(r, &formatted) + ::mingling::RenderResult::println(__renderer_inner_result, &formatted) } }; diff --git a/mingling_macros/src/renderer.rs b/mingling_macros/src/renderer.rs index d2c2221..8e0a43a 100644 --- a/mingling_macros/src/renderer.rs +++ b/mingling_macros/src/renderer.rs @@ -118,16 +118,16 @@ pub fn renderer_attr(item: TokenStream) -> TokenStream { impl ::mingling::Renderer for #struct_name { type Previous = #previous_type; - fn render(#prev_param: Self::Previous, r: &mut ::mingling::RenderResult) { - // Create a local wrapper function that includes r parameter - // This allows r_println! to access r + fn render(#prev_param: Self::Previous, __renderer_inner_result: &mut ::mingling::RenderResult) { + // Create a local wrapper function that includes `__renderer_inner_result` parameter + // This allows r_println! to access `__renderer_inner_result` #[allow(non_snake_case)] - fn render_wrapper(#prev_param: #previous_type, r: &mut ::mingling::RenderResult) { + fn render_wrapper(#prev_param: #previous_type, __renderer_inner_result: &mut ::mingling::RenderResult) { #fn_body } // Call the wrapper function - render_wrapper(#prev_param, r); + render_wrapper(#prev_param, __renderer_inner_result); } } @@ -137,7 +137,7 @@ pub fn renderer_attr(item: TokenStream) -> TokenStream { let #prev_param = #prev_param.into(); let mut dummy_r = ::mingling::RenderResult::default(); { - let r = &mut dummy_r; + let __renderer_inner_result = &mut dummy_r; #fn_body } dummy_r @@ -172,9 +172,9 @@ pub fn build_general_renderer_entry(previous_type: &TypePath) -> proc_macro2::To // SAFETY: Only types that match will enter this branch for forced conversion, // and `AnyOutput::new` ensures the type implements serde::Serialize let raw = unsafe { any.restore::<#previous_type>().unwrap_unchecked() }; - let mut r = ::mingling::RenderResult::default(); - ::mingling::GeneralRenderer::render(&raw, setting, &mut r)?; - Ok(r) + let mut __renderer_inner_result = ::mingling::RenderResult::default(); + ::mingling::GeneralRenderer::render(&raw, setting, &mut __renderer_inner_result)?; + Ok(__renderer_inner_result) } } } diff --git a/mling/src/cli/list.rs b/mling/src/cli/list.rs index 3ae9ef6..c3e4c05 100644 --- a/mling/src/cli/list.rs +++ b/mling/src/cli/list.rs @@ -77,32 +77,40 @@ pub(crate) fn handle_state_list_installed_option(prev: StateListInstalledOptions pub(crate) fn render_installed(prev: ResultInstalledNamespaces) { match prev.option { StateListInstalledOptions::All => { - print_list("Trusted".bright_green().bold().to_string(), prev.trusted, r); + print_list( + "Trusted".bright_green().bold().to_string(), + prev.trusted, + __renderer_inner_result, + ); print_list( "Untrusted".bright_red().bold().to_string(), prev.untrusted, - r, + __renderer_inner_result, ); print_list( "Untagged".bright_black().bold().to_string(), prev.untagged, - r, + __renderer_inner_result, ); } StateListInstalledOptions::OnlyTrusted => { - print_list("Trusted".bright_green().bold().to_string(), prev.trusted, r); + print_list( + "Trusted".bright_green().bold().to_string(), + prev.trusted, + __renderer_inner_result, + ); } StateListInstalledOptions::OnlyUntrusted => { print_list( "Untrusted".bright_red().bold().to_string(), prev.untrusted, - r, + __renderer_inner_result, ); } } } -fn print_list(title: String, list: Vec<String>, r: &mut RenderResult) { +fn print_list(title: String, list: Vec<String>, __renderer_inner_result: &mut RenderResult) { if list.is_empty() { return; } |
