Method, system and equipment for calling generic method by using Rust language

By automatically generating extended traits and a global registry in the Rust language, the problem of trait objects not supporting generic methods is solved, enabling type-safe and zero-runtime-overhead dynamic generic method calls, thus improving Rust's abstraction capabilities and development efficiency.

CN122064402APending Publication Date: 2026-05-19GUANGZHOU AVA ELECTRONICS TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202610185590.2
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2026-02-09
Publication Date
2026-05-19

AI Technical Summary

Technical Problem

In Rust, trait objects do not support calling generic methods, which limits flexibility and development experience, and makes it difficult to implement strategy and factory patterns.

Method used

By using procedural macros, extended traits and a global registry are automatically generated at compile time. Wrapper functions are written to handle type erasure and restoration, and generic methods are dynamically called at runtime.

Benefits of technology

It enables the safe invocation of generic methods through trait objects in the Rust language, maintaining type safety and eliminating runtime overhead, thereby improving the development experience and code reusability.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN122064402A_ABST
    Figure CN122064402A_ABST
Patent Text Reader

Abstract

The invention discloses a method, a system and equipment for calling a generic method by a Rust language. Wherein. The method comprises an expansion step during compiling and a calling step during running, during compiling, a package function for expanding trait and type erasure / recovery is automatically generated through process macro analysis trait and specific implementation, and mapping of the type and the package function is registered to a global table. During operation, when the generic method is called for the trait object, the registry is inquired according to the actual type, the corresponding packaging function is found and executed, and therefore the original generic method is indirectly called for implementation. The core is to realize dynamic distribution of generic methods through compilation period code generation and runtime search. According to the method, the core limitation that the trait object in the Rust language does not support the generic method is successfully solved, and the dynamic generic method calling with safe type and zero running overhead is realized.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of computer programming language technology, and more specifically, to a method, system, and device for calling generic methods in the Rust language. Background Technology

[0002] Rust is a modern systems programming language that combines memory safety, high performance, and concurrency safety, and is widely used in systems development, network programming, and embedded devices.

[0003] Among them, traits are the basic mechanism for defining type behavior in Rust. They can be understood as a set of method signatures that specify the functionality that a type must implement. Trait objects are an important means of implementing runtime polymorphism in Rust, allowing the invocation of appropriate methods at runtime based on the actual type of the object. Generics: One of Rust's core features, it allows writing code that is independent of specific types, improving code reusability. For example, a generic "process" method can handle data of different types, such as integers and strings.

[0004] In Rust's native design, there's a fundamental limitation: trait objects don't support calling generic methods. Simply put, once a developer defines a trait containing generic methods, they cannot be cast to a trait object for dynamic invocation. For example: The developers defined a `Processor` trait, which contains a generic method `process` that can process data of any type. <t>` - Developers have created various structs that implement this trait (such as `JsonProcessor`, `XmlProcessor`, etc.). - When developers want to use a unified trait object interface (such as `Box`)<dyn Processor> When managing these different processors, it is impossible to call their generic methods `process`. <t>`.

[0005] This limitation severely impacts the flexibility of the Rust language and the programming experience for developers, making it difficult to implement many common object-oriented design patterns (such as the strategy pattern and the factory pattern) in Rust. Summary of the Invention

[0006] To address the technical problem in existing technologies where generic methods require the generation of dedicated code for each specific type at compile time (referred to as "singleness"), while trait objects determine their specific type only at runtime (referred to as "dynamic dispatch"), this invention provides a method, system, and device for calling generic methods in the Rust language. The technical solution adopted by this invention is as follows.

