-
Notifications
You must be signed in to change notification settings - Fork 13.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add truncate_into()
#79477
Comments
Just want to point out that type inference deciding what type to truncate into can be somewhat foot gunny |
You may be interested in |
Re the potential type inference issues: We could also just start with |
I think "as" is a sharp tool that has to nearly become deprecated in normal Rust code. But I think a global vision about the whole casting topic is needed to avoid a patchwork solution. |
I think this is a large enough change that it needs to be an RFC, not just an issue. Any trait in I'd definitely like to get |
So should I write an RFC for this or is it covered by the |
I'm on lang, not libs, so as always take what I say with a grain of salt about library things. But what I'd say is that you should take the existing RFC, branch it, and cut it down to just Personally I like
|
I agree with WrappingFrom being better than TruncateFrom for all the listed reasons, but one thing is the Wrapping terminology only works for integers and not for conversions involving floats. |
Completely agreed, but I'm ok with that, as I was putting questions of floating-point as explicitly out of scope for this. (I'd like to get a solution for that too, but it seems fundamentally harder -- should it have rounding mode control, for example? -- so would like to side-step it to be able to make progress on an operation that I think has fewer open questions.) I would consider truncation of floats to be a subset of rounding -- materially different from truncation of integers (affecting low bits vs high bits, in the meanings I'm used to, for example) -- and thus using different traits is an advantage. |
Hmm, I'm a lot less sold on the utility of this as just for integers cases, but I guess that can be born out later in the RFC discussion... |
This is a proposal to add a
truncate_into()
method on the various smallprimitive types (
u*
,i*
, andf*
primarily).Currently, an
as
cast can be used to 'upcast' from e.g.u8
tou16
, but itcan also be used to 'downcast' from e.g.
u16
tou8
. The downcast variantwill truncate any bits that can't fit into the smaller variant, which may be
unexpected behavior. Changing
as
's behavior so it does not support downcastingwould be a big task, and perhaps not worth it – that is out of the scope of this
proposal. Instead, I propose to add a new method,
truncate_into()
thatperforms the downcasting behavior such that it is clear in the code what is
happening.
It would likely be implemented as two traits,
TruncateFrom
andTruncateInto
,akin to
TryFrom
andTryInto
.TruncateInto
would be implemented in terms ofTruncateFrom
, andTruncateFrom
could just useas
internally. In thefuture, if we change the behavior of
as
, we would probably have to convertinto an array first and then ignore some of the bytes.
This proposal stemmed from discussion on Zulip about the confusion arising
from
as
's truncating behavior.The text was updated successfully, but these errors were encountered: