Rust for Windows - Part 1

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome to rust for Windows a new series for Windows developers wanting to learn how to make the most of rust as a Windows developer my name is Kenny Kerr and in this Preparatory installment I'll show you what you need to turn your vanilla Windows Dev box into a rust developer machine I'm going to assume you have a recent version of Windows and have already installed Visual Studio you'll mainly want to make sure you've installed the visual C++ developer tools workload you also have other options in terms of using the Standalone visual C++ build tools or even a different C++ compiler but Visual Studio is just the simplest way to get started rust can Target any tool chain but on Windows Visual Studio is just the default and preferred tool chain keep in mind that rust is not depend on C++ itself it does however at least for now require a Linker and happens to use the same object in debug formats as your local C++ tool chain not that you ever have to really think about Visual Studio again nor will you want to after you see how straightforward and easy the rust tool chain is as an abstraction so now let's open the browser and find the rust bootstrapper called rust up you can search for rust's website one way or another one option is to hit the the get started link and there's the link to the rustup installer alternatively you can go directly to rust up. RS and there again you'll find the same link let that download and you can see I've already downloaded it in the past but as you can see this is just a standard command line installer you proceed with the installation and it takes care of the rest and in moments you'll have the rust tool chain installed on your machine once this completes you're almost ready to begin your journey as a rust developer the only thing left at least to get started is vs code a good ide is priceless and vs code naturally supports rust out of the box from this point on you can simply run rust up on the console as needed to update your rust toolchain to grab the latest rust compiler or install an optional tool chain component okay let's now get started for real we'll be going through a number of topics about this memory safe systems's programming language not the least of which is memory Safety and Security but in this first installment let's hit the ground running by calling a couple of apis if you've lived blissfully unaware of Windows these last few decades it is an operating system full of apis or system calls various programmatic features ready for developers to exploit and combine in weird and wonderful ways without of Desire or necessity calling Windows apis is a big part of Windows programming let's create a scratch project or package that we can use to write some code cargo is rust's build system or package manager basically the Central and largely only tool you'll need to drive your development workflow here we've just created a new binary package or project and we'll go ahead and open that in vs code this is way you'll live as a rust developer on Windows or really any platform in the Explorer panel to the left you'll see that cargo created a simple folder structure you honestly didn't need cargo to create this for you as it's so straightforward but it helps nonetheless this is the Manifest or description of your project what car calls a package or crate it's just where the package specific configuration lives in a very simple format so no IDE is required to try to make sense of it you don't need an XML parser you can just edit this directly as needed now we'll explore this more deeply later on you'll notice that cargo favors git and creates a default git ignore file on the assumption that you'll soon be pushing your code to a get repo the target folder is excluded by default this is just where cargo will by default write build artifacts in output this is of course optional and you can delete the get ignore file if that if that's not for you and there is the meat of the package with a oneliner to welcome you to the world you can go ahead and use cargo again to build and run it as follows congrats perhaps you've just built your first rust project on Windows now about those windows apis just like this sample package there is a whole ecosystem of other packages either binary packages like this one or Library packages often called crates that can be shared and depended on such dependencies are described in the packages cargo. tumml file this tells cargo to hit up the crates Reg looking for a crate called Windows a quick diversion here you can see the default public crates registry online at [Music] crates.io and I can go to the windows crate where you'll find information about the latest versions documentation and so forth just FYI we'll explore the crates registry in future now back to vs code we'll tell it what version uh to use and we can enjoy semantic versioning and avoid all the trouble with library dependencies in languages like C++ finally and this is a bit of a big feature but crates can themselves be conditionally compiled to avoid pulling in each and every feature mostly to reduce build time for a large crate like Windows so we'll just add a single feature in this case we'll just tell cargo would like to use the apis from the 132 system threading subm module rust code within a package is logically structured into modules somewhat like name spaces in C++ or C excellent let's rebuild and see what happens and just like that cargo chases down the new dependency and any dependencies that it has making sure everything you need to build and link for the current Target machine is primed and ready to spit out code don't worry too much about what's all going on there just realize that in a few seconds you went from a vanilla rust environment to a fully loaded Windows development environment at your fingertips and of course cargo caches all of this for you so the next time you build it takes no time at all okay let's write some code we can employ a used declaration to make these AP API is a little more accessible this is not unlike a using namespace declaration in C++ or C now in order to prove that the code works and yet keep it real simple let's just use the thread pool to increment a counter some number of times we can use this reader writer lock from the rush standard library for safe and multi-threaded access to the counter variable as follows for this example I'll just use a simple main function with a big unsafe block since virtually everything here is going to be unsafe why is that well the Windows crate lets you call foreign functions foreign to rust anyway and these are generally assumed to be unsafe we'll talk more about safety and unsafe code in due time a very important topic as we think about security and memory safety the windows crate also offers Rich error handling critical to realistic systems programming so we'll just update the main function to handle error propagation as follows rust's approach to error propagation is pretty neat but we'll explore that some other time okay let's call some apis the threadpool API is modeled as a set of objects exposed via a traditional C style API the first thing we need to do is create a work object the first parameter is a pointer to a callback function the remaining parameters are optional and you can read more about them in my threadpool series on msdn the Callback itself must be a valid cstyle callback according to the signature expected by the threadpool API here is a simple callback that we'll then use to increment that counter [Music] the parameters can safely be ignored but do come in handy from time to time perhaps we'll do a threadpool deep dive in future for the sake of this example we'll just increment the count from within the Callback to prove it's being called this might seem a little unusual with writes and unwraps but you'll quickly start recognizing the patterns from the rust standard Library the right is just requesting a right lock on the reader writer lock the unwrap is just asserting that the lock isn't poisoned you can technically handle lock poisoning but will not do so here the unwrap will effectively Panic at this point we have a valid work object but nothing is happening yet in order to kick off some work so actual work we need to submit the work object to the threadpool you can do so as many times as you like so we'll just go ahead and do it 10 times to keep things predictable you can now expect the callbacks to run concurrently hence the reader writer lock of course with all of that concurrency we need some way to tell when the work is done that's the job of the wait for threadpool work callbacks function quite a mouthful the second parameter indicates whether we would like to cancel any pending callbacks that have not started executing passing false here thus indicates that that we would like the weight function to block until all of the submitted work has completed at that point we can safely close the work object to free its memory memory and just to prove that it works reliably we can print out the counter's [Music] value here again we're using the locks corresponding read acquisition method that can also fail due to poisoning and let's just print out the [Music] sum and let's see what cargo has to say about that oh looks like I have a typo this should be result sweet the system thread pool successfully spun up just enough worker threads to maximize concurrency and system efficiency in the pursuit of getting that work done as quickly as possible making sure not to over schedu the CPU congratulations you've Co a very powerful Windows API in just a handful of lines and in Rust of all things we've glossed over a bunch of things uh from where these definitions or bindings come from how linking occurs wait there's a Linker yep and much more we'll explore it all in due time so stay tuned for more in this new series on R for Windows until next time my name is Kenny Kerr
Info
Channel: Kenny Kerr
Views: 547
Rating: undefined out of 5
Keywords:
Id: MfwtC55eNzU
Channel Id: undefined
Length: 13min 28sec (808 seconds)
Published: Tue Jun 18 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.