Skip to content

Commit

Permalink
Allow name and value types in set_string to differ (#3412)
Browse files Browse the repository at this point in the history
Co-authored-by: Kenny Kerr <[email protected]>
  • Loading branch information
kerosina and kennykerr authored Jan 8, 2025
1 parent 05f6b89 commit ec062c2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
4 changes: 2 additions & 2 deletions crates/libs/registry/src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ impl Key {
}

/// Sets the name and value in the registry key.
pub fn set_string<T: AsRef<str>>(&self, name: T, value: T) -> Result<()> {
pub fn set_string<T: AsRef<str>, U: AsRef<str>>(&self, name: T, value: U) -> Result<()> {
self.set_bytes(name, Type::String, pcwstr(value).as_bytes())
}

Expand All @@ -105,7 +105,7 @@ impl Key {
}

/// Sets the name and value in the registry key.
pub fn set_expand_string<T: AsRef<str>>(&self, name: T, value: T) -> Result<()> {
pub fn set_expand_string<T: AsRef<str>, U: AsRef<str>>(&self, name: T, value: U) -> Result<()> {
self.set_bytes(name, Type::ExpandString, pcwstr(value).as_bytes())
}

Expand Down
22 changes: 22 additions & 0 deletions crates/tests/misc/registry/tests/string.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::borrow::Cow;
use windows_registry::*;
use windows_strings::*;

Expand All @@ -13,6 +14,15 @@ fn string() -> Result<()> {
assert_eq!(key.get_hstring("string")?, "value");
assert_eq!(key.get_multi_string("string")?, ["value".to_string()]);

key.set_string("string_different_types", Cow::Owned("value".to_string()))?;
assert_eq!(key.get_type("string_different_types")?, Type::String);
assert_eq!(key.get_string("string_different_types")?, "value");
assert_eq!(key.get_hstring("string_different_types")?, "value");
assert_eq!(
key.get_multi_string("string_different_types")?,
["value".to_string()],
);

key.set_hstring("hstring", h!("value"))?;
assert_eq!(key.get_type("hstring")?, Type::String);
assert_eq!(key.get_string("hstring")?, "value");
Expand All @@ -25,6 +35,18 @@ fn string() -> Result<()> {
assert_eq!(key.get_hstring("expand_string")?, "value");
assert_eq!(key.get_multi_string("expand_string")?, ["value"]);

key.set_expand_string("expand_string_different_types".to_string(), "value")?;
assert_eq!(
key.get_type("expand_string_different_types")?,
Type::ExpandString
);
assert_eq!(key.get_string("expand_string_different_types")?, "value");
assert_eq!(key.get_hstring("expand_string_different_types")?, "value");
assert_eq!(
key.get_multi_string("expand_string_different_types")?,
["value"],
);

key.set_expand_hstring("expand_hstring", h!("value"))?;
assert_eq!(key.get_type("expand_hstring")?, Type::ExpandString);
assert_eq!(key.get_string("expand_hstring")?, "value");
Expand Down

0 comments on commit ec062c2

Please sign in to comment.