[0007] In a first aspect, the present invention provides a method for calling generic methods in the Rust language, comprising: a compile-time expansion step and a runtime invocation step. The compile-time expansion step includes: The first process macro parses the first trait defined by the developer and contains generic methods, and automatically generates an extended trait containing the same generic method signature based on the first trait; The second process involves macro parsing the developer's implementation code for the first trait for specific types, generating corresponding wrapper functions for each generic method in the implementation code, and generating registration code that associates the specific type with the corresponding wrapper function to construct a global registry; the global registry is used to store the mapping relationship between specific types and corresponding wrapper functions; wherein, the wrapper function can handle type erasure and restoration; The runtime invocation steps include: In response to the trait object's call instruction to a generic method, the extended trait corresponding to the trait object is used as the current trait object to obtain the specific type represented by the current trait object instance; Based on the specific type represented by the current trait object instance, the global registry is queried to find the target wrapper function corresponding to the specific type represented by the current trait object instance; The target wrapper function is invoked for the current trait object instance to execute the original generic method implementation on the specific type and return the execution result.

[0008] In one implementation, the generated wrapper function is configured to receive type-erased input parameters. Inside the wrapper function, the type-erased input parameters are forcibly converted to the original concrete type, and the original generic method written by the developer is called using the converted concrete type parameters.

[0009] In one implementation, the process of forcibly converting the type-erased input parameter to its original concrete type within the wrapper function includes: Convert the generic parameter to a dynamically typed container `Box`<dyn Any> `; The process of calling the target wrapper function for the current trait object instance to execute the original generic method implementation on the specific type includes: Convert the generic parameter of the current trait object instance to `Box`<dyn Any> `; Inside the wrapper function, the input parameter is transferred from `Box`<dyn Any> Restored to its original type; Execute the original implementation.

[0010] In one implementation, the registry is implemented as a HashMap.

[0011] In one implementation, the registry automatically adds the `Send + Sync + 'static' constraint.

[0012] In one implementation, the registry is looked up directly using `TypeId`.

[0013] In one implementation, the compile-time expansion step further includes: The extended trait is automatically implemented by the dynamic type of the base trait object, thereby providing the entry point for calling generic methods for the trait object; The entry point is configured to trigger the runtime invocation step during runtime.

[0014] Secondly, this invention provides a system for calling generic methods in the Rust language, comprising: Compile-time extension modules and runtime calling modules; The compile-time extension module includes: The first process macro unit is configured to: parse the first trait defined by the developer that contains generic methods, and automatically generate an extended trait containing the same generic method signature based on the first trait; The second process macro unit is configured to: parse the developer's implementation code for the first trait for a specific type, generate a corresponding wrapper function for each generic method in the implementation code, and generate registration code that associates the specific type with the corresponding wrapper function to build a global registry; wherein the wrapper function is capable of handling type erasure and restoration; A global registry used to store the mapping relationship between specific types and their corresponding wrapper functions; The runtime invocation module is configured to perform the following steps when the generic method is invoked through a trait object that implements the base trait: Use the extended trait corresponding to the trait object as the trait object to obtain the specific type represented by the trait object; Based on the specific type represented by the trait object, the global registry is queried to find the target wrapper function corresponding to the specific type represented by the trait object; The target wrapper function is called on the trait object to execute the original generic method implementation on the specific type and return the execution result.

[0015] Thirdly, the present invention provides a computer device including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the program to implement the method of any of the above embodiments.

[0016] Fourthly, the present invention provides a computer-readable storage medium having a computer program stored thereon, characterized in that the program, when executed by a processor, implements the method of any of the above embodiments.

[0017] In this invention, code is automatically generated at compile time: when a developer marks a trait with `#[dyn_generic]`, an extended trait and a global registry are automatically generated; when a developer marks a trait implementation with `#[impl_generic]`, a wrapper function and registration code are automatically generated. Dynamic runtime invocation: when a developer calls a generic method through a trait object, the type ID of the current instance is first obtained; the type ID is used to find the corresponding method implementation in the global registry; the generic parameters are converted to a unified format for transmission; and the original type is restored in the wrapper function, and the actual method implementation is executed. This invention successfully solves the core limitation of Rust language where trait objects do not support generic methods, achieving type-safe, zero-runtime-overhead dynamic generic method invocation. Attached Figure Description

[0018] Figure 1 This is a schematic diagram of the overall process of one of the embodiments of the present invention.

[0019] Figure 2 This is a schematic diagram of the overall structure of an embodiment of the second embodiment of the present invention. Detailed Implementation

