base-traits 0.1.3

base traits (for Rust)
Documentation

base-traits

base traits, for Rust

Language License Crates.io GitHub release MSRV CI docs.rs

Introduction

This crate defines general-purpose traits that:

  • seem obvious and yet are missing from the standard library (e.g. IsEmpty, Len); or
  • provide a basis to introduce specific functionality to allow for other crates to interoperate;

Table of Contents

Installation

Reference in Cargo.toml in the usual way:

base-traits = { version = "0" }

Components

Enumerations

None defined at this time.

Features

The following crate features are defined:

Name Effect Is "default"? Is "full"? Dependent feature(s)
experimental-exact_size_is_empty Causes the experimental feature exact_size_is_empty to be enabled No Yes (via implement-IsEmpty-for-standard_process_types)
full Enables the broader set of foreign-type implementations (including process/time IsEmpty and process Len) No Yes (see Cargo.toml)
implement-AsF64-for-built_ins Causes AsF64 to be implemented for built-in type f64 Yes Yes
implement-AsI128-for-built_ins Causes AsI128 to be implemented for built-in type i128 Yes Yes
implement-AsI32-for-built_ins Causes AsI32 to be implemented for built-in type i32 Yes Yes
implement-AsI64-for-built_ins Causes AsI64 to be implemented for built-in type i64 Yes Yes
implement-AsISize-for-built_ins Causes AsISize to be implemented for built-in type isize Yes Yes
implement-AsStr-for-built_ins Causes AsStr to be implemented for built-in type str Yes Yes
implement-AsStr-for-String Causes AsStr to be implemented for standard collection type String Yes Yes (via implement-AsStr-for-standard_collection_types)
implement-AsStr-for-standard_collection_types Causes AsStr to be implemented for standard collection type String No Yes implement-AsStr-for-String
implement-AsU128-for-built_ins Causes AsU128 to be implemented for built-in type u128 Yes Yes
implement-AsU32-for-built_ins Causes AsU32 to be implemented for built-in type u32 Yes Yes
implement-AsU64-for-built_ins Causes AsU64 to be implemented for built-in type u64 Yes Yes
implement-AsUSize-for-built_ins Causes AsUSize to be implemented for built-in type usize Yes Yes
implement-Infinity-for-built_ins Causes Infinity to be implemented for built-in types f32, f64 Yes Yes
implement-IsEmpty-for-built_ins Causes IsEmpty to be implemented for built-in types str, [T; N], [T] Yes Yes
implement-IsEmpty-for-String Causes IsEmpty to be implemented for standard collection type String Yes (via implement-IsEmpty-for-standard_collection_types) Yes (via implement-IsEmpty-for-standard_collection_types)
implement-IsEmpty-for-standard_collection_types Causes IsEmpty to be implemented for standard collection types BTreeMap, BTreeSet, ..., Vec, VecDeque, and String Yes Yes implement-IsEmpty-for-String
implement-IsEmpty-for-standard_ffi_types Causes IsEmpty to be implemented for standard FFI types CStr, CString Yes Yes
implement-IsEmpty-for-standard_path_types Causes IsEmpty to be implemented for standard path types Path, PathBuf Yes Yes
implement-IsEmpty-for-standard_process_types Causes IsEmpty to be implemented for standard process types CommandArgs, CommandEnvs No Yes experimental-exact_size_is_empty
implement-IsEmpty-for-standard_range_types Causes IsEmpty to be implemented for standard range types Range, RangeFrom, etc. Yes Yes
implement-IsEmpty-for-standard_time_types Causes IsEmpty to be implemented for standard time type Duration No Yes
implement-IsInfinity-for-built_ins Causes IsInfinity to be implemented for built-in types f32, f64 Yes Yes
implement-IsNAN-for-built_ins Causes IsNAN to be implemented for built-in types f32, f64 Yes Yes
implement-IsZero-for-built_ins Causes IsZero to be implemented for built-in types i8, ..., i128, u8, ..., u128, isize, usize, f32, f64, char Yes Yes
implement-IsZero-for-standard_num_types Reserved for IsZero implementations on standard numeric wrapper types Yes Yes
implement-IsZero-for-standard_process_types Causes IsZero to be implemented for standard process type ExitStatus Yes Yes
implement-IsZero-for-standard_time_types Causes IsZero to be implemented for standard time type Duration Yes Yes
implement-Len-for-built_ins Causes Len to be implemented for built-in types str, [T; N], [T] Yes Yes
implement-Len-for-String Causes Len to be implemented for standard collection type String Yes (via implement-Len-for-standard_collection_types) Yes (via implement-Len-for-standard_collection_types)
implement-Len-for-standard_collection_types Causes Len to be implemented for standard collection types BTreeMap, BTreeSet, ..., Vec, VecDeque, and String Yes Yes implement-Len-for-String
implement-Len-for-standard_ffi_types Causes Len to be implemented for standard FFI types CStr, CString Yes Yes
implement-Len-for-standard_path_types Causes Len to be implemented for standard path types Path, PathBuf Yes Yes
implement-Len-for-standard_process_types Causes Len to be implemented for standard process types CommandArgs, CommandEnvs No Yes
implement-ToF64-for-built_ins Causes ToF64 to be implemented for built-in types i8, ..., i128, u8, ..., u128, isize, usize, f32, f64 Yes Yes
implement-ToI128-for-built_ins Causes ToI128 to be implemented for built-in type i8, i16, i32, i64, i128, u8, u16, u32, u64 Yes Yes
implement-ToI16-for-built_ins Causes ToI16 to be implemented for built-in types i8, i16, u8 Yes Yes
implement-ToI32-for-built_ins Causes ToI32 to be implemented for built-in type i8, i16, i32, u8, u16 Yes Yes
implement-ToI64-for-built_ins Causes ToI64 to be implemented for built-in type i8, i16, i32, i64, u8, u16, u32 Yes Yes
implement-ToU128-for-built_ins Causes ToU128 to be implemented for built-in type u8, u16, u32, u64, u128 Yes Yes
implement-ToU16-for-built_ins Causes ToU16 to be implemented for built-in type u8, u16 Yes Yes
implement-ToU32-for-built_ins Causes ToU32 to be implemented for built-in type u8, u16, u32 Yes Yes
implement-ToU64-for-built_ins Causes ToU64 to be implemented for built-in type u8, u16, u32, u64 Yes Yes
implement-ToISize-for-built_ins Causes ToISize to be implemented for built-in types isize, i8, and all numeric types that, dependent on architecture, that can be represented in isize without loss Yes Yes
implement-ToUSize-for-built_ins Causes ToUSize to be implemented for built-in types usize, u8, and all unsigned numeric types that, dependent on architecture, that can be represented in usize without loss Yes Yes
implement-Zero-for-built_ins Causes Zero to be implemented for built-in types i8, ..., i128, u8, ..., u128, isize, usize, f32, f64, char Yes Yes

