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//! ```
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//!
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;
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//!
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//!
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//!
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//!
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//!
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
395395use std:: result;
396396use std:: error;
@@ -400,6 +400,39 @@ use std::fmt::{Display, Formatter};
400400
401401use 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 ) ]
404437struct 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 }
0 commit comments