Struct ckey::CKey

source ·
pub struct CKey { /* private fields */ }
Expand description

CKey implements a consistent hash key.

Theory here: https://en.wikipedia.org/wiki/Consistent_hashing

§Examples

use ckey::CKey;

let k1 = CKey::from(0.1);
let k2 = k1.next();
let k3 = k2 + 10u8;
assert!(k2.inside(k1, k3));
let k4 = CKey::from(1000u16);
assert_eq!("0.015258789", format!("{}", k4));

Implementations§

source§

impl CKey

source

pub fn digest(data: impl AsRef<[u8]>) -> Self

Make a digest from bytes and returns a new key from it.

Typical usage is to create a a CKey from a string.

§Examples
use ckey::CKey;

let k = CKey::digest("/a/unique/path");
assert_eq!("3b818742b0f27cfc10f4de1e5ba5594c975d580c412a31011ad58d36a2b17cdc", format!("{:?}",k));
source

pub fn serial<T>(v: T) -> Self
where T: Serialize,

Make a hash from serialized data and returns a new key from it.

Typical usage is to create a a CKey from some object content.

Note: this is not using the Hash trait, and instead serializes the complete data. You could achieve “build a CKey from a hashable object” easily by doing a digest on the hash. This is not recommended, as you could get many more collisions, and it defeats the purpose of having 256-bit keys.

§Examples
use ckey::CKey;
use serde::Serialize;

#[derive(Serialize)]
struct Obj {
    a: usize,
    b: usize,
}

let obj = Obj{a: 10, b: 100};
let k = CKey::serial(obj);
assert_eq!("fc6326e9af50c0ae08d34921f61afa012988998fac0269dbcf2dbe30748ed4eb", format!("{:?}",k));
// DONT'DO THIS
use ckey::CKey;
use std::collections::hash_map::DefaultHasher;
use bincode::serialize;
use std::hash::{Hash, Hasher};

#[derive(Hash)]
struct Obj {
    a: usize,
    b: usize,
}

let obj = Obj{a: 10, b: 100};
let mut s = DefaultHasher::new();
obj.hash(&mut s);
let h = s.finish();
let data = serialize(&h).unwrap();
let k = CKey::digest(data);
// The code above works, but has 2 caveats:
// 1 - since DefaultHasher is randomized, results are unpredictable;
// 2 - risks of collision, as we are only using 64 bits.
println!("{}", k);
source

pub fn rand() -> Self

Generate a random key.

This can be time consuming as it is using the OS random generation, which is better from a cryptographic point of view, but possibly slower than a standard prng.

§Examples
use ckey::CKey;

let k = CKey::rand();
print!("k: {:?}", k);
source

pub fn zero() -> Self

Generate a key with the value 0.

§Examples
use ckey::CKey;

let k = CKey::zero();
assert_eq!("0000000000000000000000000000000000000000000000000000000000000000", format!("{:?}", k));
source

pub fn unit() -> Self

Generate a key with the value 1, the smallest key possible just after zero.

§Examples
use ckey::CKey;

let k = CKey::unit();
assert_eq!("0000000000000000000000000000000000000000000000000000000000000001", format!("{:?}", k));
source

pub fn last() -> Self

Generate a key with the last, highest possible value.

§Examples
use ckey::CKey;

let k = CKey::last();
assert_eq!("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff", format!("{:?}", k));
source

pub fn halfway() -> Self

Generate a key which is exactly in the middle of the ring.

§Examples
use ckey::CKey;

let k = CKey::halfway();
assert_eq!("8000000000000000000000000000000000000000000000000000000000000000", format!("{:?}", k));
source

pub fn inside(self, lower: CKey, upper: CKey) -> bool

Returns true if self is inside lower and upper. Lower limit is excluded, upper is included. This is typically used in a ring to know if a given key is handled by a node. You would ask if key is inside previous node (lower) and this node (upper) and if true -> yes, this is the right node to handle it.

§Examples
use ckey::CKey;

let k1 = CKey::from(0.1);
let k2 = CKey::from(0.3);
let k3 = CKey::from(0.9);
assert!(k2.inside(k1, k3));
source

pub fn incr(&mut self)

Increment current key by one.

§Examples
use ckey::CKey;

let mut k = CKey::zero();
k.incr();
assert_eq!(CKey::unit(), k);
source

pub fn decr(&mut self)

Decrement current key by one.

§Examples
use ckey::CKey;

let mut k = CKey::zero();
k.decr();
assert_eq!(CKey::last(), k);
source

pub fn next(self) -> CKey

Return current key plus one.

§Examples
use ckey::CKey;

let k = CKey::zero();
assert_eq!(CKey::unit(), k.next());
source

pub fn prev(self) -> CKey

Return current key minus one.

§Examples
use ckey::CKey;

let k = CKey::zero();
assert_eq!(CKey::last(), k.prev());
source

pub fn parse(value: &str) -> Result<CKey, <Self as TryFrom<&str>>::Error>

Try to convert from a &str.

§Examples
use ckey::CKey;

let k = CKey::parse("12345678ffffffff01234567fffffffff00123456ffffffff0012345ffffffff").unwrap();
assert_eq!("12345678ffffffff01234567fffffffff00123456ffffffff0012345ffffffff", format!("{:?}", k));
source

pub fn set(data: impl AsRef<[u8]>) -> Self

Set value from bytes, big endian.

The data can be longer or shorter than 32 bytes. Extra bytes will be ignored. Missing bytes will be replaced by zeroes.

§Examples
use ckey::CKey;
let bytes: [u8; 3] = [4, 2, 1];
let k = CKey::set(bytes);
assert_eq!("0402010000000000000000000000000000000000000000000000000000000000", format!("{:?}", k));
source

pub fn bytes(&self) -> Vec<u8>

Get value as bytes, big endian.

§Examples
use ckey::CKey;

let k = CKey::from(42u8);
let bytes = k.bytes();
assert_eq!(32, bytes.len());
assert_eq!(42, bytes[0]);
for byte in &bytes[1..] {
    assert_eq!(0, *byte);
}
source

pub fn sort(v: &mut Vec<CKey>, start: CKey)

Sort an array of keys, using a start reference.

There is no way to sort keys in an absolute way, since they are virtually on a circle and wrapping, everything depends on where you start from.

This is why there’s a start parameter.

§Examples
use ckey::CKey;

let start = CKey::from(0.7);
let k1 = CKey::from(0.2);
let k2 = CKey::from(0.4);
let k3 = CKey::from(0.3);
let k4 = CKey::from(0.9);
let mut sorted = vec![k1, k2, k3, k4];
CKey::sort(&mut sorted, start);
assert_eq!(4, sorted.len());
assert_eq!("0.900000000", format!("{}", sorted[0]));
assert_eq!("0.200000000", format!("{}", sorted[1]));
assert_eq!("0.300000000", format!("{}", sorted[2]));
assert_eq!("0.400000000", format!("{}", sorted[3]));

Trait Implementations§

source§

impl Add<f32> for CKey

source§

fn add(self, delta: f32) -> CKey

Add an f32.

The value is first converted to a key, considering key space goes from 0 to 1.

§Examples
use ckey::CKey;

let k = CKey::from(0.3f32);
// due to rounding errors, not exactly 0.7
assert_eq!("0.700000018", String::from(k + 0.4f32));
§

type Output = CKey

The resulting type after applying the + operator.
source§

impl Add<f64> for CKey

source§

fn add(self, delta: f64) -> CKey

Add an f64.

The value is first converted to a key, considering key space goes from 0 to 1.

§Examples
use ckey::CKey;

let k = CKey::from(0.3f64);
assert_eq!("0.700000000", String::from(k + 0.4f64));
§

type Output = CKey

The resulting type after applying the + operator.
source§

impl Add<i16> for CKey

source§

fn add(self, delta: i16) -> CKey

Add a i16.

The value is first converted to a key, considering key space goes from 0 to 2^16.

§Examples
use ckey::CKey;

let k = CKey::from(0.1f64);
assert_eq!("0.252587891", String::from(k + 10_000i16))
§

type Output = CKey

The resulting type after applying the + operator.
source§

impl Add<i32> for CKey

source§

fn add(self, delta: i32) -> CKey

Add a i32.

The value is first converted to a key, considering key space goes from 0 to 2^32.

§Examples
use ckey::CKey;

let k = CKey::from(0.1f64);
assert_eq!("0.100232831", String::from(k + 1_000_000i32))
§

type Output = CKey

The resulting type after applying the + operator.
source§

impl Add<i64> for CKey

source§

fn add(self, delta: i64) -> CKey

Add a i64.

The value is first converted to a key, considering key space goes from 0 to 2^64.

§Examples
use ckey::CKey;

let k = CKey::from(0.1f64);
assert_eq!("0.100542101", String::from(k + 10_000_000_000_000_000i64))
§

type Output = CKey

The resulting type after applying the + operator.
source§

impl Add<i8> for CKey

source§

fn add(self, delta: i8) -> CKey

Add a i8.

The value is first converted to a key, considering key space goes from 0 to 2^8.

§Examples
use ckey::CKey;

let k = CKey::from(0.1f64);
assert_eq!("0.490625000", String::from(k + 100i8))
§

type Output = CKey

The resulting type after applying the + operator.
source§

impl Add<u16> for CKey

source§

fn add(self, delta: u16) -> CKey

Add a u16.

The value is first converted to a key, considering key space goes from 0 to 2^16.

§Examples
use ckey::CKey;

let k = CKey::from(0.1f64);
assert_eq!("0.252587891", String::from(k + 10_000u16))
§

type Output = CKey

The resulting type after applying the + operator.
source§

impl Add<u32> for CKey

source§

fn add(self, delta: u32) -> CKey

Add a u32.

The value is first converted to a key, considering key space goes from 0 to 2^32.

§Examples
use ckey::CKey;

let k = CKey::from(0.1f64);
assert_eq!("0.100232831", String::from(k + 1_000_000u32))
§

type Output = CKey

The resulting type after applying the + operator.
source§

impl Add<u64> for CKey

source§

fn add(self, delta: u64) -> CKey

Add a u64.

The value is first converted to a key, considering key space goes from 0 to 2^64.

§Examples
use ckey::CKey;

let k = CKey::from(0.1f64);
assert_eq!("0.100542101", String::from(k + 10_000_000_000_000_000u64))
§

type Output = CKey

The resulting type after applying the + operator.
source§

impl Add<u8> for CKey

source§

fn add(self, delta: u8) -> CKey

Add a u8.

The value is first converted to a key, considering key space goes from 0 to 2^8.

§Examples
use ckey::CKey;

let k = CKey::from(0.1f64);
assert_eq!("0.490625000", String::from(k + 100u8))
§

type Output = CKey

The resulting type after applying the + operator.
source§

impl Add for CKey

source§

fn add(self, other: CKey) -> CKey

Add another key.

If the result is outside key space, will loop back, as in wrapping mode. So the result is always within key space.

§Examples
use ckey::CKey;

let k1 = CKey::from(0.6f64);
let k2 = CKey::from(0.8f64);
let k_sum = k1 + k2;
assert_eq!("0.400000000", String::from(k_sum));
§

type Output = CKey

The resulting type after applying the + operator.
source§

impl Clone for CKey

source§

fn clone(&self) -> CKey

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for CKey

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Debug-print a key.

The representation we use is an hex dump, the way you would write down the 2^256 integer corresponding to the key.

§Examples
use ckey::CKey;
use std::convert::TryFrom;

let k = CKey::parse("12345678ffffffff01234567fffffffff00123456ffffffff0012345ffffffff").unwrap();
assert_eq!("12345678ffffffff01234567fffffffff00123456ffffffff0012345ffffffff", format!("{:?}", k));
source§

impl Default for CKey

source§

fn default() -> CKey

Returns the “default value” for a type. Read more
source§

impl<'de> Deserialize<'de> for CKey

source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl Display for CKey

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Pretty-print a key.

The representation we use is a 9-digits decimal float number, 11 chars in total, including a leading “0.”.

The reason for this is -> 1 billion keys is enough to tell them apart, there CAN be collisions, but it should remain rare. To check for equality, use == operator, do not compare string reprs.

At the same time, decimals between 0 and 1 are easier to reason about when debugging, much more than 64 chars hexa dumps.

§Examples
use ckey::CKey;
use std::convert::TryFrom;

let k = CKey::parse("12345678ffffffff01234567fffffffff00123456ffffffff0012345ffffffff").unwrap();
assert_eq!("0.071111111", format!("{}", k));
source§

impl From<&CKey> for Vec<u8>

source§

fn from(k: &CKey) -> Vec<u8>

Convert to a vector.

Gives a big-endian representation of the key. 32 bytes.

§Examples
use ckey::CKey;

let k = CKey::from(10u8);
let vec: Vec<u8> = Vec::from(&k);
assert_eq!(32, vec.len());
assert_eq!(10, vec[0]);
source§

impl From<&Vec<u8>> for CKey

source§

fn from(v: &Vec<u8>) -> CKey

