Repository navigation
fix!: Name and Alias incorrectly implement From<&str> - #423
Conversation
| let record_schema = RecordSchema::builder() | ||
| .name(name.clone()) | ||
| .aliases(Some(vec!["alias_1".into()])) | ||
| .aliases(Some(vec!["alias_1".try_into().expect("Alias is valid")])) |
There was a problem hiding this comment.
Can we have them both ?
This very first diff chunk easily convinces me that the From impl is very useful for such usages where the user is certain that it won't fail.
We just have to explain well in the rustdoc that if the string comes from user input and there is a chance that it may be invalid Name then TryFrom should be used.
WDYT ?
There was a problem hiding this comment.
That is not possible because of a generic implementation by Rust:
impl<T, U> TryFrom<U> for T
where
U: Into<T>,In my opinion this is important enough to break code, people who don't care can just use unwrap/?/expect.
I did update the commit to use ? in a lot of places.
There was a problem hiding this comment.
Implementing FromStr is a good idea, but I still want to get rid of From. It signals to the user that this is an infallible conversion, while it really isn't.
The documentation for `From` mentions the following: > The conversion is _infallible_: if the conversion can fail, use `TryFrom` instead; don’t provide a `From` impl that panics. This removes the `From` implementation and implements `TryFrom` instead. It also implements `TryFrom<String>`. I would have prefered to implement `TryFrom<T>` where `T: AsRef<str>` but that's not possible in the current type resolver. This is a **breaking** change, any user using `Name::into` or `Alias::into` will have to update their code.
3bda6f3 to
bdc2ee3
Compare
martin-g
left a comment
There was a problem hiding this comment.
Are there any advantages of TryFrom which FromStr does not provide ? Do we need both ?
Currently we have three same factory methods:
Name::new("")Name::from_str("")Name::try_from("")
and two with trait extensions:
"".parse()"".try_from()
try_from() is never the shortest one to type
impl TryFrom<String> for Name could be easily solved by the user by passing a reference: Name::from_str(&some_string) or &some_string.parse()
|
The nice thing about RecordBuilder::name(s: impl TryInto<Name>)Which means you can supply a
So in my opinion |
The documentation for
Frommentions the following:This removes the
Fromimplementation and implementsTryFrominstead. It also implementsTryFrom<String>. I would have prefered to implementTryFrom<T>whereT: AsRef<str>but that's not possible in the current type resolver.This is a breaking change, any user using
Name::intoorAlias::intowill have to update their code.