Skip to content

Commit 1cf0404

Browse files
committed
Fix ToStrRadix and str.to_owned() deprecations
Sadly there doesn't seem to be a replacement for ToStrRadix on user types at the moment. See: rust-lang/rust#18301
1 parent e792960 commit 1cf0404

File tree

2 files changed

+27
-28
lines changed

2 files changed

+27
-28
lines changed

src/lib.rs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
extern crate libc;
1111

1212
use libc::{c_char, c_double, c_int, c_long, c_ulong, c_void, size_t};
13-
use std::num::{One, Zero, ToStrRadix};
13+
use std::num::{One, Zero};
1414
use std::mem::{uninitialized,size_of};
1515
use std::{cmp, fmt};
1616
use std::from_str::FromStr;
@@ -181,6 +181,29 @@ impl Mpz {
181181
}
182182
}
183183

184+
// TODO: fail on an invalid base
185+
// FIXME: Unfortunately it isn't currently possible to use the fmt::RadixFmt
186+
// machinery for a custom type.
187+
fn to_str_radix(&self, base: uint) -> String {
188+
unsafe {
189+
// Extra two bytes are for possible minus sign and null terminator
190+
let len = __gmpz_sizeinbase(&self.mpz, base as c_int) as uint + 2;
191+
192+
// Allocate and write into a raw *c_char of the correct length
193+
let mut vector: Vec<u8> = Vec::with_capacity(len);
194+
vector.set_len(len);
195+
196+
let mut cstr = vector.as_slice().to_c_str_unchecked();
197+
198+
__gmpz_get_str(cstr.as_mut_ptr(), base as c_int, &self.mpz);
199+
200+
match cstr.as_str() {
201+
Some(slice) => slice.to_string(),
202+
None => fail!("GMP returned invalid UTF-8!")
203+
}
204+
}
205+
}
206+
184207
pub fn from_str_radix(s: &str, base: uint) -> Option<Mpz> {
185208
unsafe {
186209
assert!(base == 0 || (base >= 2 && base <= 62));
@@ -540,29 +563,6 @@ impl FromStr for Mpz {
540563
}
541564
}
542565

543-
impl ToStrRadix for Mpz {
544-
// TODO: fail on an invalid base
545-
fn to_str_radix(&self, base: uint) -> String {
546-
unsafe {
547-
// Extra two bytes are for possible minus sign and null terminator
548-
let len = __gmpz_sizeinbase(&self.mpz, base as c_int) as uint + 2;
549-
550-
// Allocate and write into a raw *c_char of the correct length
551-
let mut vector: Vec<u8> = Vec::with_capacity(len);
552-
vector.set_len(len);
553-
554-
let mut cstr = vector.as_slice().to_c_str_unchecked();
555-
556-
__gmpz_get_str(cstr.as_mut_ptr(), base as c_int, &self.mpz);
557-
558-
match cstr.as_str() {
559-
Some(slice) => slice.to_string(),
560-
None => fail!("GMP returned invalid UTF-8!")
561-
}
562-
}
563-
}
564-
}
565-
566566
impl fmt::Show for Mpz {
567567
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
568568
write!(f, "{}", self.to_str_radix(10))

src/test.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ mod mpz {
33
use std::from_str::FromStr;
44
use std::num::One;
55
use libc::c_ulong;
6-
use std::num::ToStrRadix;
76

87
#[test]
98
fn test_set() {
@@ -107,13 +106,13 @@ mod mpz {
107106
#[test]
108107
fn test_to_str_radix() {
109108
let x: Mpz = FromPrimitive::from_int(255).unwrap();
110-
assert!(x.to_str_radix(16) == "ff".to_owned());
109+
assert!(x.to_str_radix(16) == "ff".to_string());
111110
}
112111

113112
#[test]
114113
fn test_to_string() {
115114
let x: Mpz = FromStr::from_str("1234567890").unwrap();
116-
assert!(x.to_string() == "1234567890".to_owned());
115+
assert!(x.to_string() == "1234567890".to_string());
117116
}
118117

119118
#[test]
@@ -134,7 +133,7 @@ mod mpz {
134133
#[test]
135134
fn test_from_int() {
136135
let x: Mpz = FromPrimitive::from_int(150).unwrap();
137-
assert!(x.to_string() == "150".to_owned());
136+
assert!(x.to_string() == "150".to_string());
138137
assert!(x == FromStr::from_str("150").unwrap());
139138
}
140139

0 commit comments

Comments
 (0)