Convert from a vector of bytes.

The vector can be any length, extra bytes are ignored, missing bytes are replaced with zeros.

§Examples
use ckey::CKey;

let k = CKey::from(&vec![4, 5, 6, 7]);
assert_eq!("0405060700000000000000000000000000000000000000000000000000000000", format!("{:?}", k));
source§

impl From<CKey> for String

source§

fn from(k: CKey) -> String

Convert to a String.

The representation it gives is the fmt::Display impl, which gives a short (not complete/unique) yet readable representation of the key, as a floating point from 0 to 1.

§Examples
use ckey::CKey;

let k = CKey::from(0.6 as f64);
assert_eq!("0.600000000", String::from(k));
source§

impl From<CKey> for Vec<u8>

source§

fn from(k: CKey) -> Vec<u8>

Convert to a vector.

Gives a big-endian representation of the key. 32 bytes.

§Examples
use ckey::CKey;

let k = CKey::from(10u8);
let vec: Vec<u8> = Vec::from(k);
assert_eq!(32, vec.len());
assert_eq!(10, vec[0]);
source§

impl From<CKey> for f32

source§

fn from(k: CKey) -> f32

Convert to an f32, considering the ring goes from 0 to 1.

§Examples
use ckey::CKey;

let k = CKey::parse("123456ffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").unwrap();
assert_eq!("0.071111143", format!("{:0.9}", f32::from(k)));
source§

impl From<CKey> for f64

source§

fn from(k: CKey) -> f64

Convert to an f64, considering the ring goes from 0 to 1.

§Examples
use ckey::CKey;

let k = CKey::parse("123456ffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").unwrap();
assert_eq!("0.071111143", format!("{:0.9}", f64::from(k)));
source§

impl From<CKey> for i16

source§

fn from(k: CKey) -> i16

Convert to a i16, considering the ring goes from 0 to 2^16.

§Examples
use ckey::CKey;

let k = CKey::parse("987654ffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").unwrap();
assert_eq!(-26506, i16::from(k));
source§

impl From<CKey> for i32

source§

fn from(k: CKey) -> i32

Convert to a i32, considering the ring goes from 0 to 2^32.

§Examples
use ckey::CKey;

let k = CKey::parse("987654ffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").unwrap();
assert_eq!(-1737075457, i32::from(k));
source§

impl From<CKey> for i64

source§

fn from(k: CKey) -> i64

Convert to a i64, considering the ring goes from 0 to 2^64.

§Examples
use ckey::CKey;

let k = CKey::parse("987654ffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").unwrap();
assert_eq!(-7460682274204286977, i64::from(k));
source§

impl From<CKey> for i8

source§

fn from(k: CKey) -> i8

Convert to a i8, considering the ring goes from 0 to 2^8.

§Examples
use ckey::CKey;

let k = CKey::parse("987654ffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").unwrap();
assert_eq!(-104, i8::from(k));
source§

impl From<CKey> for u16

source§

fn from(k: CKey) -> u16

Convert to a u16, considering the ring goes from 0 to 2^16.

§Examples
use ckey::CKey;

let k = CKey::parse("123456ffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").unwrap();
assert_eq!(4660, u16::from(k));
source§

impl From<CKey> for u32

source§

fn from(k: CKey) -> u32

Convert to a u32, considering the ring goes from 0 to 2^16.

§Examples
use ckey::CKey;

let k = CKey::parse("123456ffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").unwrap();
assert_eq!(305420031, u32::from(k));
source§

impl From<CKey> for u64

source§

fn from(k: CKey) -> u64

Convert to a u64, considering the ring goes from 0 to 2^64.

§Examples
use ckey::CKey;

let k = CKey::parse("123456ffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").unwrap();
assert_eq!(1311769048983273471, u64::from(k));
source§

impl From<CKey> for u8

source§

fn from(k: CKey) -> u8

Convert to a u8, considering the ring goes from 0 to 2^8.

§Examples
use ckey::CKey;

let k = CKey::parse("123456ffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").unwrap();
assert_eq!(18, u8::from(k));
source§

impl From<Vec<u8>> for CKey

source§

fn from(v: Vec<u8>) -> CKey

Convert from a vector of bytes.

The vector can be any length, extra bytes are ignored, missing bytes are replaced with zeros.

§Examples
use ckey::CKey;

let k = CKey::from(vec![1, 2, 3]);
assert_eq!("0102030000000000000000000000000000000000000000000000000000000000", format!("{:?}", k));
source§

