fix: safer property setter/getter parameters#336
Conversation
|
Thanks for looking into this! I agree the current A few concerns with this approach though:
I think the right direction here might be something like a trait (e.g. Would love to hear your thoughts on either of those approaches. |
revmischa
left a comment
There was a problem hiding this comment.
Appreciate the effort here, type-safe property names is a reasonable idea. A few things I think need addressing before this can land:
-
This is a breaking change to the public
Setter/Gettertraits, but there's no way for users to constructPropertyNamevalues from the library side. The helper macro is only in the example. Users doingproperties.set("my_prop", value)today would have no migration path without building raw pointers by hand. -
Some methods (
contains,clear,get_string,set_with_cleanup,get_type) still take&strwhile the trait methods now take&PropertyName. It'd be good to pick one approach and apply it consistently. -
The type check is runtime (
if name.ty != PropertyType::BOOLEAN), but Rust's trait dispatch already prevents passing the wrong value type. If you want compile-time safety for names too, something likePropertyName<T>with a type parameter might work better than a runtime check.
Would be nice to either provide a safe constructor/macro for custom property names from the library, or maybe accept both &str and &PropertyName through a trait bound so it's not a hard break for existing users.
The syntax of Properties::set is confusing.
The first parameter is called "name", but it expects PropertyName::value, while the second paramter is called "value". SDL_Set...Property functions don't return error, when a wrong string is supplied.
How about changing the setter's and getter's parameter from &str to &PropertyName and throw error on type mismatch?