Functions

None defined at this time.

Macros

None defined at this time.

Structures

None defined at this time.

Traits

The following traits are defined:

  • AsF64 - provides (non-mutating) instance method #as_f64() : f64;
  • AsI128 - provides (non-mutating) instance method #as_i128() : i128;
  • AsI32 - provides (non-mutating) instance method #as_i32() : i32;
  • AsI64 - provides (non-mutating) instance method #as_i64() : i64;
  • AsISize - provides (non-mutating) instance method #as_isize() : isize;
  • AsStr - provides (non-mutating) instance method #as_str() : &str;
  • AsU128 - provides (non-mutating) instance method #as_u128() : u128;
  • AsU32 - provides (non-mutating) instance method #as_u32() : u32;
  • AsU64 - provides (non-mutating) instance method #as_u64() : u64;
  • AsUSize - provides (non-mutating) instance method #as_usize() : usize;
  • Infinity - provides method ::infinity() : Self;
  • Integer - provides no methods;
  • IsEmpty - provides (non-mutating) instance method #is_empty() : bool;
  • IsInfinity - provides (non-mutating) instance method #is_infinity() : bool;
  • IsNAN - provides (non-mutating) instance method #is_nan() : bool;
  • IsZero - provides (non-mutating) instance method #is_zero() : bool;
  • Len - provides (non-mutating) instance method #len() : usize;
  • Numeric - provides no methods;
  • Real - provides no methods;
  • Signed - provides no methods;
  • ToF64 - provides (non-mutating) instance method #to_f64() : f64;
  • AsU32 - provides (non-mutating) instance method #as_u32() : u32;
  • AsU64 - provides (non-mutating) instance method #as_u64() : u64;
  • AsU128 - provides (non-mutating) instance method #as_u128() : u128;
  • ToISize - provides (non-mutating) instance method #to_isize() : isize;
  • ToUSize - provides (non-mutating) instance method #to_usize() : usize;
  • Unsigned - provides no methods;
  • Zero - provides class method ::zero() : Self;

