Skip to content

fix!: Name and Alias incorrectly implement From<&str> - #423

Merged
martin-g merged 2 commits into
mainfrom
fix/name_alias_from
Jan 23, 2026
Merged

martin-g merged 2 commits into
mainfrom
fix/name_alias_from

Conversation

@Kriskras99

Copy link
Copy Markdown
Contributor

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.

@Kriskras99
Kriskras99 requested a review from martin-g January 22, 2026 20:05
Comment thread avro/src/schema/record/schema.rs Outdated
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")]))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or maybe use FromStr ?!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@martin-g martin-g left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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()

@Kriskras99

Copy link
Copy Markdown
Contributor Author

The nice thing about TryFrom is that you can use it as a trait bound:

RecordBuilder::name(s: impl TryInto<Name>)

Which means you can supply a Name, &str or String (and any user types that implement TryFrom<T> for Name) to that function and they'll all work.

FromStr is less useful, its main usage is allowing let name: Name = "some.name".parse()? to work.

So in my opinion TryFrom is way more useful than FromStr.

@Kriskras99 Kriskras99 mentioned this pull request Jan 23, 2026
@martin-g martin-g added this to the 0.22.0 milestone Jan 23, 2026
@martin-g
martin-g merged commit 08deef9 into main Jan 23, 2026
@Kriskras99
Kriskras99 deleted the fix/name_alias_from branch January 23, 2026 11:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants