Skip to content

Add TryFromIntError::kind method and IntErrorKind::NotAPowerOfTwo variant#153178

Open
sorairolake wants to merge 1 commit intorust-lang:mainfrom
sorairolake:feature/try-from-int-error-kind
Open

Add TryFromIntError::kind method and IntErrorKind::NotAPowerOfTwo variant#153178
sorairolake wants to merge 1 commit intorust-lang:mainfrom
sorairolake:feature/try-from-int-error-kind

Conversation

@sorairolake
Copy link
Copy Markdown
Contributor

@sorairolake sorairolake commented Feb 27, 2026

This pull request adds kind method to TryFromIntError, which outputs the detailed cause of converting an integer failing.

This is primarily intended for use in cases where there are multiple causes for failure, such as converting from a large signed integer type to a small signed integer type.

At the moment, this method returns &IntErrorKind. This type implements the Copy trait and could return IntErrorKind, but returns a reference to align with the behavior of ParseIntError::kind. If it is not necessary to align the behavior of TryFromIntError::kind with ParseIntError::kind, I'll change this method to return IntErrorKind.

Before this pull request, IntErrorKind was only used by ParseIntError, but I changed it to be used by TryFromIntError as well, so I updated IntErrorKind's documentation.

impl TryFrom<usize> for Alignment returns TryFromIntError as an error if the given value is not a power of two. However, IntErrorKind does not have a variant that represents this. So I added the variant NotAPowerOfTwo to represent this. For this variant to stabilize, both try_from_int_error_kind and ptr_alignment_type features must be stabilized.

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Feb 27, 2026
@sorairolake
Copy link
Copy Markdown
Contributor Author

@rustbot label +T-libs-api -T-libs
r? libs-api

@rustbot rustbot added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. and removed T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Feb 27, 2026
@sorairolake sorairolake force-pushed the feature/try-from-int-error-kind branch 2 times, most recently from 38cf8bf to 20c70f9 Compare February 27, 2026 15:21
@sorairolake sorairolake changed the title feat: Add TryFromIntError::kind feat: Add TryFromIntError::kind method and IntErrorKind::NotAPowerOfTwo variant Feb 28, 2026
@sorairolake sorairolake changed the title feat: Add TryFromIntError::kind method and IntErrorKind::NotAPowerOfTwo variant Add TryFromIntError::kind method and IntErrorKind::NotAPowerOfTwo variant Feb 28, 2026
@sorairolake sorairolake force-pushed the feature/try-from-int-error-kind branch from 20c70f9 to a0a5c69 Compare February 28, 2026 11:07
@sorairolake sorairolake force-pushed the feature/try-from-int-error-kind branch from a0a5c69 to 989926f Compare March 17, 2026 02:23
@sorairolake sorairolake marked this pull request as ready for review March 17, 2026 02:27
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Mar 17, 2026
@sorairolake sorairolake force-pushed the feature/try-from-int-error-kind branch from 989926f to e8c4298 Compare March 17, 2026 06:34
@sorairolake
Copy link
Copy Markdown
Contributor Author

r? libs-api

@rustbot rustbot assigned Amanieu and unassigned the8472 Mar 17, 2026
@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors bot commented Mar 27, 2026

☔ The latest upstream changes (presumably #154004) made this pull request unmergeable. Please resolve the merge conflicts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants