Skip to content
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

impl From<&Big[U]Int> for Big[U]Int #204

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

coolreader18
Copy link

@coolreader18 coolreader18 commented May 22, 2021

I know this impl is a little strange, but I have a use case and a rationale.

So, I have a function that takes any T: Into<BigInt> + ToPrimitive. It converts the operand to an i32 and checks whether it's in some low range, and if it is, it can do an optimized version with the i32. Otherwise, it converts the operand to a BigInt and uses that. Currently, we have 2 versions of the function, one taking that generic and the other taking a &BigInt, since it wouldn't have to clone the BigInt unless it's not in the low range. I could probably just make my own trait, but due to orphan rules I wouldn't be able to do the blanket impl, so it'd just be a bunch of manual impls. If this were implemented, I'd be able to have just the one function, and it would get the optimization for free.

Also, the standard library containers like Vec or String have a From<&slicetype> impl (where slicetype is [T] or str), but since BigInt doesn't deref to anything and you pass it by reference just as &BigInt, I think it makes sense in general to have a impl From<BorrowedFoo<'a>> for Foo like standard library types do.

Alternatively, I guess there could be a IntoBigInt trait in addition to the ToBigInt one.

@cuviper
Copy link
Member

cuviper commented May 25, 2021

What's the reason you can't use ToBigInt already? Sometimes you do want to pass BigInt by value? Otherwise I would just write it as fn foo<T: ToBigInt + ToPrimitive>(x: &T). Another way to support value/ref combinations is Cow<'_, T> for the arg.

@coolreader18
Copy link
Author

Yeah, if I pass BigInt by value I don't want an unnecessary clone.

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.

None yet

2 participants