impl From<f32> for CKey

source§

fn from(f: f32) -> CKey

Convert from an f32, considering the ring goes from 0 to 1.

§Examples
use ckey::CKey;

let k = CKey::from(0.5f32);
assert_eq!("0.500000000", format!("{}", k));
source§

impl From<f64> for CKey

source§

fn from(f: f64) -> CKey

Convert from an f64, considering the ring goes from 0 to 1.

§Examples
use ckey::CKey;

let k = CKey::from(0.5f64);
assert_eq!("0.500000000", format!("{}", k));
source§

impl From<i16> for CKey

source§

fn from(i: i16) -> CKey

Convert from a i16, considering the ring goes from 0 to 2^16.

§Examples
use ckey::CKey;

let k = CKey::from(-16384i16);
assert_eq!("0.750000000", format!("{}", k));
source§

impl From<i32> for CKey

source§

fn from(i: i32) -> CKey

Convert from a i32, considering the ring goes from 0 to 2^32.

§Examples
use ckey::CKey;

let k = CKey::from(-1073741824i32);
assert_eq!("0.750000000", format!("{}", k));
source§

impl From<i64> for CKey

source§

fn from(i: i64) -> CKey

Convert from a i64, considering the ring goes from 0 to 2^64.

§Examples
use ckey::CKey;

let k = CKey::from(-1000000000000000000i64);
assert_eq!("0.945789891", format!("{}", k));
source§

impl From<i8> for CKey

source§

fn from(i: i8) -> CKey

Convert from a i8, considering the ring goes from 0 to 2^8.

§Examples
use ckey::CKey;

let k = CKey::from(-64i8);
assert_eq!("0.750000000", format!("{}", k));
source§

impl From<u16> for CKey

source§

fn from(u: u16) -> CKey

Convert from a u16, considering the ring goes from 0 to 2^16.

§Examples
use ckey::CKey;

let k = CKey::from(16384u16);
assert_eq!("0.250000000", format!("{}", k));
source§

impl From<u32> for CKey

source§

fn from(u: u32) -> CKey

Convert from a u32, considering the ring goes from 0 to 2^32.

§Examples
use ckey::CKey;

let k = CKey::from(1073741824u32);
assert_eq!("0.250000000", format!("{}", k));
source§

impl From<u64> for CKey

source§

fn from(u: u64) -> CKey

Convert from a u64, considering the ring goes from 0 to 2^64.

§Examples
use ckey::CKey;

let k = CKey::from(1000000000000000000u64);
assert_eq!("0.054210109", format!("{}", k));
source§

impl From<u8> for CKey

source§

fn from(u: u8) -> CKey

Convert from a u8, considering the ring goes from 0 to 2^8.

§Examples
use ckey::CKey;

let k = CKey::from(64u8);
assert_eq!("0.250000000", format!("{}", k));
source§

impl Hash for CKey

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl PartialEq for CKey

source§

fn eq(&self, other: &CKey) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Serialize for CKey

source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl Sub<f32> for CKey

source§

fn sub(self, delta: f32) -> CKey

Sub an f32.

The value is first converted to a key, considering key space goes from 0 to 1.

§Examples
use ckey::CKey;

let k = CKey::from(0.3f32);
// due to rounding errors, not exactly 0.9
assert_eq!("0.900000006", String::from(k - 0.4f32));
§

type Output = CKey

The resulting type after applying the - operator.
source§

impl Sub<f64> for CKey

source§

fn sub(self, delta: f64) -> CKey

Sub an f64.

The value is first converted to a key, considering key space goes from 0 to 1.

§Examples
use ckey::CKey;

let k = CKey::from(0.3f64);
assert_eq!("0.900000000", String::from(k - 0.4f64));
§

type Output = CKey

The resulting type after applying the - operator.
source§

impl Sub<i16> for CKey

source§

fn sub(self, delta: i16) -> CKey

Sub a i16.

The value is first converted to a key, considering key space goes from 0 to 2^16.

§Examples
use ckey::CKey;

let k = CKey::from(0.1f64);
assert_eq!("0.947412109", String::from(k - 10_000i16))
§

type Output = CKey

The resulting type after applying the - operator.
source§

impl Sub<i32> for CKey

source§

fn sub(self, delta: i32) -> CKey

Sub a i32.

The value is first converted to a key, considering key space goes from 0 to 2^32.

§Examples
use ckey::CKey;

