16 ms·
Rust/WinRT Public Preview
- dstaley 7y agoI'm super excited for this! I do wish that they'd ressurect their JavaScript projection for the runtime though. Especially with their focus on React Native, it'd be awesome not to need a "native" module to call Windows APIs. That being said, I also hope Rust/WinRT becomes a valid native module format for React Native (though I suspect it won't since that would likely require React Native developers to have the Rust toolchain setup to compile the module).
- weiming 7y agoNow I'd like some native Rust bindings for Cocoa/UIKit, perhaps as an "official"/higher-level alternative to https://crates.io/crates/objc https://crates.io/crates/objc. There are some crates already attempting to replicate all of Apple frameworks, but they are community-supported/require a bit of `unsafe`.
- Klonoar 7y agoYou won't get around the unsafe. I've been hacking away at these for a few months now: https://github.com/ryanmcgrath/cacao https://github.com/ryanmcgrath/cacao Goal is to get a good-enough version to build apps with. I'm dogfooding it for my next product so there's some incentive to keep pushing.
- roblabla 7y agoThe state of Rust MacOS bindings is pretty bad. Last I checked, a lot of crates are built on top of the `core-foundation` crate which is full of unsound abstractions. And a lot of them seem to be hard to fix, as the Core Foundation types are all equivalent to `Arc<Data>` with no synchronization of the inner data. This makes mutable collections very complicated to model, for instance.
- oaiey 7y agoI start to understand the Windows team desire for this WinRT. You can flow through time with these language bindings without being locked into a programming language. .NET, JS, C++ and now Rust. That is seriously cool.
- hota_mazi 7y agoIt's been a reality on Windows for over 20 years, though. COM has made it possible to write applications on Windows and integrate with pretty much anything in it in any language with COM bindings. Glad to see Rust is now one more such language.
- Kipters 7y agoWinRT is actually built on COM, Rust/WinRT uses it under the hood
- badsectoracula 7y agoYeah though... pretty much any natively compiled language and many interpreted/VM-based languages can use the C ABI in any modern plaform anyway, so what COM provides isn't so much the ability to use the same API from many languages but a dynamic object oriented approach for doing so. After all it isn't like you can't use the Win32 API directly from Rust - or any other language, e.g. Python.
- kevingadd 7y agoThe type libraries are a big part of why it's so successful - you can take a typelib and auto-generate bindings, or manually generate your own bindings based on what you see. If you look at the typelibs for a given GUID, you are almost guaranteed that the API will match once you get a pointer back from a QueryInterface call (sometimes people are naughty). This is nice because you can compile all that in so the QueryInterface call is the only dynamic part of it - everything else at runtime is just regular calls through a vtable. The rigid nature of it means you can also reverse-engineer compiled code to figure out the shape of a given interface's vtable even without a typelib (I've done that a couple times). This is why you were able to manipulate COM objects from VBScript and JScript even though they were dynamic languages, and in fact VBScript could define new COM objects on the fly and then expose new methods and properties through a COM interface called IDispatch. It's all just vtables once you do your initial setup. IIRC there was a Windows version of Python for a while that had built-in support for COM interop in both directions - you could define IDispatch-based COM objects in Python and implement interfaces while consuming COM libraries using their typelibs.
- xrd 7y agoI stopped reading at "The Windows Runtime is based on Component Object Model (COM) APIs..." Is that a bad choice on my part? I see COM and think of OLE, CORBA and I remember that I'm old and going to die pretty soon (within the next 40-50 years almost assuredly). https://en.wikipedia.org/wiki/Component_Object_Model https://en.wikipedia.org/wiki/Component_Object_Model
- sleepinseattle 7y agoIt’s not that different from things like Protocol Buffers, and for application code it’s mostly a hidden implementation detail. The API you’re calling (like the Composition API’s in the Minesweeper example) might be calling into a separate process but you don’t have to care. Disclosure: I work on the Windows team
- blinkingled 7y agoTrouble is you can't really the solve the problem Microsoft had without using something like COM. Good news is you don't have to care - you can use C# or Rust and not worry about COM.
- pjmlp 7y agoSomehow I think .NET would have been much better had it started as something like .NET Native, but yeah cannot turn back in time.
- barrkel 7y agoIt would have been no good for web application servers, which is where the competition was going. Native would have chased the declining native GUI market.
- pjmlp 7y agoSure it would, after all that is where backend development is going to, back to AOT compilation toolchains. If Delphi never managed much on the server side, it has more to do with the downfall from Borland than anything else. So ASP + COM would be ASP.NET with COM+ Runtime instead.
- blinkingled 7y agohttps://github.com/robmikh/minesweeper-rs https://github.com/robmikh/minesweeper-rs - I just love how they brilliantly avoided mixing up C++/COM things into this. Tough one to pull when you're dealing with bindings for a foreign language. The code looks fairly standard Rust (except maybe for the winrt::import that looks like Go?). > If you are familiar with Rust, you will notice this looks far more like Rust than it looks like C++ or C#. Notice the snake_case on module and method names and the ? operator for error propagation. Developing on and now for Windows just keeps getting better.
- pathartl 7y agoIt seems like they're doing all they can to get developers on their platform. Not to establish Windows developers like they have in the past, but to have others see Windows as just another platform. We're reaching a revolution from moving from x86 to ARM and I think the more Microsoft positions themselves like this, the less likely they are to be left in some weird compatibility limbo. As weird as this sounds, I feel more confident with Microsoft being able to do a successful transition to ARM than Apple at this point.
- pianoben 7y agoIn fact, the _have_ transitioned to ARM, way back around the time of the first Surface tablet. Windows and Office have been running on ARM since at least, what, 2012?
- loeg 7y agoWindows NT in 2000? I guess ARM was not a going concern at the time, but Windows was remarkably portable in 2000 before the dark decade of more or less exclusively Wintel.
- zrm 7y agohttps://archive.org/details/Microsoft_Windows_NT_Server_Version_4.0_94373_Microsoft_1996 https://archive.org/details/Microsoft_Windows_NT_Server_Vers... "Disc contains code to run on Windows NT-compatible x86, Pentium, MIPS, R4x00, Alpha, PowerPC and Pentium Pro systems"
- jimbob45 7y agoWish they'd open the source and let the Rust community do this kind of stuff instead. No need to waste Windows development time on niche stuff.
- steveklabnik 7y agoThe article has a very prominent https://github.com/microsoft/winrt-rs https://github.com/microsoft/winrt-rs in it, where you will find that this is MIT licensed.
- snuxoll 7y agoOpen what source? Everyone has access to the winmd files necessary, the COM ABI is documented - there’s been nothing stopping anyone from doing this. All of the work MS has done on these bindings is MIT licensed as well.
- contextfree 7y agoIn fact there were third-party bindings already which I worked on: https://github.com/contextfree/winrt-rust https://github.com/contextfree/winrt-rust the new first-party ones look to be more complete and polished but this is mainly due to time and skill limitations on my part. :)
- pie_flavor 7y agoIt's so open source, the developer of C++/WinRT didn't even work for Microsoft when he made it.
- pcwalton 7y agoThe source is open, as already mentioned. But as for spending developer resources, I think having Microsoft work on things like this helps Rust and Windows alike. It helps Rust by lending a sense of official platform support to the language. And it helps Windows by encouraging Rust users to develop for the platform.
- cs702 7y agoYou have to hand it to Microsoft: It may be the only large software company that doesn't seem susceptible to not-invented-here syndrome. It's true that Microsoft historically has been an intensely competitive company, often trying to undermine competing technologies, e.g., with "embrace and extend" strategies. But whenever a competing technology -- whether a language, or a framework, or an application -- gains adoption with developers or users, Microsoft to its credit will follow along, sooner or later, and do the work necessary for making it a first-class citizen on Windows. (For those who don't know, Rust is originally a Mozilla project.)
- nemothekid 7y agoIt's in their blood - its the 'Embrace' in EEE.
- acdha 7y agoYou know that’s from 3 decades and 2 CEOs ago, right? They’re a large company so there’s sure to be plenty to dislike but I don’t think it’s contributing much without some analysis of their actions in this century.
- 29athrowaway 7y agoLet me introduce you to Steve Ballmer, the CEO before the current one, Satya Nadella: https://www.theregister.co.uk/2001/06/02/ballmer_linux_is_a_cancer/ https://www.theregister.co.uk/2001/06/02/ballmer_linux_is_a_... He was CEO of Microsoft until 2014, that is, 6 years ago. Microsoft changed strategies because they tried everything they possible could to counter open source and Linux and lost. Now they are forced to play nice. Being forced to be nice is not the same as being nice out of altruism. If at any moment becoming jerks gives them more shareholder value they will become jerks again.
- fortran77 7y agoThey _lost_? They have a 1.36 trillion USD Market cap (today). That's more than Apple's 1.29 trillion USD Market cap. It's hilarious how people on hacker news live in this bubble where Microsoft has faded away into irrelevance. And they're also very good. I'm running Windows 10 right now on a dual-monitor, dual NVidia GPU, dual Xeon system and everything "just works."
- alkonaut 7y agoNever really understood whether WinRT binds me to store apps, sandboxes, appx and so on. Can I develop a single windows app with this that runs on Windows 10 desktop as a win32 app does (such as a plugin to another application where I can’t can’t decide the deployment model)?
- pjmlp 7y agoThe future of Win32 is also bound to sandboxing, although we are not yet there. "How Windows 10X runs UWP and Win32 apps" https://www.youtube.com/watch?v=ztrmrIlgbIc&list=PLWZJrkeLOrbZ3CMHpbglE4NyJSEo-_qhr&index=5&t=8s https://www.youtube.com/watch?v=ztrmrIlgbIc&list=PLWZJrkeLOr... WinRT is an improvement of COM, it either runs sandboxed in UWP (its home), or you can access it from Win32 via XAML Islands and Win32/UWP interop. Right now Win32 applications can still choose between legacy mode, or opt into Win32 sandoxing via MSIX packages. As Windows 10X shows, it might not be for long.
- Rusky 7y agoA lot of WinRT APIs are usable from "normal" desktop win32 apps, but some are restricted to the sandboxed/appx/etc model. The most prominent of these is probably the XAML GUI stuff, but those are becoming available everywhere in the near future, and being decoupled from the OS itself, to go along with WinUI 3.0
- sleepinseattle 7y agoThe minesweeper example from the article is a regular Win32 app. Desktop Win32 apps can call WinRT API’s. https://github.com/robmikh/minesweeper-rs/blob/master/src/main.rs https://github.com/robmikh/minesweeper-rs/blob/master/src/ma...
- contextfree 7y agoWinRT, as in the API/ABI shape, at this point really isn't tied to any aspect of the UWP app model (other than that existing UWP-related APIs tend to be newer and more likely to use it). Third-party WinRT component activation used to depend on app packaging but that dependency was removed last year: https://blogs.windows.com/windowsdeveloper/2019/04/30/enhancing-non-packaged-desktop-apps-using-windows-runtime-components/ https://blogs.windows.com/windowsdeveloper/2019/04/30/enhanc... In general what used to be various facets of the "monolithic" Win32 vs. UWP divide have been refactored to allow a la carte usage, so not only can you activate your WinRT components without a packaged app, but you can also register your app as "packaged" for runtime purposes without having to install in that format ( https://blogs.windows.com/windowsdeveloper/2019/10/29/identity-registration-and-activation-of-non-packaged-win32-apps/ https://blogs.windows.com/windowsdeveloper/2019/10/29/identi... ), can install in the packaged format without having to use the sandbox features ( https://docs.microsoft.com/en-us/windows/msix/overview https://docs.microsoft.com/en-us/windows/msix/overview ) can distribute sandboxed apps outside the store ( https://docs.microsoft.com/en-us/windows/msix/app-installer/app-installer-root https://docs.microsoft.com/en-us/windows/msix/app-installer/... ), etc. The only remaining aspect I'm aware of where there's still a binary decision and coupling is in the windowing model and shell integration; a Win32 process's top-level windows can only be HWNDs and a UWP process's top-level windows can only be CoreWindows. Win32 HWNDs can host both Win32 and UWP UI, but UWP CoreWindows can only host UWP UI; on the other hand, only UWP CoreWindows can make use of UWP shell integration features like fullscreen and picture-in-picture. It would make sense to somehow decouple this facet as well but I'm not aware of any plans to do so.
- boromi 7y agoSorry for the naive question, but how does this fit into WinUI 3.0?
- camccar 7y agoYes
- deleted 7y ago[deleted]
- m-hilgendorf 7y agoWhile we've been hacking on the vst3-sys crate [0], we wound up forking the mentioned com-rs crate to support COM APIs on non-win32 targets (notably, there's some stuff with endianness of the IIDs, the calling convention of the generated vtables) as well as usability (the com-rs crate only supports up to 5 interfaces implemented at once). We're still chasing down some issues with order mattering when implementing the interfaces. Even without those small changes, it's an impressive piece of macro programming that is very close to being usable for general purpose, ABI-stable rust crates. As a little dose of irony, clippy doesn't like the com-rs crate too much. [0] https://github.com/RustAudio/vst3-sys.git https://github.com/RustAudio/vst3-sys.git
- ridiculous_fish 7y agoHow does this projection work with the borrow checker? UI frameworks typically allow you to have multiple mutable reference to the same element.
- Rusky 7y agoIt works fine- Rust only forbids multiple `&mut T`s to the same object, but `&mut T` doesn't exist outside of Rust, so code in other languages doesn't have to care about that rule at all. These bindings can just expose non-Rust pointers as (wrapped) raw pointers, leaving all the actual dereferencing and mutation to the other language. WinRT objects don't expose anything but virtual methods on opaque objects anyway, so this isn't even Rust-specific. Outside of WinRT this sort of thing requires a bit more thought, but you can still use the usual tools- `Rc` and `Cell` or their variants, or raw pointers and `unsafe`. You can think of `&T` and `&mut T`, and their associated "no shared mutability" rule, as a compiler-checked version of C's `restrict`. Stop using `restrict` and the rules go away.
- ridiculous_fish 7y agoIs the projection literally "wrapped raw pointers?" How are the raw pointers wrapped - for example is there interop between COM reference counting and Rust Arc? Or something else? Here's a classic trap: you get a reference to something interior, and then the parent is deallocated. My event handler gets a ref to this button's label and then replaces the button. Is this possible with Rust/WinRT? (No judgement either way!)
- pornel 7y agoRust's borrow checker solves the problem of "get a reference to something interior, and the parent is deallocated". It knows (by static analysis) when an inner reference may be in use, and won't let the program compile if the parent could deallocate during that time. This works well even with externally refcounted objects. You can give reference to an inner object without increasing its refcount, and rely on the borrow checker not to allow the parent to decrease the refcount. Or you bump the refcount of the inner object and give it out as an independently owned object.
- jhoechtl 7y agoWould this be possible for Go too?
- favorited 7y agoIt is possible for any language, really. They already have 3 language projections for C++ (C++/WinRT being the newest, by the same person running this Rust projection), C#, VB, and JavaScript. Under the hood they call out to (and potentially expose) COM interfaces which are C. So if your language has C interop, or if it could be added, something like this could be built.
- camccar 7y agoC interop is slow in golang so I don’t think it would be the best language for it. People do it with SDL though
- znpy 7y agoI want to reply to many other comments but I want to make this more visible: Microsoft does not do Embrace-Extend-Extinguish anymore today (or at least, it does a lot lot less). You know who does Embrace-Extend-Extinguish? Google.
- brian_herman__ 7y agoThis is so cool. I should brush up on my rust maybe I should make a python extension or something...
- adzm 7y agoWith Windows 7 on its way out slowly, WinRT is becoming more palatable.
- loeg 7y agoSlowly?! Hasn't Win7 been dead for years? > Latest release Service Pack 1 (6.1.7601) / February 22, 2011; 9 years ago > Mainstream support for Windows 7 ended on January 13, 2015. There is: > Extended support for Windows 7 ended on January 14, 2020. But that is referring to very specific business contracts, not casual users pretending EOL never happened.
- ploxiln 7y agoCasual users still received updates for Windows 7 until January 14th, 2020. Businesses can get "Windows 7 Extended Security Updates (ESU)" for a maximum of 3 years after January 14th, 2020. https://support.microsoft.com/en-us/help/4527878/faq-about-extended-security-updates-for-windows-7 https://support.microsoft.com/en-us/help/4527878/faq-about-e...
- badsectoracula 7y ago> Slowly?! Hasn't Win7 been dead for years? No. Microsoft wants to kill it, but... even just checking Steam's hardware survey (users of which are more likely to upgrade so chances are overall PC usage of Win7 is higher) there is a combined ~7% of users on Win7. With ~25M concurrent users the last 48 hours, that means there were more than 1.7M users running on Win7 just within the last two days. FWIW these numbers are a bit lower than 10 times the Linux numbers of all distros combined.
- mwcampbell 7y agoLooks like async/await support isn't ready yet. I wonder how challenging it will be to integrate WinRT's async model with Rust's futures and tasks.
- ComputerGuru 7y agoIAsyncOperation is pretty flexible. I don’t think it will be technically hard, though perhaps picking the best fit model will be a time consuming process.
- optimiz3 7y agoCoding in C++/WinRT (similaly, C++/CX) is in my experience a major mistake when targeting WinRT/UWP using C++. The primary use case for WinRT/UWP is making apps for the Microsoft Store, where most of the time you're going to be sharing code also targeting Google Play and the Apple Store given those markets are vastly larger (and have viable ad-platforms; Microsoft is shutting theirs down on June 1st). The C++/WinRT and C++/CX language projections are incompatible with compilers that target Android and IOS due to WinRT reference specific syntax and keywords such as the '^' reference indicator. As an alternative, it is possible to access all of the same WinRT/UWP platform functionality in pure C++ without the C++/WinRT syntactic sugar extensions, allowing you to cleanly abstract the WinRT/UWP platform APIs relative to Android and IOS. This is not well documented but by far a superior option for folks familiar with COM and Windows internals. Furthermore, avoiding things like '^' reference operator syntax gives you tighter control over object lifetime as C++/WinRT and C++/CX hide to some degree the mechanics of COM object lifetime and gives a pseudo-environment much more like C# on .Net. Microsoft does provide the WRL pure C++ template library, which is in many ways a spiritual successor to the ATL COM template library. WRL is in my experience the best way to interact with COM objects surrounding WinRT and UWP. (Source: developer of several top 10 apps (at times) on Microsoft Store). Glossary of Microsoft terms: WinRT/UWP (WinRT (Windows Runtime) the old name used in Windows 8/8.1 for what later was renamed to UWP (Universal Windows Platform) in Windows 10). WinRT/UWP was originally intended to be an object-oriented replacement API for Win32 but hasn't lived up to its charter due to only being useful for Microsoft Store apps and being significantly less capable than Win32. COM - Component Object Model; an object-oriented API style used by some Win32 APIs, this style was used near-exclusively as the basis for the WinRT APIs. COM can be unweildy to deal with so there are several C++ template libraries like WRL and ATL to make it easier. C++/WinRT (and C++/CX) are Microsoft specific C++ extensions that require MSVC and hide the complexity of dealing with COM relative to WinRT/UWP APIs.
- hyperrail 7y agoC++/WinRT and C++/CX are not the same; they are different enough that Microsoft Docs has a whole page explaining how to move from C++/CX to C++/WinRT: https://docs.microsoft.com/en-us/windows/uwp/cpp-and-winrt-apis/move-to-winrt-from-cx https://docs.microsoft.com/en-us/windows/uwp/cpp-and-winrt-a... In particular only the older C++/CX has ^ pointers or other Microsoft-specific C++ language extensions. C++/WinRT is standard C++. Writing your code using WRL (Windows Runtime C++ template Library) is good if you want to learn enough about how the Windows Runtime extensions to COM work in order to debug effectively. However, its close relationship to the underlying COM and Windows Runtime APIs also makes it far too verbose to quickly write good API-consuming and -producing code, in my opinion.
- wicket 7y agoWell this really confused me as I thought Microsoft had discontinued the Windows RT operating system. It turns out that they also have a platform-agnostic application architecture, ingeniously named WinRT.
- zeroc8 7y agoAs a former MFC/Winforms/WPF/Qt now turned Angular developer, this is really sparking my interest. I would love to back to writing great desktop applications. Problem is, most of my projects nowadays require a multiplatform GUI. Desktop is not enough, we also need web and mobile.
- Ericson2314 7y agohttps://github.com/retep998/winapi-rs https://github.com/retep998/winapi-rs they should work with this
- steveklabnik 7y agoThese are two different APIs, so it's not clear to me how they would.
- Ericson2314 7y agoI thought there was much overlap
- steveklabnik 7y agoMy understanding is that there’s a lot of overlap but also stuff that only one or the other can do.
- Ericson2314 7y agoWould `#[cfg(..)]` be the right way to do this then?