Hello MessageBox: Is this good Rust?


#1

Hi!

I tried implementing a small “Hello World” gist using a Windows MessageBox.

Is this “good style”? I.e. is that a good basis to start building a Windows applications?

Thanks in advance for your insights!

Cheers,
Kosta


#2

There’s no need to use encoding to do utf-8 <-> utf-16 conversions because OsStringExt in std already provides an encode_wide method to convert to UTF-16.

I personally use something like this for working with wide strings.

trait ToWide {
    fn to_wide(&self) -> Vec<u16>;
    fn to_wide_null(&self) -> Vec<u16>;
}
impl<T> ToWide for T where T: AsRef<OsStr> {
    fn to_wide(&self) -> Vec<u16> {
        self.as_ref().encode_wide().collect()
    }
    fn to_wide_null(&self) -> Vec<u16> {
        self.as_ref().encode_wide().chain(Some(0)).collect()
    }
}
let s = "some string".to_wide_null();
SomeWinapiFunction(s.as_ptr());

#3

Hi @retep998!

First of all, thanks for providing these awesome winapi bindings! :smile:

I figured that I was missing something when using encoding.

Still, I don’t understand your code… x refers to self, right?

But where is encode_wide? I don’t find it anywhere in the docs, even in nightly:
https://doc.rust-lang.org/nightly/std/?search=encode_wide

It’s definitely not on OsStringExt (should I be worried that this is in std::os::unix::ffi?)
https://doc.rust-lang.org/nightly/std/os/unix/ffi/trait.OsStringExt.html

Do you mean the unstable encode_utf16?
https://doc.rust-lang.org/nightly/std/primitive.char.html#method.encode_utf16

Thanks in advance for your insights!

Cheers,
Kosta


#4

The online docs are generated on Linux and don’t show any of the Windows-specific parts. Try looking at the locally installed documentation.


#5

Oh yeah, that was a typo, replace x with self.
The documentation online is for linux, so you’ll need to use the offline docs that came bundled with your Windows Rust installation instead.
encode_wide is part of std::os::windows::ffi::OsStrExt and from_wide is part of std::os::windows::ffi::OsStringExt. These functions are definitely stable but they’re also Windows specific.


#6

I have a funny feeling when your question is same as mine :smile: :
http://www.reddit.com/r/rust/comments/37k1ij/converting_str_to_utf16/crnjydq

issue on missing doc for windows platform:


#7

@retep998 Aaaaahhh, now OsStr starts to make some sense! :smile:

@bungcip Thanks for linking these issues! I commented on them.

I think we should really have some resource on how to start with Rust development on Windows, what the current state is, which PRs/issues to follow etc. It’s inefficient to have this only on this forums/reddit and every developer asking for themselves.

Where should such a page be hosted? I’m not aware of a rust wiki…