Skip to content

Commit 89c1bd7

Browse files
author
Rouven Spreckels
committed
Conditionally reimplement try_from feature.
1 parent ff59348 commit 89c1bd7

6 files changed

Lines changed: 83 additions & 48 deletions

File tree

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
[package]
22
name = "signifix"
3-
version = "0.7.0"
3+
version = "0.8.0"
44
authors = [
55
"Rouven Spreckels <n3vu0r@qu1x.org>",
66
]
77
description = """
88
Number Formatter of Fixed Significance with Metric or Binary Prefix"""
9-
documentation = "https://docs.rs/signifix/0.7.0/signifix/"
9+
documentation = "https://docs.rs/signifix"
1010
homepage = "https://qu1x.org/signifix"
1111
repository = "https://github.com/qu1x/signifix"
1212
readme = "README.md"

README.md

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -90,17 +90,19 @@ used by adding `signifix` to the dependencies in your project's
9090

9191
```toml
9292
[dependencies]
93-
signifix = "0.7"
93+
signifix = "0.8"
9494

95-
# Optionally enable `i128_type` support. Requires nightly Rust.
95+
# Optionally enable `try_from` and `i128_type` support on nightly Rust.
9696
#[dependencies.signifix]
9797
#features = ["nightly"]
9898
```
9999

100100
and this to your crate root:
101101

102102
```rust
103-
#![feature(try_from)] // Until stabilized. Requires nightly Rust.
103+
// Optionally enable `try_from` and `i128_type` support on nightly Rust.
104+
// Required if the `nightly` feature is enabled in your `Cargo.toml`.
105+
//#![feature(try_from, i128_type)]
104106

105107
extern crate signifix;
106108
```
@@ -113,7 +115,7 @@ The Signifix notations result in a fixed number of characters preventing
113115
jumps to the left or right while making maximum use of their occupied space:
114116

115117
```rust
116-
use std::convert::TryFrom; // Until stabilized.
118+
use signifix::TryFrom; // Until stabilized.
117119

118120
use signifix::{metric, binary, Result};
119121

@@ -168,7 +170,7 @@ assert_eq!(binary(1_023.499_999_999_999_95),
168170
This is useful to smoothly refresh a transfer rate within a terminal:
169171

170172
```rust
171-
use std::convert::TryFrom; // Until stabilized.
173+
use signifix::TryFrom; // Until stabilized.
172174

173175
use std::f64;
174176
use std::time::Duration;
@@ -218,7 +220,7 @@ Or to monitor a measured quantity like an electrical current including its
218220
direction with positive numbers being padded to align with negative ones:
219221

220222
```rust
221-
use std::convert::TryFrom; // Until stabilized.
223+
use signifix::TryFrom; // Until stabilized.
222224

223225
use signifix::metric::{Signifix, Result, DEF_MAX_LEN};
224226

@@ -242,7 +244,7 @@ While to visualize a change in file size, a plus sign might be preferred for
242244
positive numbers:
243245

244246
```rust
245-
use std::convert::TryFrom; // Until stabilized.
247+
use signifix::TryFrom; // Until stabilized.
246248

247249
use signifix::metric::{Signifix, Error, Result};
248250

@@ -263,11 +265,11 @@ The binary prefix instead suits well to visualize quantities being multiples
263265
of powers of two, such as memory boundaries due to binary addressing:
264266

265267
```rust
266-
use std::convert::TryFrom; // Until stabilized.
268+
use signifix::TryFrom; // Until stabilized.
267269

268270
use signifix::binary::{Signifix, Error, Result};
269271

270-
let boundary_stat = |used: usize, size: usize| -> Result<String> {
272+
let boundary_stat = |used: u64, size: u64| -> Result<String> {
271273
if used == 0 {
272274
let size = Signifix::try_from(size)?;
273275
return Ok(format!(" 0 B ( 0 %) of {}B", size));
@@ -281,17 +283,17 @@ let boundary_stat = |used: usize, size: usize| -> Result<String> {
281283
Ok(format!("{}B ({}) of {}B", used, p100, size))
282284
};
283285

284-
assert_eq!(boundary_stat(0_000usize.pow(1), 1_024usize.pow(3)),
286+
assert_eq!(boundary_stat(0_000u64.pow(1), 1_024u64.pow(3)),
285287
Ok(" 0 B ( 0 %) of 1.000 GiB".into()));
286-
assert_eq!(boundary_stat(1_024usize.pow(2), 1_024usize.pow(3)),
288+
assert_eq!(boundary_stat(1_024u64.pow(2), 1_024u64.pow(3)),
287289
Ok("1.000 MiB ( < 1 %) of 1.000 GiB".into()));
288-
assert_eq!(boundary_stat(3_292usize.pow(2), 1_024usize.pow(3)),
290+
assert_eq!(boundary_stat(3_292u64.pow(2), 1_024u64.pow(3)),
289291
Ok("10.34 MiB (1.009 %) of 1.000 GiB".into()));
290-
assert_eq!(boundary_stat(8_192usize.pow(2), 1_024usize.pow(3)),
292+
assert_eq!(boundary_stat(8_192u64.pow(2), 1_024u64.pow(3)),
291293
Ok("64.00 MiB (6.250 %) of 1.000 GiB".into()));
292-
assert_eq!(boundary_stat(1_000usize.pow(3), 1_024usize.pow(3)),
294+
assert_eq!(boundary_stat(1_000u64.pow(3), 1_024u64.pow(3)),
293295
Ok("953.7 MiB (93.13 %) of 1.000 GiB".into()));
294-
assert_eq!(boundary_stat(1_024usize.pow(3), 1_024usize.pow(3)),
296+
assert_eq!(boundary_stat(1_024u64.pow(3), 1_024u64.pow(3)),
295297
Ok("1.000 GiB (100.0 %) of 1.000 GiB".into()));
296298
```
297299

