Converting between extprim::u128::u128 and primitive u128 in Rust?
up vote
1
down vote
favorite
I'm working with an old Rust module that uses the extprim crate to provide a u128 type.
I'm trying to use this with a newer crate that uses Rust's primitive u128 type (available since Rust 1.26).
What's an efficient way to convert back and forth between these two types?
rust
add a comment |
up vote
1
down vote
favorite
I'm working with an old Rust module that uses the extprim crate to provide a u128 type.
I'm trying to use this with a newer crate that uses Rust's primitive u128 type (available since Rust 1.26).
What's an efficient way to convert back and forth between these two types?
rust
2
I would suggest opening an issue or providing a pull request and remove that type in that crate completly ;)
– hellow
Nov 8 at 12:56
2
Maybeas_built_in
? (docs.rs/extprim/1.6.0/src/extprim/u128.rs.html#178)
– phimuemue
Nov 8 at 13:02
@phimuemue that looks like a great option - I didn't find it in the public docs, I guess because it's only conditionally available?
– Dave Challis
Nov 8 at 13:31
1
@phimuemue look at my answer, it is even easier than that, thanks for the hint! ;)
– hellow
Nov 8 at 13:42
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I'm working with an old Rust module that uses the extprim crate to provide a u128 type.
I'm trying to use this with a newer crate that uses Rust's primitive u128 type (available since Rust 1.26).
What's an efficient way to convert back and forth between these two types?
rust
I'm working with an old Rust module that uses the extprim crate to provide a u128 type.
I'm trying to use this with a newer crate that uses Rust's primitive u128 type (available since Rust 1.26).
What's an efficient way to convert back and forth between these two types?
rust
rust
edited Nov 8 at 13:50
asked Nov 8 at 12:42
Dave Challis
1,4401336
1,4401336
2
I would suggest opening an issue or providing a pull request and remove that type in that crate completly ;)
– hellow
Nov 8 at 12:56
2
Maybeas_built_in
? (docs.rs/extprim/1.6.0/src/extprim/u128.rs.html#178)
– phimuemue
Nov 8 at 13:02
@phimuemue that looks like a great option - I didn't find it in the public docs, I guess because it's only conditionally available?
– Dave Challis
Nov 8 at 13:31
1
@phimuemue look at my answer, it is even easier than that, thanks for the hint! ;)
– hellow
Nov 8 at 13:42
add a comment |
2
I would suggest opening an issue or providing a pull request and remove that type in that crate completly ;)
– hellow
Nov 8 at 12:56
2
Maybeas_built_in
? (docs.rs/extprim/1.6.0/src/extprim/u128.rs.html#178)
– phimuemue
Nov 8 at 13:02
@phimuemue that looks like a great option - I didn't find it in the public docs, I guess because it's only conditionally available?
– Dave Challis
Nov 8 at 13:31
1
@phimuemue look at my answer, it is even easier than that, thanks for the hint! ;)
– hellow
Nov 8 at 13:42
2
2
I would suggest opening an issue or providing a pull request and remove that type in that crate completly ;)
– hellow
Nov 8 at 12:56
I would suggest opening an issue or providing a pull request and remove that type in that crate completly ;)
– hellow
Nov 8 at 12:56
2
2
Maybe
as_built_in
? (docs.rs/extprim/1.6.0/src/extprim/u128.rs.html#178)– phimuemue
Nov 8 at 13:02
Maybe
as_built_in
? (docs.rs/extprim/1.6.0/src/extprim/u128.rs.html#178)– phimuemue
Nov 8 at 13:02
@phimuemue that looks like a great option - I didn't find it in the public docs, I guess because it's only conditionally available?
– Dave Challis
Nov 8 at 13:31
@phimuemue that looks like a great option - I didn't find it in the public docs, I guess because it's only conditionally available?
– Dave Challis
Nov 8 at 13:31
1
1
@phimuemue look at my answer, it is even easier than that, thanks for the hint! ;)
– hellow
Nov 8 at 13:42
@phimuemue look at my answer, it is even easier than that, thanks for the hint! ;)
– hellow
Nov 8 at 13:42
add a comment |
1 Answer
1
active
oldest
votes
up vote
3
down vote
accepted
Update:
When your rustc version is greater than 1.26.0 the From trait is implemented and you can use into
respectively from
easily.
For a lower version than that see below.
As a note: "The most efficient way" is very subjective.
I would use the low64()
and high64()
methods to generate a rust u128
.
extern crate extprim; // 1.6.0
use extprim::u128;
fn main() u128::from(number.low64());
println!(":?", number);
assert_eq!(number.to_string(), real_number.to_string());
// and back
let old_number = u128::u128::from_parts((real_number >> 64) as u64, (real_number) as u64);
assert_eq!(number, old_number);
(playground)
Since you can't compare both directly, I used the to_string()
function to convert them to a string and compare those.
1
You could convert theextprim::u128
to a string and then parse au128
out of the string, that would be a way too, but not very efficient :D
– hellow
Nov 8 at 13:32
yup, that's the implementation I wanted to avoid :)
– Dave Challis
Nov 8 at 13:51
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
accepted
Update:
When your rustc version is greater than 1.26.0 the From trait is implemented and you can use into
respectively from
easily.
For a lower version than that see below.
As a note: "The most efficient way" is very subjective.
I would use the low64()
and high64()
methods to generate a rust u128
.
extern crate extprim; // 1.6.0
use extprim::u128;
fn main() u128::from(number.low64());
println!(":?", number);
assert_eq!(number.to_string(), real_number.to_string());
// and back
let old_number = u128::u128::from_parts((real_number >> 64) as u64, (real_number) as u64);
assert_eq!(number, old_number);
(playground)
Since you can't compare both directly, I used the to_string()
function to convert them to a string and compare those.
1
You could convert theextprim::u128
to a string and then parse au128
out of the string, that would be a way too, but not very efficient :D
– hellow
Nov 8 at 13:32
yup, that's the implementation I wanted to avoid :)
– Dave Challis
Nov 8 at 13:51
add a comment |
up vote
3
down vote
accepted
Update:
When your rustc version is greater than 1.26.0 the From trait is implemented and you can use into
respectively from
easily.
For a lower version than that see below.
As a note: "The most efficient way" is very subjective.
I would use the low64()
and high64()
methods to generate a rust u128
.
extern crate extprim; // 1.6.0
use extprim::u128;
fn main() u128::from(number.low64());
println!(":?", number);
assert_eq!(number.to_string(), real_number.to_string());
// and back
let old_number = u128::u128::from_parts((real_number >> 64) as u64, (real_number) as u64);
assert_eq!(number, old_number);
(playground)
Since you can't compare both directly, I used the to_string()
function to convert them to a string and compare those.
1
You could convert theextprim::u128
to a string and then parse au128
out of the string, that would be a way too, but not very efficient :D
– hellow
Nov 8 at 13:32
yup, that's the implementation I wanted to avoid :)
– Dave Challis
Nov 8 at 13:51
add a comment |
up vote
3
down vote
accepted
up vote
3
down vote
accepted
Update:
When your rustc version is greater than 1.26.0 the From trait is implemented and you can use into
respectively from
easily.
For a lower version than that see below.
As a note: "The most efficient way" is very subjective.
I would use the low64()
and high64()
methods to generate a rust u128
.
extern crate extprim; // 1.6.0
use extprim::u128;
fn main() u128::from(number.low64());
println!(":?", number);
assert_eq!(number.to_string(), real_number.to_string());
// and back
let old_number = u128::u128::from_parts((real_number >> 64) as u64, (real_number) as u64);
assert_eq!(number, old_number);
(playground)
Since you can't compare both directly, I used the to_string()
function to convert them to a string and compare those.
Update:
When your rustc version is greater than 1.26.0 the From trait is implemented and you can use into
respectively from
easily.
For a lower version than that see below.
As a note: "The most efficient way" is very subjective.
I would use the low64()
and high64()
methods to generate a rust u128
.
extern crate extprim; // 1.6.0
use extprim::u128;
fn main() u128::from(number.low64());
println!(":?", number);
assert_eq!(number.to_string(), real_number.to_string());
// and back
let old_number = u128::u128::from_parts((real_number >> 64) as u64, (real_number) as u64);
assert_eq!(number, old_number);
(playground)
Since you can't compare both directly, I used the to_string()
function to convert them to a string and compare those.
edited Nov 8 at 13:48
answered Nov 8 at 13:01
hellow
3,69812041
3,69812041
1
You could convert theextprim::u128
to a string and then parse au128
out of the string, that would be a way too, but not very efficient :D
– hellow
Nov 8 at 13:32
yup, that's the implementation I wanted to avoid :)
– Dave Challis
Nov 8 at 13:51
add a comment |
1
You could convert theextprim::u128
to a string and then parse au128
out of the string, that would be a way too, but not very efficient :D
– hellow
Nov 8 at 13:32
yup, that's the implementation I wanted to avoid :)
– Dave Challis
Nov 8 at 13:51
1
1
You could convert the
extprim::u128
to a string and then parse a u128
out of the string, that would be a way too, but not very efficient :D– hellow
Nov 8 at 13:32
You could convert the
extprim::u128
to a string and then parse a u128
out of the string, that would be a way too, but not very efficient :D– hellow
Nov 8 at 13:32
yup, that's the implementation I wanted to avoid :)
– Dave Challis
Nov 8 at 13:51
yup, that's the implementation I wanted to avoid :)
– Dave Challis
Nov 8 at 13:51
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53207980%2fconverting-between-extprimu128u128-and-primitive-u128-in-rust%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
2
I would suggest opening an issue or providing a pull request and remove that type in that crate completly ;)
– hellow
Nov 8 at 12:56
2
Maybe
as_built_in
? (docs.rs/extprim/1.6.0/src/extprim/u128.rs.html#178)– phimuemue
Nov 8 at 13:02
@phimuemue that looks like a great option - I didn't find it in the public docs, I guess because it's only conditionally available?
– Dave Challis
Nov 8 at 13:31
1
@phimuemue look at my answer, it is even easier than that, thanks for the hint! ;)
– hellow
Nov 8 at 13:42