[0020] Exemplary embodiments of the present disclosure will now be described in more detail with reference to the accompanying drawings. While exemplary embodiments of the present disclosure are shown in the drawings, it should be understood that the present disclosure may be implemented in various forms and should not be limited to the embodiments set forth herein. Rather, these embodiments are provided so that this disclosure will be thorough and complete, and will fully convey the scope of the disclosure to those skilled in the art.

[0021] It should be noted that the terms "first, second, ..." used in the embodiments of the present invention are merely used to distinguish similar objects and do not represent a specific ordering of objects. It is understood that "first, second, ..." can be interchanged in a specific order or sequence where permissible. It should be understood that the objects distinguished by "first, second, ..." can be interchanged where appropriate so that the embodiments of the present invention described herein can be implemented in an order other than those illustrated or described herein. Example

[0022] Please see Figure 1 , Figure 1 This is a flowchart illustrating a method for calling a generic method in Rust language according to Embodiment 1 of the present invention. The method includes a compile-time expansion step and a runtime invocation step. The compile-time expansion step includes steps S110 and S120, and the runtime invocation step includes steps S130, S140, and S150. It should be noted that steps S110, S120, S130, S140, and S150 are merely reference numerals used to clearly explain the embodiment and the accompanying drawings. Figure 1 The correspondence is not intended to limit the order of steps in this embodiment.

[0023] The compile-time expansion steps include: Step S110: Parse the first trait defined by the developer and containing generic methods through the first process macro, and automatically generate an extended trait containing the same generic method signature based on the first trait; Step S120: The second process macro parses the developer's implementation code for the first trait for specific types, generates corresponding wrapper functions for each generic method in the implementation code, and generates registration code that associates the specific type with the corresponding wrapper function to build a global registry; the global registry is used to store the mapping relationship between specific types and corresponding wrapper functions; wherein, the wrapper function can handle type erasure and restoration; The runtime invocation steps include: Step S130: In response to the trait object's instruction to call the generic method, the extended trait corresponding to the trait object is used as the current trait object to obtain the specific type represented by the current trait object instance; Step S140: Based on the specific type represented by the current trait object instance, query the global registry to find the target wrapper function corresponding to the specific type represented by the current trait object instance; Step S150: Invoke the target wrapper function for the current trait object instance to execute the original generic method implementation on the specific type, and return the execution result.

[0024] In this method, the necessary code is generated during the compile-time extension step.

[0025] In step S110, the process is mainly implemented through the first process macro, which is the `#[dyn_generic]` attribute macro. It is used to automatically generate extended code for traits containing generic methods, generate a global registry to store generic method implementations, and generate core calling functions for dynamic method lookup.

[0026] Specifically, in step S110, the compile-time processing of the `#[dyn_generic]` macro includes: Parse the trait defined by the developer that contains generic methods; Automatically generate an extended trait (such as `SafeForDynGenericExt`) that contains the same generic method signature as the original trait; Generate a global registry module (such as `safe_for_dyn_generic_registry`) to store the mapping from type to method implementation; Automatically implement extended traits for `dyn BaseTrait` (such as `dyn SafeForDyn`), providing an entry point for calling generic methods.

[0027] In step S120, the process is mainly implemented through the second process macro, which is the `#[impl_generic]` attribute macro. This macro is used to automatically generate wrapper functions for the trait and generate registration code to register it to the global registry.

[0028] Specifically, in step S120, the compile-time processing of `#[impl_generic]` includes: Analyze the trait implementation code written by the developer; Generate a corresponding wrapper function for each generic method, which can handle type erasure and restoration; Generate automatic registration code to register the type and its corresponding method implementation to the global registry.

[0029] The core technical principles of this method include: 1. Type erasure and recovery: ① Use `std::any::Any` to convert the generic parameter to `Box`<dyn Any> Perform type erasure; ②The specific type is identified and restored at runtime using `TypeId`.

[0030] 2. Registry Mode: ① Generate a global registry for each trait, mapping `TypeId` to generic method implementations; ②Supports dynamic lookup and retrieval at runtime.

[0031] 3. Extend the trait mechanism: ① Automatically generate extended traits, providing a generic method interface for `dyn BaseTrait`; ② Maintain the same calling interface as the original generic method.

