microsoft / windows-rs

Rust for Windows
https://kennykerr.ca/rust-getting-started/
Apache License 2.0
10.41k stars 489 forks source link

[`windows-registry`] `set_string` `set_expand_string` and `set_multi_string` are hard to use #3313

Open Link1J opened 1 week ago

Link1J commented 1 week ago

Suggestion

set_string set_expand_string and set_multi_string only accept one generic type. This can make them hard to use, as it means the name and value need to use the same string type. Can it be changed so that they have two generic types, one for name and another for value?


New Signatures:

fn set_string<T: AsRef<str>, V: AsRef<str>>(&self, name: T, value: V) -> Result<()>;
fn set_expand_string<T: AsRef<str>, V: AsRef<str>>(&self, name: T, value: V) -> Result<()>;
fn set_multi_string<T: AsRef<str>, V: AsRef<str>>(&self, name: T, value: &[V]) -> Result<()>;

[!Note] set_multi_string does not accept an empty array easily with this signature.


I came across a problem with code like this.

let order_key = windows_registry::LOCAL_MACHINE.create(r"SYSTEM\CurrentControlSet\Control\NetworkProvider\Order").unwrap();
let mut providers = order_key.get_multi_string("ProviderOrder").unwrap();
providers.push("TestThing".into());
order_key.set_multi_string("ProviderOrder", &providers).unwrap();

It was causing an error (E0308), because providers was a Vec<String> and the generic type T was &str. I fixed by adding .to_string() after "ProviderOrder". I feel like that is bad rust code, but it was the easiest way to fix it.

kennykerr commented 1 week ago

Thanks for the suggestion!