Struct hashmap_settings::Stg

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

type abstraction for types implementing Setting

Types implementing Setting can be turned into a Stg with .stg().

use hashmap_settings::stg::{Setting,Stg};
let bool_stg: Stg = true.stg();

They can be turned back to a specific type with .unstg() or .unstg_panic()

let bool: bool = bool_stg.unstg()?;

Additionally there is the StgTrait that can be implemented for types containing Stg to allow .unstg() and .unstg_panic() to be called on them.

The main example would be Option<&Stg>

use std::collections::HashMap;
use hashmap_settings::stg::{Setting,Stg,StgError,StgTrait};
let bool_stg: Stg = true.stg();
let mut hashmap = HashMap::new();
hashmap.insert("bool",bool_stg);
let bool: bool = hashmap.get("bool").unstg()?;

Implementations§

source§

impl Stg

source

pub fn unstg<S: Setting>(self) -> Result<S, Box<dyn Any>>

turns a Stg into a Result<S, Box<dyn Any>>

´unstg´ is the main and safe way to used to get a concrete type S from Stg

Consider using unstg_panic if it’s guaranteed that we will convert to the right type.

§Example
use hashmap_settings::stg::{Setting,Stg};

let bool_stg: Stg = true.stg();
assert_eq!(bool_stg.unstg::<bool>()?, true);
//we need to use ::<bool> to specify that want to turn bool_stg into a bool
use hashmap_settings::stg::{Setting,Stg};

let bool_stg: Stg = true.stg();
let bool :bool = bool_stg.unstg()?;
// here we don't as we specific the type annotation when we use :bool
assert_eq!(bool, true);
§Errors

This function returns a Err(Box<dyn Any>) if we try to covert to the wrong type.

use hashmap_settings::stg::{Setting,Stg};

let bool_stg: Stg = true.stg();
let number = match bool_stg.unstg::<i32>(){
    Ok(x)   => x, //unreachable!()
    Err(x)  => {
        print!("wrong conversion {:?}",x);
        404
    },
};
assert_eq!(number, 404)
source

pub fn unstg_panic<S: Setting>(self) -> S

turns a Stg into a concrete type S, can panic!

This method is used to get a concrete type out of a Stg when it’s know what S it contains.

§Panics

We need to be careful using unstg_panic as if we try convert to a type that isn’t the one contained in Stg the program will panic. Consider using unstg as it returns a result type instead.

use hashmap_settings::stg::{Setting,Stg};

let bool_stg: Stg = true.stg();
let _number :i32 = bool_stg.unstg_panic();
// this panics, as the Box<dyn Setting> holds a bool value but we are trying to convert it to a i32
§Examples
use hashmap_settings::stg::{Setting,Stg};

let bool_stg: Stg = true.stg();
assert_eq!(bool_stg.unstg_panic::<bool>(), true);
//we need to use ::<bool> to specify that want to turn bool_stg into a bool
use hashmap_settings::stg::{Setting,Stg};

let bool_stg: Stg = true.stg();
let bool :bool = bool_stg.unstg_panic();
// here we don't as we specific the type annotation when we use :bool
assert_eq!(bool, true);

Trait Implementations§

source§

impl Clone for Stg

source§

fn clone(&self) -> Stg

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 Stg

source§

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

Formats the value using the given formatter. Read more
source§

impl PartialEq for Stg

source§

fn eq(&self, other: &Self) -> 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 Setting for Stg

source§

fn stg(self) -> Stg
where Self: Setting + Sized,

turns a type implementing Setting into a Stg Read more

Auto Trait Implementations§

§

impl !RefUnwindSafe for Stg

§

impl !Send for Stg

§

impl !Sync for Stg

§

impl Unpin for Stg

§

impl !UnwindSafe for Stg

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> DynClone for T
where T: Clone,

source§

fn __clone_box(&self, _: Private) -> *mut ()

source§

impl<T> DynEq for T
where T: Any + PartialEq,

source§

fn as_any(&self) -> &(dyn Any + 'static)

source§

fn dyn_eq(&self, other: &(dyn DynEq + 'static)) -> bool

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> 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, 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.