{"id":232,"date":"2020-09-01T12:40:20","date_gmt":"2020-09-01T12:40:20","guid":{"rendered":"https:\/\/blog.mozilla.org\/data\/?p=232"},"modified":"2020-09-25T15:04:41","modified_gmt":"2020-09-25T15:04:41","slug":"twig-leveraging-rust","status":"publish","type":"post","link":"https:\/\/blog.mozilla.org\/data\/2020\/09\/01\/twig-leveraging-rust\/","title":{"rendered":"This Week in Glean: Leveraging Rust to build cross-platform mobile libraries"},"content":{"rendered":"<p>(\u201cThis Week in Glean\u201d is a series of blog posts that the Glean Team at Mozilla is using to try to communicate better about our work. They could be release notes, documentation, hopes, dreams, or whatever: so long as it is inspired by Glean.)<\/p>\n<p>All \u201cThis Week in Glean\u201d blog posts are listed in the <a href=\"https:\/\/mozilla.github.io\/glean\/book\/appendix\/twig.html\">TWiG index<\/a>.<\/p>\n<hr \/>\n<p>A couple of weeks ago I gave a talk titled <a href=\"https:\/\/www.youtube.com\/watch?v=j5rczOF7pzg\">\u201cLeveraging Rust to build cross-platform mobile libraries\u201d<\/a>. You can find my <a href=\"https:\/\/fnordig.de\/talks\/2020\/rustydays\/slides.pdf\">slides as a PDF<\/a>. It was part of the <a href=\"https:\/\/rusty-days.org\/\">Rusty Days<\/a> Webference, an online conference that was initially planned to happen in Poland, but had to move online. Definitely check out <a href=\"https:\/\/www.youtube.com\/watch?v=QaCvUKrxNLI&amp;list=PLf3u8NhoEikhTC5radGrmmqdkOK-xMDoZ\">the other talks<\/a>.<\/p>\n<p>One thing I wanted to achieve with that talk is putting that knowledge out there. While multiple teams at Mozilla are already building cross-platform libraries, with a focus on mobile integration, the available material and documentation is lacking. I\u2019d like to see better guides online, and I probably have to start with what we have done. But that should also be encouragement for those out there doing similar things to blog, tweet &amp; speak about it.<\/p>\n<h2>Who else is using Rust to build cross-platform libraries, targetting mobile?<\/h2>\n<p>I\u2019d like to hear about it. Find me on Twitter (<a href=\"https:\/\/twitter.com\/badboy_\">@badboy_<\/a>) or drop me <a href=\"mailto:janerik@fnordig.de\">an email<\/a>.<\/p>\n<h2 id=\"the-glean-sdk\">The Glean SDK<\/h2>\n<p>I won\u2019t reiterate the full talk (<a href=\"https:\/\/www.youtube.com\/watch?v=j5rczOF7pzg\">go watch it<\/a>, really!), so this is just a brief overview of the Glean SDK itself.<\/p>\n<p>The Glean SDK is our approach to build a modern Telemetry library, used in Mozilla\u2019s mobile products and soon in Firefox on Desktop as well.<\/p>\n<p>The SDK consists of multiple components, spanning multiple programming languages for different implementations. All of the Glean SDK lives in the GitHub repository at <a href=\"https:\/\/github.com\/mozilla\/glean\">mozilla\/glean<\/a>. This is a rough diagram of the Glean SDK tech stack:<\/p>\n<p><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-233\" src=\"http:\/\/blog.mozilla.org\/data\/files\/2020\/09\/glean-stack.png\" alt=\"Glean SDK Stack\" width=\"739\" height=\"705\" srcset=\"https:\/\/blog.mozilla.org\/data\/files\/2020\/09\/glean-stack.png 739w, https:\/\/blog.mozilla.org\/data\/files\/2020\/09\/glean-stack-300x286.png 300w, https:\/\/blog.mozilla.org\/data\/files\/2020\/09\/glean-stack-600x572.png 600w\" sizes=\"(max-width: 739px) 100vw, 739px\" \/><\/p>\n<p>On the very bottom we have <code>glean-core<\/code>, a pure Rust library that is the heart of the SDK. It\u2019s responsible for controlling the database, storing data and handling additional logic (e.g.\u00a0assembling pings, clearing data, ..). As it is pure Rust we can rely on all Rust tooling for its development. We can write tests that <code>cargo test<\/code> picks up. We can generate the full API documentation thanks to <code>rustdoc<\/code> and we rely on <code>clippy<\/code> to tell us when our code is suboptimal. Working on <code>glean-core<\/code> should be possible for everyone that knows some Rust.<\/p>\n<p>On top of that sits <code>glean-ffi<\/code>. This is the FFI layer connecting <code>glean-core<\/code> with everything else. While glean-core is pure Rust, it doesn\u2019t actually provide the <em>nice API<\/em> we intend for users of Glean. That one is later implemented on top of it all. glean-ffi doesn\u2019t contain much logic. It\u2019s a translation between the proper Rust API of glean-core and C-compatible functions exposed into the dynamic library. In it we rely on the excellent <a href=\"https:\/\/docs.rs\/ffi-support\/\"><code>ffi-support<\/code> crate<\/a>. ffi-support knows how to translate between Rust and C types, offers a nice (and safer) abstraction for C strings. glean-ffi holds <em>some<\/em> state: the instantiated global Glean object and metric objects. We don\u2019t need to pass pointers back and forth. Instead we use opaque handles that index into a map held inside the FFI crate.<\/p>\n<p>The top layer of the Glean SDK are the different <a href=\"https:\/\/mozilla.github.io\/glean\/book\/dev\/core\/internal\/implementations.html\">language implementations<\/a>. Language implementations expose a nice ergonomic API to initialize Glean and record metrics in the respective language. Additionally each implementation handles some special cases for the platform they are running on, like gathering application and platform data or hooking into system events. The nice API calls into the Glean SDK using the exposed FFI functions of <code>glean-ffi<\/code>. Unfortunately at the moment different language implementations carry different amounts of actual logic in them. Sometimes metric implementations require this (e.g.\u00a0we rely on the clock source of Kotlin for timing metrics), in other parts we just didn\u2019t move the logic out of the implementations yet. We\u2019re actively working on <a href=\"https:\/\/bugzilla.mozilla.org\/show_bug.cgi?id=1651382\">moving logic into the Rust part where we can<\/a> and might eventually use some code generation to unify the other parts. <a href=\"https:\/\/github.com\/mozilla\/uniffi-rs\">uniffi<\/a> is a current experiment for a multi-language bindings generator for Rust we might end up using.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>(\u201cThis Week in Glean\u201d is a series of blog posts that the Glean Team at Mozilla is using to try to communicate better about our work. They could be release &hellip; <a class=\"go\" href=\"https:\/\/blog.mozilla.org\/data\/2020\/09\/01\/twig-leveraging-rust\/\">Read more<\/a><\/p>\n","protected":false},"author":1756,"featured_media":197,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[448297],"tags":[],"coauthors":[],"_links":{"self":[{"href":"https:\/\/blog.mozilla.org\/data\/wp-json\/wp\/v2\/posts\/232"}],"collection":[{"href":"https:\/\/blog.mozilla.org\/data\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.mozilla.org\/data\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.mozilla.org\/data\/wp-json\/wp\/v2\/users\/1756"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.mozilla.org\/data\/wp-json\/wp\/v2\/comments?post=232"}],"version-history":[{"count":0,"href":"https:\/\/blog.mozilla.org\/data\/wp-json\/wp\/v2\/posts\/232\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.mozilla.org\/data\/wp-json\/wp\/v2\/media\/197"}],"wp:attachment":[{"href":"https:\/\/blog.mozilla.org\/data\/wp-json\/wp\/v2\/media?parent=232"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.mozilla.org\/data\/wp-json\/wp\/v2\/categories?post=232"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.mozilla.org\/data\/wp-json\/wp\/v2\/tags?post=232"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/blog.mozilla.org\/data\/wp-json\/wp\/v2\/coauthors?post=232"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}