Examples

The purpose of all traits provided in this package is to enhance generic programming.

For example, you may be working on a subsystem that needs to abstract numbers into strong types, but you also need to build algorithms that can work generically on such types as well as on basic types. You might, therefore, implement ToF64 on your Price type and then go ahead and build your generic algorithm(s) around ToF64, as in:


use base_traits::ToF64;

/// Trivial implementation of a price type using exact types (integers)
#[derive(Debug)]
struct Price {
    dollars : u32,
    cents : u8,
}

impl ToF64 for Price {
    fn to_f64(&self) -> f64 {
        self.dollars as f64 + (self.cents as f64 / 100.0)
    }
}


/// Trivial implementation for calculation of mean and std-deviation
fn calc_mean_and_stddev<'a, F, I>(
    i : I
) -> Option<(
    f64, // mean
    f64, // stddev
)>
where
    F : ToF64 + 'a,
    I : Iterator<Item = &'a F>,
{
    let values = i.map(|v| v.to_f64()).collect::<Vec::<_>>();

    if values.is_empty() {
        None
    } else {
        let n = values.len() as f64;
        let sum : f64 = values.iter().sum();
        let mean = sum / n;
        let ss : f64 = values.iter().map(|v| (v - mean)).map(|v| v*v).sum();
        let var = ss / n;
        let stddev = var.sqrt();

        Some((mean, stddev))
    }
}


fn main() {
    {
        let raw_values = vec![
            // insert list
            10.0,
            10.1,
            10.2,
            10.3,
        ];

        let (raw_mean, raw_stddev) = calc_mean_and_stddev(raw_values.iter()).unwrap();

        println!("for {raw_values:?}, mean={raw_mean}, std-dev={raw_stddev}");
    }

    {
        let prices = vec![
            // insert list
            Price { dollars: 10, cents : 0 },
            Price { dollars: 10, cents : 1 },
            Price { dollars: 10, cents : 2 },
            Price { dollars: 10, cents : 3 },
        ];

        let (price_mean, price_stddev) = calc_mean_and_stddev(prices.iter()).unwrap();

        println!("for {prices:?}, mean={price_mean}, std-dev={price_stddev}");
    }
}

Project Information

Where to get help

GitHub Page

Contribution guidelines

Defect reports, feature requests, and pull requests are welcome on https://github.com/synesissoftware/base-traits.

Dependencies

Efferent (fan-out)

Libraries upon which base-traits depends:

Runtime Dependencies

There are no runtime dependencies (aka "normal dependencies") on other crates.

Build Dependencies

Libraries used only when building base-traits (not required by downstream consumers):

Development Dependencies

There are no development dependencies on other crates.

Afferent (fan-in)

Projects that depend on base-traits:

Related projects

License

base-traits is released under the 3-clause BSD license. See LICENSE for details.