With Rust Procedural macros, developer are able to parse the TokenTree of Rust code, such as the contents of a struct. But if this struct is from another crate there does not seem to be a direct solution to implement a macro to parse it.
There is an incredibly verbose thread on the Rust Forum here related to the orphan-impl problem in Rust which does not seem to have an official conclusion. There are libraries out in the wild which do their best given the tools provided by the Rust ecosystem such as serde's remote-derive with relevant code. This though necessitates the user to define structs from external crates in their own, which makes coupling with other crates very fragile.
Example usage for serde's remote linting can be found here:
serde remote linting
Wondering if there are any different methods to solve this use case in the rust ecosystem or if there are plans in the rust core team for the future.
Related
I am exploring the option to dynamically load libraries at runtime in Rust. I am still a Rust newbie, so I found several articles detailing why this is not the recommended way.
Plugins in Rust
Dynamically Loading Plugin
Rust has no stable ABI
I have not found, or maybe have not fully understood why this is so? Rust is a systems programming language to my understanding, and such, isn't dynamic loading and plugin writing important to this goal?
Is there a meaningful design decision behind Rust's static compiling be default?
As a side note: I have found a crate that supports this pretty well, but before using it I would like to understand why the Rust creators would rather not build this into the language.
I am so surprised when I look into the substrate-relevant project code. It's so hard to understand, runtime macros everywhere.
Now, It's easier for you to develop your own blockchain base on the Substrate framework. The most difficult section might be how to make rustc accept your code.
It is not necessary to use the macros to develop on Substrate. As you may know, the macros ultimately expand to be real rust code, so if you understand the inner working of Substrate at that level, then of course you can write that code yourself, but this will certainly not be as easy as using the macros.
I believe the macros expand to about 3x the lines of code as you write, and contains logic that we try to keep opaque from the average runtime developer.
It is a fair criticism that the runtime macros can be hard to debug or work with, but we are looking to solve this issue by using Rust attribute macros and staying closer to traditional Rust syntax.
See the tracking issue here: https://github.com/paritytech/substrate/issues/5678
I like that Rust comes with a lot of macros which moves computation to compile-time instead of repeatably to run-time.
print! and all its variants using format_args! See source code are great examples.
Unfortunately, in the source code you see the comment /* compiler built-in */ instead of a implementation direct in the source file.
Does Rust have the capability to let the user write such complex logic as a macro as well? If so, how can I do so?
Complex macros are usually implemented as procedural macros, which you can learn more about in The Rust Programming Language or in The Rust Reference books.
You can also achieve very complex things with the so called declarative macros, look at the excellent The Little Book of Rust Macros.
There are several talks about these on YouTube, but you may find the following in particular very interesting, which was given at the RustConf 2018, My Little Procedural Macro by Chris Wong:
I was wondering how are Rust crates are implemented.
For example, there is a crate named num_cpus. This crate has this basic method num_cpus::get() which tells number of CPUs in your computer.
My questions:
How is the method num_cpus::get() implemented (is it done using another language?)
Can the same result be achieved with plain Rust code without using any crates?
... in Rust. There's no reason to believe it'd be anything else.
You can also check this by just looking at the source code, easily done by:
Search crates.io for "num_cpus"..
Select the num_cpus crate.
Select "Repository" for the source code.
Open the only .rs source file in the repository, src/lib.rs.
is there a way to inpect the traits an object provides at runtime. Similar to pythons dir()? I wish to inspect the contents of a core::str::StrSplits<'_> and would like to be able to view the traits it implements.
There is no facility for doing this at runtime; at compile time, however, there is, and this is what rustdoc does.
With that you can see things like the core::str::StrSplits documentation which covers the information you requested.