[0032] In this method, when a developer calls a generic method through a trait object, the generic method call needs to be executed during the runtime call step phase.

[0033] In step S130, the developer calls a generic method (such as `safe_dyn.generic_process(42i32)`) through the trait object, but actually calls the corresponding method of the extended trait. The method call entry point in step S130 is `trait_object.generic_method(input)`, which actually calls the corresponding method in the automatically generated extended trait. The actual type ID of the current trait object instance is obtained through `self.type_id()`, where the type ID is a unique identifier assigned internally by Rust for each type.

[0034] In step S140, the corresponding generic method implementation is searched in the registry, and the corresponding method implementation function pointer is searched in the global registry using the type ID. The registry is implemented in the form of HashMap, which has high search efficiency (time complexity O(1)).

[0035] In step S150, type processing is performed, converting the input generic parameter into a unified dynamic type `Box`. <dynany>`(type erasure) for unified passing; call the found method to implement the function pointer; inside the wrapper function, transfer the input parameters from `Box`<dyn Any> The code reverts to the original type (type restoration). Then, it executes the original implementation: calling the original generic method implementation of the concrete type to execute the actual business logic. Finally, it returns the result to the caller.

[0036] This method uses procedural macros to automatically generate the necessary code at compile time, enabling the calling of generic methods through a traitobject. In short, the entire process can be summarized as follows: 1. Automatic code generation during compilation: When a developer marks a trait with `#[dyn_generic]`, an extended trait and a global registry are automatically generated. When developers mark `#[impl_generic]` on a trait implementation, wrapper functions and registration code are automatically generated.

[0037] 2. Dynamic runtime invocation: When developers call generic methods through a trait object, they first obtain the type ID of the current instance; The method implementation is achieved by searching for the corresponding method in the global registry using the type ID; Convert generic parameters into a uniform format for transmission; The original type is restored in the wrapper function, and the actual method implementation is executed. This design maintains type safety while enabling efficient dynamic calls, thus addressing a core limitation of the Rust language.

[0038] Implementation details of this method: I. Type Identification and Transmission Mechanism This invention uses the `std::any::Any` trait and `TypeId` to implement type identification and transmission: 1. Type identification: via `TypeId::of::` <t>`()` retrieves the unique identifier of the type; 2. Type passing: Convert the generic parameter to `Box`<dyn Any> ` to transmit; 3. Type restoration: Restore the original type at runtime using the `downcast_ref` or `downcast` methods.

[0039] II. Registry Design and Implementation The registry is one of the core components of this invention, used to store the mapping from types to generic method implementations: 1. Registry structure: using `HashMap`<TypeId, FnPtr> Mapping from storage type to function pointer; 2. Thread safety: Automatically adds `Send + Sync + 'static' constraints to ensure thread safety; 3. Search efficiency: Direct search using `TypeId` has a time complexity of O(1).

[0040] III. Extended Trait Mechanism The extended trait mechanism ensures compatibility with the original generic method interface: 1. Automatic generation: Automatically generate extended traits for each trait containing generic methods; 2. Interface consistency: The method signature of the extended trait is completely identical to that of the original generic method; 3. Dynamic dispatch: Implement extended traits for `dyn BaseTrait` to enable dynamic invocation.