@@ -303,7 +305,7 @@ into a locale-sensitive newtype which implements the `Display` trait via the
303305
`Signifix::fmt()` method:
304306

305307
```rust
306-
use std::convert::TryFrom; // Until stabilized.
308+
use signifix::TryFrom; // Until stabilized.
307309

308310
use signifix::binary::{Signifix, Result};
309311

@@ -347,7 +349,7 @@ Customization can be achieved by extracting information from the `Signifix`
347349
type via its methods:
348350

349351
```rust
350-
use std::convert::TryFrom; // Until stabilized.
352+
use signifix::TryFrom; // Until stabilized.
351353

352354
use signifix::metric::{Signifix, Result};
353355

RELEASES.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
# Version 0.8.0 (2017-09-26)
2+
3+
* Compiles on stable Rust by reimplementing the `try_from` feature.
4+
* Added `nightly` feature enabling `try_from` and `i128_type` support. This
5+
replaces the reimplementation of `try_from`.
6+
* Simplified code by using `binary_search_by()`.
7+
18
# Version 0.7.0 (2017-07-10)
29

310
* Added explicit localization support.

src/binary.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//
77
// DISCLAIMER: THE WORKS ARE WITHOUT WARRANTY.
88

9-
use std::convert::TryFrom;
9+
use super::TryFrom;
1010

1111
use std::result;
1212
use std::error;

src/lib.rs

Lines changed: 53 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -82,17 +82,19 @@
8282
//!
8383
//! ```toml
8484
//! [dependencies]
85-
//! signifix = "0.7"
85+
//! signifix = "0.8"
8686
//!
87-
//! # Optionally enable `i128_type` support. Requires nightly Rust.
87+
//! # Optionally enable `try_from` and `i128_type` support on nightly Rust.
8888
//! #[dependencies.signifix]
8989
//! #features = ["nightly"]
9090
//! ```
9191
//!
9292
//! and this to your crate root:
9393
//!
9494
//! ```
95-
//! #![feature(try_from)] // Until stabilized. Requires nightly Rust.
95+
//! // Optionally enable `try_from` and `i128_type` support on nightly Rust.
96+
//! // Required if the `nightly` feature is enabled in your `Cargo.toml`.
97+
//! //#![feature(try_from, i128_type)]
9698
//!
9799
//! extern crate signifix;
98100
//! ```
@@ -105,8 +107,8 @@
105107
//! jumps to the left or right while making maximum use of their occupied space:
106108
//!
107109
//! ```
108-
//! # #![feature(try_from)]
109-
//! use std::convert::TryFrom; // Until stabilized.
110+
//! # #![cfg_attr(feature = "nightly", feature(try_from, i128_type))]
111+
//! use signifix::TryFrom; // Until stabilized.
110112
//!
111113
//! use signifix::{metric, binary, Result};
112114
//!
@@ -161,8 +163,8 @@
161163
//! This is useful to smoothly refresh a transfer rate within a terminal:
162164
//!
163165
//! ```
164-
//! # #![feature(try_from)]
165-
//! use std::convert::TryFrom; // Until stabilized.
166+
//! # #![cfg_attr(feature = "nightly", feature(try_from, i128_type))]
167+
//! use signifix::TryFrom; // Until stabilized.
166168
//!
167169
//! use std::f64;
168170
//! use std::time::Duration;
@@ -212,8 +214,8 @@
212214
//! direction with positive numbers being padded to align with negative ones:
213215
//!
214216
//! ```
215-
//! # #![feature(try_from)]
216-
//! use std::convert::TryFrom; // Until stabilized.
217+
//! # #![cfg_attr(feature = "nightly", feature(try_from, i128_type))]
218+
//! use signifix::TryFrom; // Until stabilized.
217219
//!
218220
//! use signifix::metric::{Signifix, Result, DEF_MAX_LEN};
219221
//!
@@ -237,8 +239,8 @@
237239
//! positive numbers:
238240
//!
239241
//! ```
240-
//! # #![feature(try_from)]
241-
//! use std::convert::TryFrom; // Until stabilized.
242+
//! # #![cfg_attr(feature = "nightly", feature(try_from, i128_type))]
243+
//! use signifix::TryFrom; // Until stabilized.
242244
//!
243245
//! use signifix::metric::{Signifix, Error, Result};
244246
//!
@@ -259,8 +261,8 @@
259261
//! of powers of two, such as memory boundaries due to binary addressing:
260262
//!
261263
//! ```
262-
//! # #![feature(try_from)]
263-
//! use std::convert::TryFrom; // Until stabilized.
264+
//! # #![cfg_attr(feature = "nightly", feature(try_from, i128_type))]
265+
//! use signifix::TryFrom; // Until stabilized.
264266
//!
265267
//! use signifix::binary::{Signifix, Error, Result};
266268
//!
@@ -300,8 +302,8 @@
300302
//! `Signifix::fmt()` method:
301303
//!
302304
//! ```
303-
//! # #![feature(try_from)]
304-
//! use std::convert::TryFrom; // Until stabilized.
305+
//! # #![cfg_attr(feature = "nightly", feature(try_from, i128_type))]
306+
//! use signifix::TryFrom; // Until stabilized.
305307
//!
306308
//! use signifix::binary::{Signifix, Result};
307309
//!
@@ -345,8 +347,8 @@
345347
//! type via its methods:
346348
//!
347349
//! ```
348-
//! # #![feature(try_from)]
349-
//! use std::convert::TryFrom; // Until stabilized.
350+
//! # #![cfg_attr(feature = "nightly", feature(try_from, i128_type))]
351+
//! use signifix::TryFrom; // Until stabilized.
350352
//!
351353
//! use signifix::metric::{Signifix, Result};
352354
//!
@@ -388,9 +390,7 @@
388390
389391
#![deny(missing_docs)]
390392

