Communication method and device for realizing cross-three-terminal system based on Rust, equipment and medium
By defining the language interaction target on the Rust side and using process macros to generate intermediary links across language communication, the problem of needing to write multiple sets of FFI codes when communicating across mobile three-end systems is solved, and efficient cross-three-end system communication development is achieved.
Patent Information
- Application Number
- CN202411558873.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-04
- Publication Date
- 2025-02-18
AI Technical Summary
When the existing technology realizes communication between three mobile terminals (Android, iOS, and Hongmeng), two sets of FFI codes are required to write, which increases development costs and reduces development efficiency.
By defining a set of language interaction goals on the Rust side, using process macros to generate cross-language communication intermediate links for each operating system, cross-language data communication between Android, iOS, Hongmeng operating system and Rust is realized.
It realizes that only one set of programming program definition data is required on the Rust side, so that communication across three-end systems can be realized, significantly improving the efficiency of language programming and development across three-end systems.
Smart Images

Figure CN119473248A_ABST
Abstract
Description
Technical Field
[0001] Embodiments of the present disclosure relate to the field of cross-language communication technology, and in particular, to a communication method, apparatus, device, and medium applicable to implementing a cross-three-terminal system based on Rust. Background Art
[0002] Language communication between operating systems can facilitate the improvement of optimization efficiency during the implementation of some underlying logical frameworks between different operating systems.
[0003] In the related technology, the language communication between the operating systems at both ends is mainly implemented based on the corresponding components. For example, the communication between the Rust programming language and the Android / iOS programming language is implemented only through the uniffi-rs component, or the communication between the Rust programming language and the Hongmeng programming language is implemented only through the ohos-rs component. Since these two types of communications are implemented based on different operating platforms and there are large differences in the operation implementation process, if on this basis, you want to achieve communication across three mobile terminals (Android, iOS, and Hongmeng), you need to write two sets of FFI (Foreign Function Interface) codes, which undoubtedly increases the development cost of the FFI code and leads to low development efficiency of cross-language programming. Summary of the invention
[0004] The embodiments described in this article provide a method, apparatus, device and medium for implementing communication across a three-terminal system based on Rust, which overcomes the above-mentioned problems.
[0005] In a first aspect, according to the content of the present disclosure, a method for implementing communication across three-terminal systems based on Rust is provided, including:
[0006] Obtain a language interaction target for cross-language data communication between three-terminal systems, where the three-terminal systems include: a first operating system, a second operating system, and a third operating system, where the first operating system is used to describe an Android operating system, the second operating system is used to describe an iOS operating system, and the third operating system is used to describe a Hongmeng operating system, and the language interaction target is used to describe an interaction operation performed on cross-language interaction data in the first operating system, the second operating system, and the third operating system;
[0007] Performing macro definition on the language interaction target to obtain macro definition data corresponding to the language interaction target;
[0008] Based on the macro definition data corresponding to the language interaction target, a cross-language communication intermediate link corresponding to the first operating system, a cross-language communication intermediate link corresponding to the second operating system, and a cross-language communication intermediate link corresponding to the third operating system are generated, so that the first operating system performs cross-language data communication with the Rust programming language by calling the cross-language communication intermediate link corresponding to the first operating system, the second operating system performs cross-language data communication with the Rust programming language by calling the cross-language communication intermediate link corresponding to the second operating system, and the third operating system performs cross-language data communication with the Rust programming language by calling the cross-language communication intermediate link corresponding to the third operating system.
[0009] In a second aspect, according to the content of the present disclosure, a communication device for implementing a cross-three-terminal system based on Rust is provided, including:
[0010] an acquisition module, used to acquire a language interaction target for cross-language data communication between three-terminal systems, the three-terminal systems comprising: a first operating system, a second operating system, and a third operating system, the first operating system being used to describe an Android operating system, the second operating system being used to describe an iOS operating system, the third operating system being used to describe a Hongmeng operating system, and the language interaction target being used to describe an interaction operation performed on cross-language interaction data in the first operating system, the second operating system, and the third operating system;
[0011] A definition module, used to perform macro expression definition on the language interaction target and obtain macro definition data corresponding to the language interaction target;
[0012] A generation module is used to generate a cross-language communication intermediate link corresponding to the first operating system, a cross-language communication intermediate link corresponding to the second operating system, and a cross-language communication intermediate link corresponding to the third operating system based on the macro definition data corresponding to the language interaction target, so that the first operating system performs cross-language data communication with the Rust programming language by calling the cross-language communication intermediate link corresponding to the first operating system, the second operating system performs cross-language data communication with the Rust programming language by calling the cross-language communication intermediate link corresponding to the second operating system, and the third operating system performs cross-language data communication with the Rust programming language by calling the cross-language communication intermediate link corresponding to the third operating system.
[0013] In a third aspect, a computer device is provided, including a memory and a processor, wherein a computer program is stored in the memory, and when the processor executes the computer program, the steps of a method for implementing a communication method across a three-terminal system based on Rust in any of the above embodiments are implemented.
[0014] In a fourth aspect, a computer-readable storage medium is provided, on which a computer program is stored. When the computer program is executed by a processor, the steps of a method for implementing a communication method across a three-terminal system based on Rust in any of the above embodiments are implemented.
[0015] The embodiment of the present application provides a method for implementing communication across three-terminal systems based on Rust, obtaining a language interaction target for cross-language data communication between the three-terminal systems, where the three-terminal systems include: a first operating system, a second operating system, and a third operating system, wherein the first operating system is used to describe an Android operating system, the second operating system is used to describe an iOS operating system, and the third operating system is used to describe a Hongmeng operating system, and the language interaction target is used to describe interaction operations performed on cross-language interaction data in the first operating system, the second operating system, and the third operating system; a macro representation definition is performed on the language interaction target to obtain macro definition data corresponding to the language interaction target; based on the macro definition data corresponding to the language interaction target, a cross-language communication intermediate link corresponding to the first operating system, a cross-language communication intermediate link corresponding to the second operating system, and a cross-language communication intermediate link corresponding to the third operating system are generated, so that the first operating system performs cross-language data communication with the Rust programming language by calling the cross-language communication intermediate link corresponding to the first operating system, the second operating system performs cross-language data communication with the Rust programming language by calling the cross-language communication intermediate link corresponding to the second operating system, and the third operating system performs cross-language data communication with the Rust programming language by calling the cross-language communication intermediate link corresponding to the third operating system. In this way, by only performing data definition for one set of programming programs on the Rust side, it is easy to achieve communication across three-terminal systems, effectively improving the efficiency of language programming development across three-terminal systems.
[0016] The above description is only an overview of the technical solution of the embodiment of the present application. In order to more clearly understand the technical means of the embodiment of the present application, it can be implemented in accordance with the contents of the specification. In order to make the above and other purposes, features and advantages of the embodiment of the present application more obvious and easy to understand, the specific implementation methods of the present application are listed below. BRIEF DESCRIPTION OF THE DRAWINGS
[0017] In order to more clearly illustrate the technical solutions of the embodiments of the present disclosure, the drawings of the embodiments will be briefly described below. It should be noted that the drawings described below only relate to some embodiments of the present disclosure, but are not intended to limit the present disclosure, wherein:
[0018] Figure 1 It is a flowchart of a method for implementing a communication across three-terminal systems based on Rust provided in the present disclosure.
[0019] Figure 2 It is a structural diagram of a communication device for implementing a cross-three-terminal system based on Rust provided by the present disclosure.
[0020] Figure 3 It is a structural schematic diagram of a computer device provided by the present disclosure.
[0021] It should be noted that the elements in the drawings are schematic and not drawn to scale. DETAILED DESCRIPTION
[0022] In order to make the purpose, technical solution and advantages of the embodiments of the present disclosure clearer, the technical solution of the embodiments of the present disclosure will be clearly and completely described below in conjunction with the accompanying drawings. Obviously, the described embodiments are part of the embodiments of the present disclosure, rather than all of the embodiments. Based on the described embodiments of the present disclosure, all other embodiments obtained by those skilled in the art without creative work also fall within the scope of protection of the present disclosure.
[0023] Unless otherwise defined, all terms (including technical and scientific terms) used herein have the same meaning as commonly understood by a person skilled in the art to which the subject matter of the present disclosure belongs. It will be further understood that terms such as those defined in commonly used dictionaries should be interpreted as having a meaning consistent with their meaning in the context of the specification and the relevant art, and will not be interpreted in an idealized or overly formal form unless otherwise explicitly defined herein. As used herein, a statement that two or more parts are "connected" or "coupled" together shall mean that the parts are joined together directly or through one or more intermediate components.
[0024] Reference to "embodiments" herein means that a particular feature, structure, or characteristic described in conjunction with the embodiments may be included in at least one embodiment of the present application. The appearance of the phrase "embodiments" in various locations in the specification does not necessarily refer to the same embodiment, nor is it an independent or alternative embodiment that is mutually exclusive with other embodiments. It is explicitly and implicitly understood by those skilled in the art that the embodiments described herein may be combined with other embodiments.
[0025] The term "and / or" in this article is only a description of the association relationship of associated objects, indicating that three relationships may exist. For example, A and / or B can mean: A exists, A and B exist at the same time, and B exists. In addition, the character " / " in this article generally indicates that the associated objects before and after are in an "or" relationship. Terms such as "first" and "second" are only used to distinguish one component (or a part of a component) from another component (or another part of a component).
[0026] In the description of the present application, unless otherwise specified, "plurality" means more than two (including two), and similarly, "multiple groups" means more than two groups (including two).
[0027] In order to enable those skilled in the art to better understand the solution of the present application, the technical solution in the embodiments of the present application will be clearly and completely described below in conjunction with the accompanying drawings.
[0028] Figure 1 is a flow chart of a method for implementing a communication across three-terminal systems based on Rust provided in an embodiment of the present disclosure, such as Figure 1 As shown in the figure, the specific process of implementing the communication method across the three-terminal system based on Rust includes:
[0029] S110: Acquire a language interaction target for cross-language data communication between three-terminal systems, where the three-terminal systems include: a first operating system, a second operating system, and a third operating system.
[0030] Among them, the first operating system is used to describe the Android operating system, the second operating system is used to describe the iOS operating system, and the third operating system is used to describe the Hongmeng operating system.
[0031] The language interaction target is used to describe the interaction operation performed on the cross-language interaction data in the first operating system, the second operating system, and the third operating system, for example, a data download operation on the cross-language interaction data in the first operating system, the second operating system, and the third operating system, or a data summation operation on the cross-language interaction data in the first operating system, the second operating system, and the third operating system.
[0032] It should be noted that the cross-language interaction data referred to in this embodiment is the internal underlying programming data in the corresponding operating system (such as instance code for implementing a specific function), and not the front-end user social data of each operating system (such as user text chat content).
[0033] S120: Define a macro representation of the language interaction target to obtain macro definition data corresponding to the language interaction target.
[0034] Among them, the macro represents a data definition implemented by procedural macros in the Rust programming language. In Rust, procedural macros are a powerful metaprogramming tool that can be used to generate code at compile time, and it allows developers to dynamically manipulate, modify and generate Rust code during the compilation process, thereby implementing complex functions and reducing duplicate code. Compared with simple attribute macros or declaration macros, procedural macros have greater flexibility and functionality because they can parse and operate the entire Rust syntax tree.
[0035] Macro definition data can be used to describe a program embodiment obtained by defining data for language interaction targets through procedural macro tools.
[0036] S130: Generate a cross-language communication intermediate link corresponding to the first operating system, a cross-language communication intermediate link corresponding to the second operating system, and a cross-language communication intermediate link corresponding to the third operating system based on the macro definition data corresponding to the language interaction target.
[0037] Among them, based on the macro definition data corresponding to the language interaction target, a cross-language communication intermediate link corresponding to the first operating system, a cross-language communication intermediate link corresponding to the second operating system, and a cross-language communication intermediate link corresponding to the third operating system are generated, so that the first operating system performs cross-language data communication with the Rust programming language by calling the cross-language communication intermediate link corresponding to the first operating system, the second operating system performs cross-language data communication with the Rust programming language by calling the cross-language communication intermediate link corresponding to the second operating system, and the third operating system performs cross-language data communication with the Rust programming language by calling the cross-language communication intermediate link corresponding to the third operating system.
[0038] In some embodiments, generating a cross-language communication intermediate link corresponding to the first operating system based on macro definition data corresponding to the language interaction target includes:
[0039] Based on the macro definition data corresponding to the language interaction target, the glue layer code data corresponding to the first operating system is determined; based on the preset interaction rules of the first operating system, the corresponding interactive communication representation data is extracted from the glue layer code data corresponding to the first operating system to generate a cross-language communication intermediate link corresponding to the first operating system.
[0040] The glue layer code data corresponding to the first operating system is a glue layer code data FFI binding based on the Android operating system that is automatically generated by Rust after the language interaction target is macro-defined.
[0041] The preset interaction rules of the first operating system are used to describe the communication method and communication interface definition based on which the first operating system performs cross-language data communication.
[0042] Since the glue layer code data FFI binding includes not only the communication method and communication interface definition used by the first operating system for cross-language data communication, but also other intermediate data related to the first operating system, and the other intermediate data related to the first operating system has no substantial impact on the language communication of the first operating system, the corresponding interactive communication representation data can be extracted from the glue layer code data corresponding to the first operating system based on the preset interaction rules of the first operating system, so as to obtain accurate and effective cross-language communication intermediate links corresponding to the first operating system.
[0043] In some embodiments, generating a cross-language communication intermediate link corresponding to the second operating system and a cross-language communication intermediate link corresponding to the third operating system based on the macro definition data corresponding to the language interaction target includes:
[0044] Based on the macro definition data corresponding to the language interaction target, determine the glue layer code data corresponding to the second operating system and the glue layer code data corresponding to the second operating system; based on the preset interaction rules of the second operating system, extract the corresponding interactive communication representation data from the glue layer code data corresponding to the second operating system to generate a cross-language communication intermediate link corresponding to the second operating system; based on the preset interaction rules of the third operating system, extract the corresponding interactive communication representation data from the glue layer code data corresponding to the third operating system to generate a cross-language communication intermediate link corresponding to the third operating system.
[0045] Among them, the glue layer code data corresponding to the second operating system is a glue layer code data FFI binding based on the iOS operating system that is automatically generated by Rust after the language interaction target is macro-defined. The glue layer code data corresponding to the third operating system is a glue layer code data FFI binding based on the Hongmeng operating system that is automatically generated by Rust after the language interaction target is macro-defined.
[0046] The preset interaction rules of the second operating system are used to describe the communication method and communication interface definition based on which the second operating system performs cross-language data communication, and the preset interaction rules of the third operating system are used to describe the communication method and communication interface definition based on which the third operating system performs cross-language data communication.
[0047] Since the glue layer code data FFI binding includes not only the communication method and communication interface definition used by the second operating system / third operating system for cross-language data communication, but also other intermediate data related to the second operating system / third operating system, and the other intermediate data related to the second operating system / third operating system has no substantial impact on the language communication of the second operating system / third operating system, the corresponding interactive communication representation data can be extracted from the glue layer code data corresponding to the second operating system based on the preset interaction rules of the second operating system, and the corresponding interactive communication representation data can be extracted from the glue layer code data corresponding to the third operating system based on the preset interaction rules of the third operating system, so as to obtain accurate and effective cross-language communication intermediate links corresponding to the second operating system and the third operating system.
[0048] In some embodiments, determining the glue layer code data corresponding to the second operating system and the glue layer code data corresponding to the second operating system based on the macro definition data corresponding to the language interaction target includes:
[0049] Based on the macro definition data corresponding to the language interaction target, two communication data directories are generated, each communication data directory corresponds to an operating system identifier; the data content contained in the communication data directory whose operating system identifier matches the second operating system is determined to be the glue layer code data corresponding to the second operating system; and the data content contained in the communication data directory whose operating system identifier matches the third operating system is determined to be the glue layer code data corresponding to the third operating system.
[0050] Among them, since the Rust programming language can be used as the native language of the Android operating system and can also be used as the native language of the iOS operating system, when generating glue layer code data, it can simultaneously generate glue layer code data corresponding to the Android operating system and glue layer code data corresponding to the iOS operating system.
[0051] When generating glue layer code data corresponding to the Android operating system and glue layer code data corresponding to the iOS operating system, the corresponding data of the Android operating system and the iOS operating system are effectively distinguished by means of the target's operating system identification, so as to facilitate the effective identification of the glue layer code data corresponding to the second operating system and the glue layer code data corresponding to the third operating system from the communication data target.
[0052] In this embodiment, a language interaction target for cross-language data communication of a three-terminal system is obtained, and the three-terminal system includes: a first operating system, a second operating system, and a third operating system, the first operating system is used to describe an Android operating system, the second operating system is used to describe an iOS operating system, and the third operating system is used to describe a Hongmeng operating system, and the language interaction target is used to describe the interactive operation performed on the cross-language interactive data in the first operating system, the second operating system, and the third operating system; a macro representation definition is performed on the language interaction target to obtain macro definition data corresponding to the language interaction target; based on the macro definition data corresponding to the language interaction target, a cross-language communication intermediate link corresponding to the first operating system, a cross-language communication intermediate link corresponding to the second operating system, and a cross-language communication intermediate link corresponding to the third operating system are generated, so that the first operating system performs cross-language data communication with the Rust programming language by calling the cross-language communication intermediate link corresponding to the first operating system, the second operating system performs cross-language data communication with the Rust programming language by calling the cross-language communication intermediate link corresponding to the second operating system, and the third operating system performs cross-language data communication with the Rust programming language by calling the cross-language communication intermediate link corresponding to the third operating system. In this way, by only performing a set of programming program data definition on the Rust side, it is convenient to realize communication across the three-terminal system, and effectively improve the language programming development efficiency across the three-terminal system.
[0053] In some embodiments, the method of this embodiment further includes:
[0054] When a language interaction target is defined in a macro expression, if the cross-language interaction data of the first operating system does not meet the data constraint conditions preset by the first operating system, the data constraint results of the cross-language interaction data of the first operating system are reported to the first operating system; if the cross-language interaction data of the second operating system does not meet the data constraint conditions preset by the second operating system, the data constraint results of the cross-language interaction data of the second operating system are reported to the second operating system; if the cross-language interaction data of the third operating system does not meet the data constraint conditions preset by the third operating system, the data constraint results of the cross-language interaction data of the third operating system are reported to the third operating system.
[0055] For example, if the cross-language interaction data of the first operating system (such as A=101) does not meet the data constraint conditions preset by the first operating system (such as the maximum data value is 100), the data constraint results of the cross-language interaction data of the first operating system are reported to the first operating system (such as the data value is too high, please reduce the processing); if the cross-language interaction data of the second operating system (such as the download capacity B=200M) does not meet the data constraint conditions preset by the second operating system (such as the maximum download capacity is 150M), the data constraint results of the cross-language interaction data of the second operating system are reported to the second operating system (such as the download capacity is too high, please reduce the processing); if the cross-language interaction data of the third operating system (such as real number type C=floating point type) does not meet the data constraint conditions preset by the third operating system (such as the real number type does not include floating point type), the data constraint results of the cross-language interaction data of the third operating system are reported to the third operating system (such as the real number type is incorrect, please adjust it).
[0056] Therefore, when an abnormal situation occurs, the abnormal problem can be reported to the corresponding operating system in a timely manner through a pre-set error handling program, so that the abnormal situation can be effectively handled.
[0057] In some embodiments, the method of this embodiment further includes:
[0058] When a language interaction target is macro-expression defined, if the cross-language interaction data of the first operating system does not meet the data constraints preset by the first operating system, an interaction data adjustment request is sent to the first operating system, and the interaction data adjustment request sent to the first operating system is used to obtain the modification permission to modify the cross-language interaction data of the first operating system; if the cross-language interaction data of the second operating system does not meet the data constraints preset by the second operating system, an interaction data adjustment request is sent to the second operating system, and the interaction data adjustment request sent to the second operating system is used to obtain the modification permission to modify the cross-language interaction data of the second operating system; if the cross-language interaction data of the third operating system does not meet the data constraints preset by the third operating system, an interaction data adjustment request is sent to the third operating system, and the interaction data adjustment request sent to the third operating system is used to obtain the modification permission to modify the cross-language interaction data of the third operating system.
[0059] For example, if the cross-language interaction data of the first operating system (such as A=101) does not meet the data constraints preset by the first operating system (such as the maximum data value is 100), an interaction data adjustment request is sent to the first operating system (such as changing A to 100, whether it is allowed); if the cross-language interaction data of the second operating system (such as download capacity B=200M) does not meet the data constraints preset by the second operating system
[0060] (such as the maximum download capacity is 150M), then send an interactive data adjustment request to the second operating system (such as changing the download capacity to 120M, whether it is allowed); if the cross-language interactive data of the third operating system (such as real number type C = floating point type) does not meet the data constraints preset by the third operating system (such as the real number type does not include floating point type), then send an interactive data adjustment request to the third operating system (such as changing the real number type to floating point type, whether it is allowed).
[0061] Therefore, when an abnormal situation occurs, the pre-set error handling program can be used to promptly request data modification permission from the corresponding operating system, so that the abnormal situation can be effectively handled.
[0062] In some embodiments, the method of this embodiment further includes:
[0063] The cross-language communication intermediate link corresponding to the Rust programming language is called from the preset operating system. The cross-language communication intermediate link corresponding to the Rust programming language is used to realize cross-language data communication between the Rust programming language and the native programming language of the preset operating system; based on the cross-language communication intermediate link corresponding to the Rust programming language, cross-language data communication operations with the preset operating system are performed.
[0064] The preset operating system is at least one of the first operating system, the second operating system and the third operating system.
[0065] The cross-language communication intermediate link corresponding to the Rust programming language is generated by acquiring the language interaction target in a preset operating system, and performing macro representation definition on the acquired language interaction target to obtain the macro definition data corresponding to the language interaction target.
[0066] Thus, bidirectional communication across languages is achieved. This embodiment not only supports the native language in the operating system calling Rust, but also supports Rust calling the native language.
[0067] In addition, in this embodiment, a specific example is also provided.
[0068] The FFI definition on the Rust side is as follows.
[0069]
[0070]
[0071] As shown above, an asynchronous add method is defined and an FFI macro with only one line of code is added
[0072] (such as #[ffi]). After this definition, the FFI binding code (i.e. glue layer code) for Android / iOS / Hongmeng NEXT will be automatically generated.
[0073] For the Hongmeng platform, an inder.d.ts declaration file (i.e., the intermediate link for cross-language communication) will be generated, which contains the definition of the interactive communication method and data interface on the ArkTS (the native language of the Hongmeng operating system) side, making it easier for ArkTS to indirectly call the code defined on the Rust side.
[0074] For the Android platform, a xxx.kt declaration file (i.e., the intermediate link for cross-language communication) will be generated, where xxx is the name of the rust lib, which contains the definition of the interactive communication method and data interface on the Kotlin (the native language of the Android operating system) side, making it easier for Kotlin to indirectly call the code defined on the Rust side.
[0075] For the iOS platform, a xxx.swift, module.modulemap, and xxxFFI.h declaration file (i.e., the intermediate link for cross-language communication) will be generated, where xxx is the name of the rust lib. xxx.swift contains the definition of the interactive communication method and data interface on the Swift (native language of the iOS operating system) side, making it easier for Swift to indirectly call the code defined on the Rust side.
[0076] Because the characteristics of uniffi-rs (Rust tool library) are: support traits (interfaces) and do not support singleton static
[0077] Method; The characteristics of ohos-rs (Rust tool library) are: no support for traits (interfaces) and support for singleton static methods.
[0078] Trait capability means that the calling method can implement the custom trait (interface) in Rust, which makes it easier to implement Rust's ability to call other languages. ohos-rs does not have this capability and can only use Function and ThreadsafeFunction (asynchronous) to implement it. ohos-rs can define a static method of struct, which can play a role in module management, such as HttpFFI.request(), but uniffi-rs does not support it and can only mount a specific instance, so it can only be similar to HttpFFI().request(), that is, you need to create a new object first.
[0079] Considering the difference between uniffi-rs and ohos-rs, if they need to be integrated into a ffi macro, they need to smooth out the difference in the procedural macro.
[0080] Define conditions: Developers define the logic of conditional compilation through cfg or features in the code or Cargo.toml file. For example, we define two features through features: uniffi and napi. Cargo parses configuration: When the developer runs cargobuild, Cargo parses the conditions in Cargo.toml and the code according to the configuration (including platform, compilation target, enabled features, etc.) and generates a final set of compilation instructions. The compiler makes conditional judgments: The Rust compiler checks whether each code snippet or dependency meets the defined conditions during the compilation process according to the conditions parsed by Cargo; only the code and dependencies that meet the conditions will be compiled into the final executable file or library; specify uniffifeature to compile only uniffi-rs related, and specify napi feature to compile only ohos-rs related. After setting the conditional compilation in this way, the corresponding ffi (uniffi or napi) can be compiled according to the feature specified during compilation, so that the two do not interfere with each other.
[0081] Implementation of procedural macros. To meet most scenarios, FFI macros support but will not be limited to the following types in the future: 1) Function: synchronous and asynchronous functions; 2) Impl block: defined on the impl block, FFI of all methods in the impl block can be generated at the same time; 3) Struct: Rust structure; 4) Enum: enumeration, supporting int enumeration and string enumeration; 5) Trait: interface with other languages. Since ohos-rs itself does not support traits, this difference needs to be handled in FFI procedural macros. That is, for ohos-rs scenarios, it needs to be converted into Function or ThreadsafeFunction.
[0082] Function means function. Function scenarios include the following situations: 1) Ordinary function: no callback in the parameter. 2) Parameter contains callback (Box<dyn XXXTrait> ): Since napi does not natively support Trait, the napi feature scenario needs to change the definition and call of trait to ThreadsafeFunction. 3) Function defined by #[ffi(replace)]: Internal method, not exposed to native language. It means that only the replacement callback(trait) logic needs to be executed (uniffi's callback(trait) is replaced with napi's ThreadsafeFunction), and there is no need to add uniffi and napi macros.
[0083] Impl block is the implementation block in Rust. The following situations need to be handled in the Impl block scenario: 1) All methods defined under the Impl block; 2) uniffi-rs does not support static methods, so if the method in the Impl block does not define &self, it needs to be automatically generated; 3) Same as situation 2, since uniffi-rs does not support static methods, a constructor needs to be automatically generated for the Impl block; 4) Handling the scenario of Rust calling native: For example, an Impl AppDelegateManager is defined in a way supported by uniffi (Rust uses this Manager to call the platform native language), and then the ffi macro needs to convert it into the napi version of the AppDelegateManager.
[0084] Struct is a structure in Rust. Struct scenarios need to handle the following situations: 1) Pure data types (record), such as the data class that will generate Kotlin. 2) Object type struct (object), Rust will have the Impl struct. 3) Handle the scenario of Rust calling native: that is, the struct AppDelegateManager corresponding to the Impl AppDelegateManager mentioned above.
[0085] Enum is the enumeration in Rust. The Enum scenario needs to handle the following situations: 1) int_enum: enumeration of int type; 2) string_enum: enumeration of string type; 3) error: error type. The Error type returned by the Result of the method in Rust FFI is also defined using enum.
[0086] Trait is an important means to achieve polymorphism and code reuse in Rust, similar to interface in other languages (such as Java). The core scenario of Trait needs to process the napi feature, which requires converting traits into ThreadsafeFunction, because ohos-rs does not natively support traits.
[0087] In summary, this embodiment, based on Rust procedural macros and Cargo conditional compilation, proposes a Rust FFI method across three mobile terminals (Android / iOS / Hongmeng) that combines uniffi-rs and ohos-rs. Only one line of Rust macro definition code is required to implement Rust FFI, which greatly improves development efficiency; it not only supports native languages (Kotlin / Swift / ArkTS) to call Rust, but also supports Rust to call native languages; traditional FFI solutions, such as JNI in Android, do not directly support the call of async methods, and supporting async methods makes scenarios with async requirements unrestricted; because it is built on the basis of the industry's excellent uniffi-rs and ohos-rs solutions, it also naturally supports complex data structures, which avoids the cumbersome process of explicit serialization and deserialization, making cross-terminal development simpler; because uniffi-rs and ohos-rs support error handling, they also naturally support error handling, which helps exception handling and makes the software more robust.
[0088] Figure 2A structural diagram of a communication device for implementing a cross-three-terminal system based on Rust provided in this embodiment. The communication device for implementing a cross-three-terminal system based on Rust may include: an acquisition module 210, a definition module 220 and a generation module 230.
[0089] The acquisition module 210 is used to obtain the language interaction target of the three-terminal system for cross-language data communication. The three-terminal system includes: a first operating system, a second operating system and a third operating system. The first operating system is used to describe the Android operating system, the second operating system is used to describe the iOS operating system, and the third operating system is used to describe the Hongmeng operating system. The language interaction target is used to describe the interaction operations performed on the cross-language interaction data in the first operating system, the second operating system and the third operating system.
[0090] The definition module 220 is used to perform macro definition on the language interaction target and obtain macro definition data corresponding to the language interaction target.
[0091] A generation module 230 is used to generate a cross-language communication intermediate link corresponding to the first operating system, a cross-language communication intermediate link corresponding to the second operating system, and a cross-language communication intermediate link corresponding to the third operating system based on the macro definition data corresponding to the language interaction target, so that the first operating system performs cross-language data communication with the Rust programming language by calling the cross-language communication intermediate link corresponding to the first operating system, the second operating system performs cross-language data communication with the Rust programming language by calling the cross-language communication intermediate link corresponding to the second operating system, and the third operating system performs cross-language data communication with the Rust programming language by calling the cross-language communication intermediate link corresponding to the third operating system.
[0092] In this embodiment, optionally, the generating module 230 is specifically configured to:
[0093] Based on the macro definition data corresponding to the language interaction target, the glue layer code data corresponding to the first operating system is determined; based on the preset interaction rules of the first operating system, the corresponding interactive communication representation data is extracted from the glue layer code data corresponding to the first operating system to generate a cross-language communication intermediate link corresponding to the first operating system; wherein the preset interaction rules of the first operating system are used to describe the communication method and communication interface definition based on which the first operating system performs cross-language data communication.
[0094] In this embodiment, optionally, the generating module 230 is specifically configured to:
[0095] Based on the macro definition data corresponding to the language interaction target, determine the glue layer code data corresponding to the second operating system and the glue layer code data corresponding to the second operating system; based on the preset interaction rules of the second operating system, extract the corresponding interactive communication representation data from the glue layer code data corresponding to the second operating system to generate a cross-language communication intermediate link corresponding to the second operating system; based on the preset interaction rules of the third operating system, extract the corresponding interactive communication representation data from the glue layer code data corresponding to the third operating system to generate a cross-language communication intermediate link corresponding to the third operating system; wherein the preset interaction rules of the second operating system are used to describe the communication method and communication interface definition based on which the second operating system performs cross-language data communication, and the preset interaction rules of the third operating system are used to describe the communication method and communication interface definition based on which the third operating system performs cross-language data communication.
[0096] In this embodiment, optionally, the generating module 230 is specifically configured to:
[0097] Based on the macro definition data corresponding to the language interaction target, two communication data directories are generated, each communication data directory corresponds to an operating system identifier; the data content contained in the communication data directory whose operating system identifier matches the second operating system is determined to be the glue layer code data corresponding to the second operating system; and the data content contained in the communication data directory whose operating system identifier matches the third operating system is determined to be the glue layer code data corresponding to the third operating system.
[0098] In this embodiment, optionally, it also includes: a reporting module.
[0099] A reporting module is used to, when macro-expression definition is performed on a language interaction target, report data constraint results of the cross-language interaction data of the first operating system to the first operating system if the cross-language interaction data of the first operating system does not satisfy data constraint conditions preset by the first operating system; report data constraint results of the cross-language interaction data of the second operating system to the second operating system if the cross-language interaction data of the second operating system does not satisfy data constraint conditions preset by the second operating system; and report data constraint results of the cross-language interaction data of the third operating system to the third operating system if the cross-language interaction data of the third operating system does not satisfy data constraint conditions preset by the third operating system.
[0100] In this embodiment, optionally, it also includes: a sending module.
[0101] A sending module is used to, when macro-expression defining a language interaction target, send an interaction data adjustment request to the first operating system if the cross-language interaction data of the first operating system does not meet the data constraint conditions preset by the first operating system, and the interaction data adjustment request sent to the first operating system is used to obtain the modification permission to modify the cross-language interaction data of the first operating system; if the cross-language interaction data of the second operating system does not meet the data constraint conditions preset by the second operating system, send the interaction data adjustment request to the second operating system, and the interaction data adjustment request sent to the second operating system is used to obtain the modification permission to modify the cross-language interaction data of the second operating system; if the cross-language interaction data of the third operating system does not meet the data constraint conditions preset by the third operating system, send the interaction data adjustment request to the third operating system, and the interaction data adjustment request sent to the third operating system is used to obtain the modification permission to modify the cross-language interaction data of the third operating system.
[0102] In this embodiment, optionally, it also includes: a calling module and an execution module.
[0103] The calling module is used to call the cross-language communication intermediate link corresponding to the Rust programming language from the preset operating system. The cross-language communication intermediate link corresponding to the Rust programming language is used to realize cross-language data communication between the Rust programming language and the native programming language of the preset operating system.
[0104] An execution module is used to perform a cross-language data communication operation with a preset operating system based on a cross-language communication intermediate link corresponding to the Rust programming language; wherein the preset operating system is at least one of the first operating system, the second operating system, and the third operating system.
[0105] The communication device for implementing a cross-three-terminal system based on Rust provided in the present disclosure can execute the above method embodiments. Its specific implementation principles and technical effects can be found in the above method embodiments, and the present disclosure will not repeat them here.
[0106] The present application also provides a computer device. Figure 3 , Figure 3 This is a basic structural block diagram of the computer device in this embodiment.
[0107] The computer device includes a memory 310 and a processor 320 that are connected to each other through a system bus. It should be noted that the figure only shows a computer device with a memory 310 and a processor 320, but it should be understood that it is not required to implement all the components shown, and more or fewer components can be implemented instead. Among them, those skilled in the art can understand that the computer device here is a device that can automatically perform numerical calculations and / or information processing according to pre-set or stored instructions, and its hardware includes but is not limited to microprocessors, application specific integrated circuits (Application Specific Integrated Circuit, ASIC), programmable gate arrays (Field-Programmable Gate Array, FPGA), digital processors (Digital Signal Processor, DSP), embedded devices, etc.
[0108] Computer devices can be computing devices such as desktop computers, notebooks, PDAs, and cloud servers. Computer devices can interact with users through keyboards, mice, remote controls, touch pads, or voice control devices.
[0109] The memory 310 includes at least one type of readable storage medium, and the readable storage medium includes a non-volatile memory or a volatile memory, such as a flash memory, a hard disk, a multimedia card, a card-type memory (e.g., SD or DX memory, etc.), a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM), an electrically erasable programmable read-only memory (EEPROM), a programmable read-only memory (PROM), a magnetic memory, a magnetic disk, an optical disk, etc., and the RAM may include a static RAM or a dynamic RAM. In some embodiments, the memory 310 may be an internal storage unit of a computer device, such as a hard disk or a memory of the computer device. In other embodiments, the memory 310 may also be an external storage device of a computer device, such as a plug-in hard disk, a smart memory card (SMC), a secure digital (SD) card, or a flash card (Flash Card), etc., equipped on the computer device. Of course, the memory 310 may also include both an internal storage unit of the computer device and an external storage device thereof. In this embodiment, the memory 310 is generally used to store an operating system and various application software installed on the computer device, such as the program code of the above method, etc. In addition, the memory 310 may also be used to temporarily store various data that have been output or are to be output.
[0110] The processor 320 is generally used to perform the overall operation of the computer device. In this embodiment, the memory 310 is used to store program codes or instructions, the program code includes computer operation instructions, and the processor 320 is used to execute the program codes or instructions stored in the memory 310 or process data, such as running the program code of the above method.
[0111] In this article, the bus can be an Industry Standard Architecture (ISA) bus, a Peripheral Component Interconnect (PCI) bus, or an Extended Industry Standard Architecture (EISA) bus, etc. The bus system can be divided into an address bus, a data bus, a control bus, etc. For ease of representation, only one thick line is used in the figure, but it does not mean that there is only one bus or one type of bus.
[0112] Another embodiment of the present application also provides a computer-readable medium, which may be a computer-readable signal medium or a computer-readable medium. A processor in a computer reads a computer-readable program code stored in the computer-readable medium, so that the processor can execute the functional actions specified in each step or a combination of steps in the above method; and generate a device for implementing the functional actions specified in each block or a combination of blocks in the block diagram.
[0113] Computer-readable media include but are not limited to electronic, magnetic, optical, electromagnetic, infrared memory or semiconductor systems, devices or apparatuses, or any appropriate combination of the foregoing, the memory is used to store program codes or instructions, the program codes include computer operating instructions, and the processor is used to execute the program codes or instructions of the above methods stored in the memory.
[0114] For the definitions of memory and processor, please refer to the description of the aforementioned computer device embodiment and will not be repeated here.
[0115] In the several embodiments provided in the present application, it should be understood that the disclosed systems, devices and methods can be implemented in other ways. For example, the device embodiments described above are only schematic, for example, the division of modules or units is only a logical function division, and there may be other division methods in actual implementation, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of devices or units, which can be electrical, mechanical or other forms.
[0116] Each functional unit or module in each embodiment of the present application may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit. The above-mentioned integrated unit may be implemented in the form of hardware or in the form of software functional units.
[0117] If the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present application is essentially or the part that contributes to the prior art or all or part of the technical solution can be embodied in the form of a software product, and the computer software product is stored in a storage medium, including a number of instructions to enable a computer device (which can be a personal computer, server, or network device, etc.) or a processor (processor) to perform all or part of the steps of each embodiment method of the present application. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (Read-Only Memory, ROM), random access memory (Random Access Memory, RAM), disk or optical disk and other media that can store program code.
[0118] In the claims, any reference symbols placed between brackets shall not be construed as limiting the claims. The word "comprising" described in the present application does not exclude the presence of elements or steps not listed in the claims. The word "a" or "an" preceding an element does not exclude the presence of a plurality of such elements. The present application can be implemented with the aid of hardware comprising several different elements and with the aid of a suitably programmed computer. In a unit claim that lists a number of devices, several units of these devices may be embodied by the same hardware item. The use of first, second, and third, etc. does not indicate any order, and these words may be interpreted as names. The steps in the above embodiments, unless otherwise specified, should not be understood as limitations on the order of execution.
[0119] The above embodiments are only used to illustrate the technical solutions of the present application, rather than to limit them. Although the present application has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present application.
Claims
1. A method for implementing communication across three-terminal systems based on Rust, characterized in that: include: Obtain a language interaction target for cross-language data communication between three-terminal systems, where the three-terminal systems include: a first operating system, a second operating system, and a third operating system, where the first operating system is used to describe an Android operating system, the second operating system is used to describe an iOS operating system, and the third operating system is used to describe a Hongmeng operating system, and the language interaction target is used to describe an interaction operation performed on cross-language interaction data in the first operating system, the second operating system, and the third operating system; Performing macro expression definition on the language interaction target to obtain macro definition data corresponding to the language interaction target; Based on the macro definition data corresponding to the language interaction target, a cross-language communication intermediate link corresponding to the first operating system, a cross-language communication intermediate link corresponding to the second operating system, and a cross-language communication intermediate link corresponding to the third operating system are generated, so that the first operating system performs cross-language data communication with the Rust programming language by calling the cross-language communication intermediate link corresponding to the first operating system, the second operating system performs cross-language data communication with the Rust programming language by calling the cross-language communication intermediate link corresponding to the second operating system, and the third operating system performs cross-language data communication with the Rust programming language by calling the cross-language communication intermediate link corresponding to the third operating system.
2. The method according to claim 1, characterized in that The step of generating a cross-language communication intermediate link corresponding to the first operating system based on the macro definition data corresponding to the language interaction target includes: Determining glue layer code data corresponding to the first operating system based on the macro definition data corresponding to the language interaction target; Based on the preset interaction rules of the first operating system, corresponding interactive communication representation data is extracted from the glue layer code data corresponding to the first operating system to generate a cross-language communication intermediate link corresponding to the first operating system; The preset interaction rules of the first operating system are used to describe the communication method and communication interface definition based on which the first operating system performs cross-language data communication.
3. The method according to claim 1, characterized in that The step of generating, based on the macro definition data corresponding to the language interaction target, a cross-language communication intermediate link corresponding to the second operating system and a cross-language communication intermediate link corresponding to the third operating system includes: Determining glue layer code data corresponding to the second operating system and glue layer code data corresponding to the second operating system based on the macro definition data corresponding to the language interaction target; Based on the preset interaction rules of the second operating system, corresponding interactive communication representation data is extracted from the glue layer code data corresponding to the second operating system to generate a cross-language communication intermediate link corresponding to the second operating system; Based on the preset interaction rules of the third operating system, corresponding interactive communication representation data is extracted from the glue layer code data corresponding to the third operating system to generate a cross-language communication intermediate link corresponding to the third operating system; Among them, the preset interaction rules of the second operating system are used to describe the communication method and communication interface definition based on which the second operating system performs cross-language data communication, and the preset interaction rules of the third operating system are used to describe the communication method and communication interface definition based on which the third operating system performs cross-language data communication.
4. The method according to claim 3, characterized in that The determining, based on the macro definition data corresponding to the language interaction target, the glue layer code data corresponding to the second operating system and the glue layer code data corresponding to the second operating system includes: Based on the macro definition data corresponding to the language interaction target, two communication data directories are generated, each communication data directory corresponding to an operating system identifier; Determine that data content included in the communication data directory whose operating system identifier matches the second operating system is glue layer code data corresponding to the second operating system; It is determined that the data content contained in the communication data directory whose operating system identifier matches the third operating system is the glue layer code data corresponding to the third operating system.
5. The method according to claim 1, characterized in that Also includes: When macro-expression definition is performed on the language interaction target, if the cross-language interaction data of the first operating system does not satisfy the data constraint condition preset by the first operating system, reporting the data constraint result of the cross-language interaction data of the first operating system to the first operating system; If the cross-language interaction data of the second operating system does not satisfy the data constraint condition preset by the second operating system, reporting the data constraint result of the cross-language interaction data of the second operating system to the second operating system; If the cross-language interaction data of the third operating system does not satisfy the data constraint condition preset by the third operating system, a data constraint result of the cross-language interaction data of the third operating system is reported to the third operating system.
6. The method according to claim 1, characterized in that Also includes: When macro-expression definition is performed on the language interaction target, if the cross-language interaction data of the first operating system does not satisfy the data constraint condition preset by the first operating system, an interaction data adjustment request is sent to the first operating system, and the interaction data adjustment request sent to the first operating system is used to obtain modification permission for modifying the cross-language interaction data of the first operating system; If the cross-language interaction data of the second operating system does not satisfy the data constraint condition preset by the second operating system, sending an interaction data adjustment request to the second operating system, where the interaction data adjustment request sent to the second operating system is used to obtain the modification permission to modify the cross-language interaction data of the second operating system; If the cross-language interaction data of the third operating system does not meet the data constraint conditions preset by the third operating system, an interaction data adjustment request is sent to the third operating system, and the interaction data adjustment request sent to the third operating system is used to obtain the modification permission of modifying the cross-language interaction data of the third operating system.
7. The method according to claim 1, characterized in that Also includes: Calling a cross-language communication intermediate link corresponding to the Rust programming language from a preset operating system, wherein the cross-language communication intermediate link corresponding to the Rust programming language is used to implement cross-language data communication between the Rust programming language and the native programming language of the preset operating system; Based on the cross-language communication intermediate link corresponding to the Rust programming language, perform a cross-language data communication operation with the preset operating system; The preset operating system is at least one of a first operating system, a second operating system and a third operating system.
8. A communication device for implementing a three-terminal system based on Rust, characterized in that: include: an acquisition module, used to acquire a language interaction target for cross-language data communication between three-terminal systems, the three-terminal systems comprising: a first operating system, a second operating system, and a third operating system, the first operating system being used to describe an Android operating system, the second operating system being used to describe an iOS operating system, the third operating system being used to describe a Hongmeng operating system, and the language interaction target being used to describe an interaction operation performed on cross-language interaction data in the first operating system, the second operating system, and the third operating system; A definition module, used to perform macro expression definition on the language interaction target and obtain macro definition data corresponding to the language interaction target; A generation module is used to generate a cross-language communication intermediate link corresponding to the first operating system, a cross-language communication intermediate link corresponding to the second operating system, and a cross-language communication intermediate link corresponding to the third operating system based on the macro definition data corresponding to the language interaction target, so that the first operating system performs cross-language data communication with the Rust programming language by calling the cross-language communication intermediate link corresponding to the first operating system, the second operating system performs cross-language data communication with the Rust programming language by calling the cross-language communication intermediate link corresponding to the second operating system, and the third operating system performs cross-language data communication with the Rust programming language by calling the cross-language communication intermediate link corresponding to the third operating system.
9. A computer device, characterized in that: The invention comprises a memory and a processor, wherein a computer program is stored in the memory, and when the processor executes the computer program, a communication method across a three-terminal system based on Rust as described in any one of claims 1 to 7 is implemented.
10. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by the processor, the communication method across the three-terminal system based on Rust as described in any one of claims 1 to 7 is implemented.
Citation Information
Cited By
Fault information reporting method, electronic equipment, storage medium and program product
CN120547044A