[0041] The following are typical usage examples of this invention: rust use dyn_generic::{dyn_generic, impl_generic}; use std::any::Any; use std::fmt::Debug; / / 1. Define the basic trait (Any must be implemented) pub trait SafeForDyn: Any { fn get_name(&self) -> &str; } / / 2. Define a trait that contains generic methods (using the #[dyn_generic] macro) #[dyn_generic(base = "SafeForDyn")] pub trait SafeForDynGeneric { fn generic_process<U: Debug + 'static> (&self, input: U) -> String; } / / 3. Implement basic traits struct Processor; impl SafeForDyn for Processor { fn get_name(&self) -> &str { "Processor"} } / / 4. Implement generic methods (using the #[impl_generic] macro) #[impl_generic] impl SafeForDynGeneric for Processor { #[generic(i32, u64, f64, String)] fn generic_process<U: Debug + 'static> (&self, input: U) -> String{ format!("Processed: {:?}", input) } } / / 5. Calling generic methods through trait objects fn main() { let processor = Processor; let safe_dyn: &dyn SafeForDyn = &processor; / / Import extended traits use crate::SafeForDynGenericExt; / / Dynamically calling generic methods let int_result = safe_dyn.generic_process(42i32); let float_result = safe_dyn.generic_process(3.14159f64); let string_result = safe_dyn.generic_process("Hello".to_string()); println!("Int result: {}", int_result); println!("Float result: {}", float_result); println!("String result: {}", string_result); } ``` Using the procedural macro library provided by this invention, developers only need to write code according to the examples above and then compile it normally using the Rust compiler. The macros will automatically generate the necessary extended code, registry entries, and wrapper functions during compilation, without requiring any additional compilation steps.

[0042] Beneficial technical effects of this method I. Core Technology Effects 1. Breaking native limitations: Successfully resolved the core limitation in Rust that trait objects do not support generic methods; 2. Full type safety: Preserves all type information, ensuring compile-time type checking and runtime type safety; 3. Zero runtime overhead: Runtime type lookup is directly mapped via `TypeId`, resulting in minimal performance overhead; 4. Seamless integration: Perfectly compatible with the existing Rust ecosystem, requiring no modification to existing code structure.

[0043] II. Improved Developer Experience 1. Simplified development process: Developers only need to add simple macro annotations, without having to write complex type conversion and dispatch code. 2. Improve code reusability: It can simultaneously leverage the code reusability advantages of generics and the dynamic polymorphism capabilities of trait objects. 3. Enhanced abstraction capabilities: Supports more complex design patterns and architectural implementations. Third, this invention opens up new application scenarios for Rust development: 1. Plugin System: Supports dynamically loading and calling generic methods of plugins through trait objects. 2. Dependency Injection Framework: Implements type-safe dependency injection and dynamic service discovery. 3. Polymorphic data processing: Supports processing different types of data through a unified interface. 4. Dynamic Configuration System: Supports runtime dynamic configuration and expansion of system functions. This invention provides a more powerful abstraction capability for the Rust language's trait system, and has broad application prospects: 1. In the field of systems programming: supporting more complex system components and plug-in architectures; 2. Network programming: Implementing more flexible network protocols and service frameworks; 3. Embedded development: Achieving efficient polymorphic design in resource-constrained environments; 4. Rust ecosystem: Provides core technical support for frameworks such as Rust's dependency injection and plugin system.

[0044] This invention successfully overcomes the core limitation of Rust's trait objects not supporting generic methods through innovative procedural macro technology, achieving type-safe, zero-runtime-overhead dynamic generic method invocation. This invention not only improves Rust's abstraction capabilities and development efficiency but also opens up new possibilities for the development of the Rust ecosystem, possessing significant technical value and broad application prospects. Example

[0045] Corresponding to the method in Example 1, such as Figure 2 As shown, Figure 2 This is a schematic diagram of the overall structure of an embodiment of the second embodiment of the present invention. The present invention also provides a system 2 for calling generic methods in the Rust language, including a compile-time extension module 210 and a runtime calling module 220. The compile-time extension module 210 includes: The first process macro unit is configured to: parse the first trait defined by the developer that contains generic methods, and automatically generate an extended trait containing the same generic method signature based on the first trait; The second process macro unit is configured to: parse the developer's implementation code for the first trait for a specific type, generate a corresponding wrapper function for each generic method in the implementation code, and generate registration code that associates the specific type with the corresponding wrapper function to build a global registry; wherein the wrapper function is capable of handling type erasure and restoration; A global registry used to store the mapping relationship between specific types and their corresponding wrapper functions; The runtime invocation module 220 is configured to perform the following steps when the generic method is invoked through a trait object that implements the base trait: Use the extended trait corresponding to the trait object as the trait object to obtain the specific type represented by the trait object; Based on the specific type represented by the trait object, the global registry is queried to find the target wrapper function corresponding to the specific type represented by the trait object; The target wrapper function is called on the trait object to execute the original generic method implementation on the specific type and return the execution result.

[0046] In one implementation, the generated wrapper function is configured to receive type-erased input parameters. Inside the wrapper function, the type-erased input parameters are forcibly converted to the original concrete type, and the original generic method written by the developer is called using the converted concrete type parameters.

[0047] In one implementation, the process of calling the target wrapper function on the Trait object to execute the original generic method implementation on the specific type includes: Convert the generic parameter of the current Trait object instance to `Box`<dyn Any> `; Inside the wrapper function, the input parameter is transferred from `Box`<dyn Any> Restored to its original type; Execute the original implementation.

[0048] In one implementation, the registry is implemented as a HashMap.

[0049] In one implementation, the registry automatically adds the `Send + Sync + 'static' constraint.

[0050] In one implementation, the registry is looked up directly using `TypeId`.

[0051] In one implementation, the execution steps further include: The extended Trait is automatically implemented by the dynamic type of the base Trait object, thereby providing the call entry point for generic methods of the Trait object; The entry point is configured to trigger the runtime invocation step during runtime.

[0052] In this device, code is automatically generated at compile time: when a developer marks a trait with `#[dyn_generic]`, an extended trait and a global registry are automatically generated; when a developer marks a trait implementation with `#[impl_generic]`, a wrapper function and registration code are automatically generated. Dynamic runtime invocation: when a developer calls a generic method through a trait object, the type ID of the current instance is first obtained; the type ID is used to find the corresponding method implementation in the global registry; the generic parameters are converted to a unified format for transmission; the original type is restored in the wrapper function, and the actual method implementation is executed. This device successfully solves the core limitation of Rust language where trait objects do not support generic methods, achieving type-safe, zero-runtime-overhead dynamic generic method invocation. Example

[0053] This invention also provides a storage medium storing computer instructions that, when executed by a processor, implement the Rust language method for calling generic methods in any of the above embodiments.

[0054] Those skilled in the art will understand that all or part of the steps of the above method embodiments can be implemented by hardware related to program instructions. The aforementioned program can be stored in a computer-readable storage medium. When the program is executed, it performs the steps of the above method embodiments. The aforementioned storage medium includes various media capable of storing program code, such as mobile storage devices, random access memory (RAM), read-only memory (ROM), magnetic disks, or optical disks.

[0055] Alternatively, if the integrated units of the present invention are implemented as software functional modules and sold or used as independent products, they can also be stored in a computer-readable storage medium. Based on this understanding, the technical solutions of the embodiments of the present invention, or the parts that contribute to related technologies, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, terminal, or network device, etc.) to execute all or part of the methods of the various embodiments of the present invention. The aforementioned storage medium includes various media capable of storing program code, such as mobile storage devices, RAM, ROM, magnetic disks, or optical disks.