let k = CKey::from(0.1f64);
assert_eq!("0.099767169", String::from(k - 1_000_000i32))
§

type Output = CKey

The resulting type after applying the - operator.
source§

impl Sub<i64> for CKey

source§

fn sub(self, delta: i64) -> CKey

Sub a i64.

The value is first converted to a key, considering key space goes from 0 to 2^64.

§Examples
use ckey::CKey;

let k = CKey::from(0.1f64);
assert_eq!("0.099457899", String::from(k - 10_000_000_000_000_000i64))
§

type Output = CKey

The resulting type after applying the - operator.
source§

impl Sub<i8> for CKey

source§

fn sub(self, delta: i8) -> CKey

Sub a i8.

The value is first converted to a key, considering key space goes from 0 to 2^8.

§Examples
use ckey::CKey;

let k = CKey::from(0.1f64);
assert_eq!("0.709375000", String::from(k - 100i8))
§

type Output = CKey

The resulting type after applying the - operator.
source§

impl Sub<u16> for CKey

source§

fn sub(self, delta: u16) -> CKey

Sub a u16.

The value is first converted to a key, considering key space goes from 0 to 2^16.

§Examples
use ckey::CKey;

let k = CKey::from(0.1f64);
assert_eq!("0.947412109", String::from(k - 10_000u16))
§

type Output = CKey

The resulting type after applying the - operator.
source§

impl Sub<u32> for CKey

source§

fn sub(self, delta: u32) -> CKey

Sub a u32.

The value is first converted to a key, considering key space goes from 0 to 2^32.

§Examples
use ckey::CKey;

let k = CKey::from(0.1f64);
assert_eq!("0.099767169", String::from(k - 1_000_000u32))
§

type Output = CKey

The resulting type after applying the - operator.
source§

impl Sub<u64> for CKey

source§

fn sub(self, delta: u64) -> CKey

Sub a u64.

The value is first converted to a key, considering key space goes from 0 to 2^64.

§Examples
use ckey::CKey;

let k = CKey::from(0.1f64);
assert_eq!("0.099457899", String::from(k - 10_000_000_000_000_000u64))
§

type Output = CKey

The resulting type after applying the - operator.
source§

impl Sub<u8> for CKey

source§

fn sub(self, delta: u8) -> CKey

Sub a u8.

The value is first converted to a key, considering key space goes from 0 to 2^8.

§Examples
use ckey::CKey;

let k = CKey::from(0.1f64);
assert_eq!("0.709375000", String::from(k - 100u8))
§

type Output = CKey

The resulting type after applying the - operator.
source§

impl Sub for CKey

source§

fn sub(self, other: CKey) -> CKey

Sub another key.

If the result is outside key space, will loop back, as in wrapping mode. So the result is always within key space.

§Examples
use ckey::CKey;

let k1 = CKey::from(0.5f64);
let k2 = CKey::from(0.9f64);
let k_diff = k1 - k2;
assert_eq!("0.600000000", String::from(k_diff));
§

type Output = CKey

The resulting type after applying the - operator.
source§

impl TryFrom<&str> for CKey

source§

fn try_from(value: &str) -> Result<CKey, Self::Error>

Try to convert from a &str.

§Examples
use ckey::CKey;

let k = CKey::try_from("12345678ffffffff01234567fffffffff00123456ffffffff0012345ffffffff").unwrap();
assert_eq!("12345678ffffffff01234567fffffffff00123456ffffffff0012345ffffffff", format!("{:?}", k));
§

type Error = FromHexError

The type returned in the event of a conversion error.
source§

impl TryFrom<String> for CKey

source§

fn try_from(value: String) -> Result<CKey, Self::Error>

Try to convert from a String.

§Examples
use ckey::CKey;

let k = CKey::try_from(String::from("12345678ffffffff01234567fffffffff00123456ffffffff0012345ffffffff")).unwrap();
assert_eq!("12345678ffffffff01234567fffffffff00123456ffffffff0012345ffffffff", format!("{:?}", k));
§

type Error = FromHexError

The type returned in the event of a conversion error.
source§

impl Copy for CKey

source§

impl Eq for CKey

source§

impl StructuralPartialEq for CKey

Auto Trait Implementations§

§

impl Freeze for CKey

§

impl RefUnwindSafe for CKey

§

impl Send for CKey

§

impl Sync for CKey

§

impl Unpin for CKey

§

impl UnwindSafe for CKey

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,