[][src]Crate safecoin

Example implementation of safecoin types.

Examples

use safecoin::{Coins, Error, MicroCoins, MilliCoins, NanoCoins};
use std::{convert::TryFrom, str::FromStr};

let c1 = Coins::from_str("1.234").unwrap();
let c2 = Coins::from(MilliCoins::new(1234).unwrap());
assert_eq!(c1, c2);

let c3 = "0.123456789".parse::<Coins>().unwrap();
let c4 = Coins::from(NanoCoins::new(123_456_789).unwrap());
assert_eq!(c3, c4);

let c5 = Coins::from_str("1.2345").unwrap();
let micro = MicroCoins::try_from(c5).unwrap();
assert_eq!(1_234_500, micro.num());

// We can't accurately represent 1.2345 safecoin purely in terms of milli-safecoin
let milli_err = MilliCoins::try_from(c5).unwrap_err();
assert_eq!(Error::LossOfPrecision, milli_err);

Structs

Coins

Structure representing a safecoin amount.

MicroCoins

Structure representing a quantity of micro-safecoins

MilliCoins

Structure representing a quantity of milli-safecoins

NanoCoins

Structure representing a quantity of nano-safecoins

Enums

Error

Safecoin error variants.

Constants

MAX_COINS_VALUE

The maximum amount of safecoin represented by a single Coins.

Type Definitions

Result

A specialised Result type for safecoin.