[0056] Corresponding to the computer storage medium described above, one embodiment also provides a computer device, which includes a memory, an encoder, and a computer program stored in the memory and executable on the encoder, wherein the encoder executes the program to implement any of the Rust language methods for calling generic methods as described in the above embodiments.

[0057] The aforementioned computer device automatically generates code during compilation: when a developer marks a trait with `#[dyn_generic]`, an extended trait and a global registry are automatically generated; when a developer marks a trait implementation with `#[impl_generic]`, a wrapper function and registration code are automatically generated. Runtime dynamic invocation: when a developer calls a generic method through a trait object, the type ID of the current instance is first obtained; the type ID is used to find the corresponding method implementation in the global registry; the generic parameters are converted to a unified format for transmission; and the original type is restored in the wrapper function, and the actual method implementation is executed. This computer device successfully overcomes the core limitation of Rust's trait object not supporting generic methods, achieving type-safe, zero-runtime-overhead dynamic generic method invocation.

[0058] The technical features of the above embodiments can be combined in any way. For the sake of brevity, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.

[0059] Obviously, the above embodiments of the present invention are merely examples for clearly illustrating the present invention, and are not intended to limit the implementation of the present invention. Those skilled in the art can make other variations or modifications based on the above description. It is neither necessary nor possible to exhaustively describe all embodiments here. Any modifications, equivalent substitutions, and improvements made within the spirit and principles of the present invention should be included within the scope of protection of the claims of the present invention.< / t> < / dynany> < / t> < / t>

