{"id":27767,"date":"2020-01-03T14:42:58","date_gmt":"2020-01-03T19:42:58","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=27767"},"modified":"2020-01-03T14:42:58","modified_gmt":"2020-01-03T19:42:58","slug":"rewriting-m4vgalib-in-rust","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2020\/01\/03\/rewriting-m4vgalib-in-rust\/","title":{"rendered":"Rewriting m4vgalib in Rust"},"content":{"rendered":"<p><a href=\"http:\/\/cliffle.com\/blog\/m4vga-in-rust\/\">Cliff Biffle<\/a> (via <a href=\"https:\/\/twitter.com\/bcantrill\/status\/1212145618891591680\">Bryan Cantrill<\/a>, <a href=\"https:\/\/news.ycombinator.com\/item?id=21927248\">Hacker News<\/a>):<\/p>\n<blockquote cite=\"http:\/\/cliffle.com\/blog\/m4vga-in-rust\/\"><p>The Rust implementation is simpler, shorter (in lines of code), faster, and smaller (in bytes of Flash) than my heavily-optimized C++ version &mdash; and because it&rsquo;s almost entirely safe code, several types of bugs that I fought regularly, such as race conditions and dangling pointers, are now caught <em>by the compiler<\/em>.<\/p><p>[&#8230;]<\/p><p>Rust&rsquo;s ownership rules produce a sort of bizarro-world of API design.<\/p><ul><li><p>Some (uncommon, but reasonable) API designs won&rsquo;t make it past the borrow\nchecker. (In nearly every case, these are APIs that were <em>easy to use\nincorrectly<\/em> in other languages.<\/p><\/li><li><p>Some API patterns that are grossly unsafe or unwise in other languages are\nroutine in Rust because of lifetime checking.<\/p><\/li><\/ul><p>[&#8230;]<\/p><p>Because of Rust&rsquo;s ownership and thread-safety rules, you can <em>only<\/em> share data\nbetween threads and ISRs if it&rsquo;s packaged in one of these thread-safe\ncontainers. In Rust terms, the containers convert a type that is <code>Send<\/code>, or safe\nto move <em>between<\/em> threads but not safe to use <em>concurrently<\/em>, into a type that\nis <code>Sync<\/code>, or safe for concurrent use. If you add some new data and attempt to\nshare it without protecting it, your code will simply not compile. This means <em>I\ndon&rsquo;t have to think about data races<\/em> except when I&rsquo;m hacking the internals of a\nlocking primitive, so I can think about other things instead.<\/p><\/blockquote>","protected":false},"excerpt":{"rendered":"<p>Cliff Biffle (via Bryan Cantrill, Hacker News): The Rust implementation is simpler, shorter (in lines of code), faster, and smaller (in bytes of Flash) than my heavily-optimized C++ version &mdash; and because it&rsquo;s almost entirely safe code, several types of bugs that I fought regularly, such as race conditions and dangling pointers, are now caught [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"2020-01-03T19:43:01Z","apple_news_api_id":"7bbad64a-9f08-4e76-a855-06cbfa2064e8","apple_news_api_modified_at":"2020-01-03T19:43:01Z","apple_news_api_revision":"AAAAAAAAAAD\/\/\/\/\/\/\/\/\/\/w==","apple_news_api_share_url":"https:\/\/apple.news\/Ae7rWSp8ITnaoVQbL-iBk6A","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[326,800,571,71,408],"class_list":["post-27767","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-c-plus-plus","tag-concurrency","tag-memory-management","tag-programming","tag-rust-lang"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/27767","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=27767"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/27767\/revisions"}],"predecessor-version":[{"id":27768,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/27767\/revisions\/27768"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=27767"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=27767"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=27767"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}