Method and system for constructing multi-layer nested structure body based on Rust macro
Constructing multi-layer nested structures through Rust macros solves the complex problem of writing multi-layer nested structures in Rust, achieving rapid construction and efficient data processing, and improving the readability and maintainability of the code.
Patent Information
- Application Number
- CN202510430447.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-04-08
- Publication Date
- 2025-07-22
AI Technical Summary
When writing multi-layer nested structures in Rust, the existing technology is complex in operation and is not suitable for large-scale data processing, resulting in inefficiency.
The method of constructing multi-layer nested structures is adopted by Rust macros. The input data is converted into an abstract syntax tree through the parsing library, logical recursive analysis is performed, and a flat one-dimensional structure list is generated. The quote library is used for compilation and output, combining global counters and visibility declarations to achieve rapid construction.
It realizes the rapid construction of multi-layer nested structures, improves the efficiency of large-scale data processing, simplifies the encoding process, and improves the readability and maintainability of code.
Smart Images

Figure CN120353446A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of data processing, and particularly relates to a method and system for constructing a multi-layer nested structure based on Rust macros. Background Art
[0002] In programming or data structures, nesting refers to a situation where one structure contains another structure inside. And multi-layer nesting means that this nesting relationship occurs multiple times, forming a hierarchical structure. Such a structure can effectively organize and manage complex data or code; multi-layer nesting is very useful when dealing with and solving complex problems. It can effectively organize and divide code or data, making it more readable and maintainable. For example, in scenarios such as operating systems, file management systems, database management systems, etc., a large amount of data and complex logic need to be processed, and multi-layer nesting can effectively help understand and handle these problems.
[0003] When writing multi-layer nested structures in Rust, it is only possible to complete the declaration by writing each layer of sub-structures step by step and referencing each other, and the operation is relatively complex, which is not conducive to large-scale data processing. Summary of the Invention
[0004] One of the purposes of the present invention is to provide a method for constructing a multi-layer nested structure based on Rust macros, which can realize the rapid construction of a multi-layer nested structure to cope with the scenario of large-scale data processing and improve data processing efficiency.
[0005] A method for constructing a multi-layer nested structure based on Rust macros provided by an embodiment of the present invention includes:
[0006] Analyze the input data to determine whether it meets the call condition;
[0007] When it meets the condition, call the Rust macro to process the input data and construct a multi-layer nested structure corresponding to the input data.
[0008] Preferably, the processing steps of the Rust macro for the input data are as follows:
[0009] Based on a pre-configured parsing library, parse the input data into an abstract syntax tree;
[0010] Perform logical recursive analysis on the abstract syntax tree to obtain an analysis result;
[0011] Collect and integrate the analysis results to obtain a flattened one-dimensional structure list;
[0012] Compile and output each structure in the one-dimensional structure list.
[0013] Preferably, the parsing library includes the syn library.
[0014] Preferably, compile and output each structure in the one-dimensional structure list, including:
[0015] Use the quote library of Rust to compile and output each structure.
[0016] Preferably, create an isolated scope, write out the structures, enums, and objects contained in the array object in the list in order, and configure the external visibility according to the visibility declaration of the root element.
[0017] Preferably, configure a global counter to assign a unique identifier to all anonymous elements in the list.
[0018] The present invention also provides a system for constructing multi-layer nested structures based on Rust macros, including: a call analysis module and a processing module; wherein, the call analysis module analyzes the input data to determine whether it meets the call conditions; when it meets, the processing module calls Rust macros to process the input data and construct the multi-layer nested structure corresponding to the input data.
[0019] Preferably, the processing steps of Rust macros for the input data are as follows:
[0020] Based on a pre-configured parsing library, parse the input data into an abstract syntax tree;
[0021] Conduct a logical recursive analysis on the abstract syntax tree to obtain an analysis result;
[0022] Collect and integrate the analysis results to obtain a flattened one-dimensional structure list;
[0023] Compile and output each structure in the one-dimensional structure list.
[0024] Preferably, the parsing library includes the syn library.
[0025] Preferably, compile and output each structure in the one-dimensional structure list, including:
[0026] Use the quote library of Rust to compile and output each structure.
[0027] Other features and advantages of the present invention will be described in the subsequent specification, and, in part, will be obvious from the specification, or will be understood by implementing the present invention. The objectives and other advantages of the present invention can be achieved and obtained through the structures specifically pointed out in the written specification and the drawings.
[0028] Next, through the drawings and embodiments, the technical solutions of the present invention will be further described in detail. Description of the Drawings
[0029] The accompanying drawings are used to provide a further understanding of the present invention, and constitute a part of the specification. Together with the embodiments of the present invention, they are used to explain the present invention and do not constitute a limitation to the present invention. In the accompanying drawings:
[0030] Figure 1 It is a schematic diagram of a method for constructing a multi-layer nested structure based on Rust macros in an embodiment of the present invention;
[0031] Figure 2 It is a schematic diagram of a system for constructing a multi-layer nested structure based on Rust macros in an embodiment of the present invention. Detailed implementation manners
[0032] The following describes the preferred embodiments of the present invention with reference to the accompanying drawings. It should be understood that the preferred embodiments described herein are only used to illustrate and explain the present invention and are not used to limit the present invention.
[0033] An embodiment of the present invention provides a method for constructing a multi-layer nested structure based on Rust macros, as Figure 1 shown, including:
[0034] Step 1: Analyze the input data to determine whether it meets the call conditions;
[0035] Step 2: When it meets the conditions, call the Rust macro to process the input data and construct a multi-layer nested structure corresponding to the input data.
[0036] Among them, the processing steps of the Rust macro for the input data are as follows:
[0037] Based on a pre-configured parsing library, parse the input data into an abstract syntax tree;
[0038] Conduct a logical recursive analysis on the abstract syntax tree to obtain an analysis result;
[0039] Collect and integrate the analysis results to obtain a flattened one-dimensional structure list;
[0040] Compile and output each structure in the one-dimensional structure list.
[0041] Among them, the parsing library includes the syn library.
[0042] Among them, compiling and outputting each structure in the one-dimensional structure list includes:
[0043] Use the quote library of Rust to compile and output each structure.
[0044] In addition, create an isolated scope, write out the structures, enums, and objects contained in the array objects in the list in order, and configure the external visibility according to the visibility declaration of the root element.
[0045] In addition, configure a global counter to assign a unique identifier to all anonymous elements in the list.
[0046] To declare a multi-level nested struct, you need to introduce a package (crate) named yuuka from the central library crates.io. To import the package into the project, you can execute "cargo add yuuka" in the terminal.
[0047] For nested structs, the data type is not limited to the Path in the native Rust semantics. It can also be a directly written named struct, unnamed struct, named enum, and unnamed enum.
[0048] For nested structs, the data type can be in single or array form. For the array form, the writing method is to add a pair of square brackets "[...]" on both sides of the original type. After compilation, this type will be automatically converted to a type wrapped by std::vec::Vec. The specific code is as follows:
[0049]
[0050]
[0051] For a struct, each key is a combination of "key: data type" or "key: data type = default value", and multiple keys are separated by commas. If a default value is not specified for the data type, the macro will try to find the implementation of the Default trait for the corresponding type. The specific code is as follows:
[0052]
[0053] For the default value implementation of a struct, the procedural macro will automatically generate an impl Default implementation for each unnamed struct and unnamed enum, and fill in the implementation code according to the actual declaration.
[0054] If a key of an unnamed struct is not declared with a default value, the procedural macro will fill in "Default::default()" at the corresponding position in impl Default to try to call its possible default initialization function.
[0055] If an unnamed enum does not specify a default value, the procedural macro will directly fill in "unimplemented!()" in the implementation of implDefault to ensure that the enum is not called with a non-existent default initialization function.
[0056] To declare a named struct, the format is "struct identifier {...}". Compared with Rust's original declaration method, this writing does not require rewriting the struct keyword.
[0057] To declare an unnamed struct, the format is "{...}", that is, directly use a pair of curly braces as the declaration block of the struct.
[0058] For an enum, each key in it is a combination of "name", "name (data type)", or "name {key: data type,...}", and multiple keys are separated by commas. The default value declaration of the enum should follow after its entire curly brace declaration block, in the format of "= default value". The specific code is as follows:
[0059]
[0060] To declare a named enum, the format is "enum enum identifier {...}". When declaring an enum, the enum keyword must be provided to distinguish it from the struct declaration.
[0061] To declare an unnamed enum, the format is "enum {...}". When declaring an enum, the enum keyword must be provided to distinguish it from the struct declaration.
[0062] You can quickly create an entity object of a nested struct through the auto! macro. Among them, only the name of the root struct needs to be clearly specified, and the names of all sub-structs below do not need to be rewritten. The specific code is as follows:
[0063]
[0064] You can quickly create an entity object of a nested enum through the auto! macro. Among them, only the enum keys used at each level need to be continuously specified, and there is no need to rewrite the parentheses in the middle to adapt to Rust's native syntax. The specific code is as follows:
[0065]
[0066] By using the macros_recursive attribute macro, the attribute macros wrapped by it can be supplemented to the beginning of all its subordinate sub - structures. This kind of attribute macro can effectively avoid potential vulnerabilities caused by missing declarations, and ensure that when any modification is made to the future structure, the required attribute macro declarations of all its subordinate sub - structures can be maintained. The specific code is as follows:
[0067]
[0068]
[0069] By using macro_export, the_auto! declaration macro generated by the derive_struct! macro can be made public outside the package to achieve cross - package (Crate) calls to the_auto! macro. The specific code is as follows:
[0070]
[0071] As shown in the following code, by using macro_export, the_auto! declaration macro generated by the derive_enum! macro can be made public outside the package to achieve cross - package (Crate) calls to the_auto! macro.
[0072]
[0073] When making cross - package (Crate) calls to the_auto! macro, it is necessary to ensure that the root node type is declared at the top of the package (Crate), that is, the derive_struct! macro and derive_enum! macro used to build nested declarations need to be written or re - exported under the "lib.rs" file.
[0074] The package (Crate) used for cross - package (Crate) calls needs to be compilable as a Rust link library (rlib). To achieve this, the "[lib]" field in the package (Crate)'s description file "Cargo.toml" needs to declare at least "crate - type = ["rlib"]".
[0075] This patent can be applied to various configuration files. The following code is used to declare a classic server configuration.
[0076]
[0077]
[0078]
[0079] It contains multiple layers of configuration information for different contents, and is used to parse and deserialize the JSON configuration text as shown in the following code.
[0080]
[0081] This patent can be applied to the internationalization requirements of various software texts. The code for declaring a simple multi-language identifier table is as follows:
[0082]
[0083]
[0084] It contains description information at different levels, which is used to parse and deserialize the JSON configuration text corresponding to the following code and can be directly referenced in the software.
[0085]
[0086]
[0087] In order to achieve reasonable invocation, in one embodiment, the input data is analyzed to determine whether it meets the invocation conditions, including:
[0088] Parse the input data to determine whether there is invocation identification data. When it exists, it meets the invocation conditions;
[0089] Among them, the call identification data can be input by the user when inputting data. The input method can be that the user types it in or it can be automatically inserted after clicking on a quick command button floating on the input interface. The call identification data can also be automatically inserted according to the user's input situation. The analysis of the input situation includes: configuration situation parameters, trigger parameters, and trigger thresholds. The situation parameters characterize the current input situation. The trigger parameter is based on the analysis of the user's input to determine whether to trigger a change in the situation parameters. When the user initially uses it, the configuration situation parameters are set to the initial value, which can be set to 0. At this time, it represents that the input situation is blank. The trigger parameter is also set to the initial value, which is also 0. The trigger threshold cannot be set to zero or one and can be set to any value from 2 to 100. Taking 10 as an example, when the input data entered by the user is used for the construction of a multi-layer nested structure, the trigger parameter is incremented by one. When the trigger parameter reaches the trigger threshold, the situation parameter is set to 1. At this time, it represents that the input situation is a dedicated input environment for constructing a multi-layer nested structure. At this time, there is no need for the user to type in the call identification data or click on the quick command button, and it is automatically inserted by the system. When the user input data is used for non-construction of a multi-layer nested structure, the trigger parameter is reset to the initial value. In addition, a timer and a timing threshold can be configured. When the automatic input is triggered, the timer starts timing. When the time of the timer is greater than the timing threshold, the trigger parameter is decremented by one. By using the timer and the timing threshold, the user's operation time can be analyzed to exit the automatic insertion state and facilitate the user's input. Among them, the analysis of the timing threshold is mainly obtained by jointly analyzing the time interval between the user's historical input operations and the specific value of the trigger parameter. The time interval between historical input operations characterizes the characteristics of the user's single operation, and the specific value of the trigger parameter characterizes the characteristics of the user's current input task. The sum of the product of the average value of the time interval, the value of the trigger parameter, and their respective weight coefficients, plus a pre-configured constant, is used to obtain the timing threshold. Among them, the sum value of the weight coefficients is 1, and the constant is analyzed and configured by professionals according to the interface switching time and the human reaction time.
[0090] To achieve the exit of automatic insertion, corresponding buttons can be configured on the input interface; however, there are sometimes situations where users are not aware that they have entered the automatic insertion mode, or are not familiar with the interface and cannot perform the subjective operation of exiting the automatic insertion mode. Analyzing the input data also includes: configuring an input data matching module, a judgment parameter, and a judgment threshold; the judgment parameter is initially set to zero, and the judgment threshold is any value from 2 to 10; the input data matching module matches the input data with the previous input data. When they are the same, the judgment parameter is incremented by one; when the judgment parameter is greater than or equal to the judgment threshold, the automatic insertion mode is exited; generally, the judgment threshold is configured to be 2; in actual use, it is possible that the two input data are exactly the same. For example, due to user misoperation, but the probability of three consecutive identical inputs in the scenario of constructing a multi-layer nested structure is extremely low. Therefore, when the user does not need to perform the operation of constructing a multi-layer nested structure of input data, that is, the user's input data is not for constructing a multi-layer nested structure, but due to the mode, construction is performed. After the first input, a second attempt will be made; since the reason is not found, a third attempt will be made, and after the third attempt, the automatic insertion mode is exited, which can achieve automatic exit in the case of not being aware of the automatic insertion mode and avoid interference of the automatic insertion mode on other operations of the user.
[0091] The present invention also provides a system for constructing a multi-layer nested structure based on Rust macros, as Figure 2 shown, including: a call analysis module 1 and a processing module 2; wherein, the call analysis module 1 analyzes the input data to determine whether it meets the call conditions; when it meets the conditions, the processing module 2 calls Rust macros to process the input data and constructs a multi-layer nested structure corresponding to the input data.
[0092] The call conditions include: there is a pre-configured call instruction code in the input data, or, the data processing in the previous preset number of times (greater than or equal to 2 times) is all processing operations for constructing a multi-layer nested structure and the current operation time is less than or equal to a preset time threshold (any value from 100 ms to 1 s);
[0093] Among them, the processing steps of Rust macros for the input data are as follows:
[0094] Based on a pre-configured parsing library, the input data is parsed into an abstract syntax tree;
[0095] Perform logical recursive analysis on the abstract syntax tree to obtain an analysis result;
[0096] Collect and integrate the analysis results to obtain a flattened one-dimensional structure list;
[0097] Compile and output each structure in the one-dimensional structure list.
[0098] Among them, the parsing library includes the syn library.
[0099] Among them, compiling and outputting each structure in the one-dimensional structure list includes:
[0100] Compiling and outputting each structure with the quote library of Rust.
[0101] Obviously, those skilled in the art can make various changes and modifications to the present invention without departing from the spirit and scope of the present invention. Thus, if these modifications and variations of the present invention fall within the scope of the claims of the present invention and their equivalent technologies, the present invention also intends to include these modifications and variations.
Claims
1. A method for constructing multi-layer nested structures based on Rust macros, characterized in that, including: Analyze the input data to determine whether it meets the call conditions; When it meets the conditions, call the Rust macro to process the input data and construct a multi-level nested structure corresponding to the input data.
2. The method for constructing a multi-layer nested structure based on Rust macros as described in claim 1, wherein, The processing steps of the Rust macro for the input data are as follows: Based on a pre-configured parsing library, parse the input data into an abstract syntax tree; Perform a logical recursive analysis on the abstract syntax tree to obtain an analysis result; Collect and integrate the analysis results to obtain a flattened one-dimensional structure list; Compile and output each structure in the one-dimensional structure list.
3. The method for constructing a multi-layer nested structure based on Rust macros according to claim 2, wherein The parsing library includes the syn library.
4. The method for constructing a multi-layer nested structure based on Rust macros according to claim 2, wherein Compiling and outputting each structure in the one-dimensional structure list includes: Compiling and outputting each structure with Rust's quote library.
5. The method for constructing a multi-layer nested structure based on Rust macros according to claim 2, wherein, Create an isolated scope, write out the structures, enums, and objects contained in the array objects in the list in order, and configure the external visibility according to the visibility declaration of the root element.
6. The method for constructing a multi-layer nested structure based on Rust macros according to claim 2, wherein, Configure a global counter to assign a unique identifier to all anonymous elements in the list.
7. A system for constructing multi-layer nested structures based on Rust macros, characterized in that including: Call the analysis module and the processing module; among them, the analysis module analyzes the input data to determine whether it meets the call conditions; When it meets the conditions, the processing module calls the Rust macro to process the input data and construct a multi-level nested structure corresponding to the input data.
8. The system for constructing a multi-layer nested structure based on Rust macros as claimed in claim 7, wherein, The processing steps of the Rust macro for the input data are as follows: Based on a pre-configured parsing library, parse the input data into an abstract syntax tree; Perform a logical recursive analysis on the abstract syntax tree to obtain an analysis result; Collect and integrate the analysis results to obtain a flattened one-dimensional structure list; Compile and output each structure in the one-dimensional structure list.
9. The system for constructing a multi-layer nested structure based on Rust macros according to claim 8, wherein, The parsing library includes the syn library.
10. The system for constructing a multi-layer nested structure based on Rust macros as claimed in claim 8, wherein Compiling and outputting each structure in the one-dimensional structure list includes: Compiling and outputting each structure with Rust's quote library.