Claims

1. A method for calling generic methods in the Rust language, characterized in that, include: Compile-time expansion steps and runtime invocation steps The compile-time expansion step includes: The first process macro parses the first trait defined by the developer and contains generic methods, and automatically generates an extended trait containing the same generic method signature based on the first trait; The second process involves macro parsing the developer's implementation code for the first trait for specific types, generating corresponding wrapper functions for each generic method in the implementation code, and generating registration code that associates the specific type with the corresponding wrapper function to construct a global registry; the global registry is used to store the mapping relationship between specific types and corresponding wrapper functions; wherein, the wrapper function can handle type erasure and restoration; The runtime invocation steps include: In response to the trait object's call instruction to a generic method, the extended trait corresponding to the trait object is used as the current trait object to obtain the specific type represented by the current trait object instance; Based on the specific type represented by the current trait object instance, the global registry is queried to find the target wrapper function corresponding to the specific type represented by the current trait object instance; The target wrapper function is invoked for the current trait object instance to execute the original generic method implementation on the specific type and return the execution result.

2. The method for calling generic methods in Rust language according to claim 1, characterized in that, The generated wrapper function is configured to receive type-erased input parameters. Inside the wrapper function, the type-erased input parameters are forcibly converted to the original concrete type, and the original generic method written by the developer is called using the converted concrete type parameter.

3. The method for calling generic methods in Rust language according to claim 2, characterized in that, The process of forcibly converting the type-erased input parameter to its original concrete type within the wrapper function includes: Convert the generic parameter to a dynamically typed container `Box`<dyn Any> `; The process of calling the target wrapper function for the current trait object instance to execute the original generic method implementation on the specific type includes: Convert the generic parameter of the current trait object instance to `Box`<dyn Any> `; Inside the wrapper function, the input parameter is transferred from `Box`<dyn Any> Restored to its original type; Execute the original implementation.

4. The method for calling generic methods in Rust language according to claim 1, characterized in that, The registry is implemented as a HashMap.

5. The method for calling generic methods in Rust language according to claim 1, characterized in that, The registry automatically adds the `Send + Sync + 'static' constraint.

6. The method for calling generic methods in Rust language according to claim 1, characterized in that, The registry can be searched directly using `TypeId`.

7. The method for calling generic methods in Rust language according to claim 1, characterized in that, The compile-time expansion step also includes: The extended trait is automatically implemented by the dynamic type of the base trait object, thereby providing the entry point for calling generic methods for the trait object; The entry point is configured to trigger the runtime invocation step during runtime.

8. A system for calling generic methods in the Rust language, characterized in that, include: Compile-time extension modules and runtime calling modules; The compile-time extension module includes: The first process macro unit is configured to: parse the first trait defined by the developer that contains generic methods, and automatically generate an extended trait containing the same generic method signature based on the first trait; The second process macro unit is configured to: parse the developer's implementation code for the first trait for a specific type, generate a corresponding wrapper function for each generic method in the implementation code, and generate registration code that associates the specific type with the corresponding wrapper function to build a global registry; wherein the wrapper function is capable of handling type erasure and restoration; A global registry used to store the mapping relationship between specific types and their corresponding wrapper functions; The runtime invocation module is configured to perform the following steps when the generic method is invoked through a trait object that implements the base trait: Use the extended trait corresponding to the trait object as the trait object to obtain the specific type represented by the trait object; Based on the specific type represented by the trait object, the global registry is queried to find the target wrapper function corresponding to the specific type represented by the trait object; The target wrapper function is called on the trait object to execute the original generic method implementation on the specific type and return the execution result.

9. A computer device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that, When the processor executes the program, it implements the method as described in any one of claims 1-7.

10. A computer-readable storage medium having a computer program stored thereon, characterized in that, When the program is executed by the processor, it implements the method as described in any one of claims 1-7.