391-
#![cfg_attr(feature = "nightly", feature(i128_type))]
392-
393-
#![feature(try_from)]
393+
#![cfg_attr(feature = "nightly", feature(try_from, i128_type))]
394394

395395
use std::result;
396396
use std::error;
@@ -400,6 +400,39 @@ use std::fmt::{Display, Formatter};
400400

401401
use std::cmp::Ordering;
402402

403+
#[cfg(feature = "nightly")]
404+
pub use std::convert::{TryInto, TryFrom};
405+
406+
/// An attempted conversion that consumes `self`, which may or may not be
407+
/// expensive.
408+
#[cfg(not(feature = "nightly"))]
409+
pub trait TryInto<T>: Sized {
410+
/// The type returned in the event of a conversion error.
411+
type Error;
412+
413+
/// Performs the conversion.
414+
fn try_into(self) -> result::Result<T, Self::Error>;
415+
}
416+
417+
/// Attempt to construct `Self` via a conversion.
418+
#[cfg(not(feature = "nightly"))]
419+
pub trait TryFrom<T>: Sized {
420+
/// The type returned in the event of a conversion error.
421+
type Error;
422+
423+
/// Performs the conversion.
424+
fn try_from(value: T) -> result::Result<Self, Self::Error>;
425+
}
426+
427+
#[cfg(not(feature = "nightly"))]
428+
impl<T, U> TryInto<U> for T where U: TryFrom<T> {
429+
type Error = U::Error;
430+
431+
fn try_into(self) -> result::Result<U, U::Error> {
432+
U::try_from(self)
433+
}
434+
}
435+
403436
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
404437
struct Signifix {
405438
numerator: i16,
@@ -425,33 +458,26 @@ impl Signifix {
425458
fn significand(&self) -> f64 {
426459
self.numerator() as f64 * [1E-00, 1E-01, 1E-02, 1E-03][self.exponent()]
427460
}
428-
429461
fn numerator(&self) -> i32 {
430462
self.numerator.into()
431463
}
432-
433464
fn denominator(&self) -> i32 {
434465
[1, 10, 100, 1_000][self.exponent()]
435466
}
436-
437467
fn exponent(&self) -> usize {
438468
self.exponent.into()
439469
}
440-
441470
fn integer(&self) -> i32 {
442471
self.numerator() / self.denominator()
443472
}
444-
445473
fn fractional(&self) -> i32 {
446474
self.numerator().abs() % self.denominator()
447475
}
448-
449476
fn parts(&self) -> (i32, i32) {
450477
let trunc = self.numerator() / self.denominator();
451478
let fract = self.numerator() - self.denominator() * trunc;
452479
(trunc, fract.abs())
453480
}
454-
455481
fn prefix(&self) -> usize {
456482
self.prefix.into()
457483
}

src/metric.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//
77
// DISCLAIMER: THE WORKS ARE WITHOUT WARRANTY.
88

9-
use std::convert::TryFrom;
9+
use super::TryFrom;
1010

1111
use std::result;
1212
use std::error;

0 commit comments

Comments
 (0)