Generic numeric type

Want use

Strt<K>
where K: num::Numcast + ToPrimitive...{
block: K
....
}

Impl<K,...> Strt<K,...>{
where K: num::Numcast + ToPrimitive...
 fn do_smt(&mut self,... ){
 ........
let k: u32= NumCast::from(new_block);
....}


fn main(...){
let nnn: Strt= Strt<u32...>::new(...)// This doesn't work!
}

Because u32 doesn't impl ToPrimitive, unless u32 doesn't be identic to primitive? How i can express what i want in code(to use k as digit, convert, add 5,6... etc)

It’s a bit hard to tell what it is that you want to code. You don’t explain much and your pseudo-code contains lots of .... You indicate This doesn't work! at one place, I would assume this is because you have written some actual Rust code and this is where you got a compiler error. If this is the case, you also might want to post this (more complete) Rust code here, as well as the compiler error you got.

Excuse me, I wrote from phone.

#[derive(Debug, Clone)]
//This my struct
pub struct GlobalExpiredTime<'ph, T: NumCast, V, P: ?Sized>
where P: 'ph + std::marker::Send,
V: num_traits::ToPrimitive,
T: num_traits::ToPrimitive + num_traits::NumCast {
//First-  time of entry in block (Local or UTC)
//Second- key: (for example simply) number of block (or associated name) 
//        value: time count (on whether it was done in threads or simply run the same block twise, thrice, fourfold etc.)
//(Option- maybe you don't want to take it into account, then counted time on this=0)
    LBlockT: Arc<Mutex<Vec
        <DateTime<Local>>>>, // u32- count times
    IntInfo: HashMap<T, (V, Option<u32>)>, //Int- intilligence (mark it)
    BlLife: PhantomData<&'ph P>}
.................
//For example:
impl<'ph, K, V, P> GlobalExpiredTime<'ph, K, V, P> where
//K must be the number! of block in program counted times.(as example)
//Default- zero, ToPrimitive + std::cmp::Ord- requirement NumCast::from, Send- to use in threads
K: std::default::Default + num_traits::NumCast + num_traits::ToPrimitive +
Copy + Sync + std::cmp::Ord + std::fmt::Debug,//, TimeSpec:
V: Sync +std::fmt::Debug + Clone + Display + num_traits::NumCast + num_traits::ToPrimitive + std::cmp::Ord,
P: std::marker::Send {
    fn new(t_kind: Option<String>) -> Self {
        if let Some(kind_of_time) = t_kind {
            if kind_of_time.to_uppercase()=="UTC" {
                let now: DateTime<Utc> = Utc::now();
                println!("UTC now is: {}, so you had instantiated time in program TBC_eq(TransferBurguerCorrection_eq)", now.to_rfc2822());
            }
            else if kind_of_time.to_uppercase()=="LOCAL" {//How impl |"LOC" ... etc?
                let now: DateTime<Local> = Local::now();
                println!("Local time now is: {}, so you had instantiated time in program TBC_eq(TransferBurguerCorrection_eq)", now.to_rfc2822());
            }
        }
//Now only create fields and push them in structs
        let mut date_vec = Vec::<DateTime<Local>>::new();
        date_vec.push(Local::now());
        let LBlockT= Arc::new(Mutex::new(date_vec));
        let IntInfo= HashMap::new();
            Self{
                LBlockT, //equiv LBlockT: LBlockT
                IntInfo, //because we know only start point- no other info to push
                BlLife: PhantomData
    }
fn main(){
//....
let mut  time: super::GlobalExpiredTime<u32, (i32, i32)>= GlobalExpiredTime::new(Some("UTC".to_owned()));//This what i want to, use with different digits or anything convertible, but wish convert to all(Into<u32>, Into<u64>...)
}

I want to initialize struct and pass between programs, measure several parts. Thanks

Honestly, this is basically unreadable. I don't think I would be able to help you with just this.

1 Like

The main part- in main, want to pass types u32, (u32, u32): that's all, excuse me
Compiler error- the trait bound (i32, i32): num_traits::ToPrimitive is not satisfied
the trait num_traits::ToPrimitive is not implemented for (i32, i32)

After some formatting and adding imports

use chrono::DateTime;
use chrono::Local;
use chrono::Utc;
use num::NumCast;
use std::collections::HashMap;
use std::fmt::Display;
use std::marker::PhantomData;
use std::sync::Arc;
use std::sync::Mutex;

#[derive(Debug, Clone)]
//This my struct
pub struct GlobalExpiredTime<'ph, T: NumCast, V, P: ?Sized>
where
    P: 'ph + std::marker::Send,
    V: num_traits::ToPrimitive,
    T: num_traits::ToPrimitive + num_traits::NumCast,
{
    //First-  time of entry in block (Local or UTC)
    //Second- key: (for example simply) number of block (or associated name)
    //        value: time count (on whether it was done in threads or simply run the same block twise, thrice, fourfold etc.)
    //(Option- maybe you don't want to take it into account, then counted time on this=0)
    LBlockT: Arc<Mutex<Vec<DateTime<Local>>>>, // u32- count times
    IntInfo: HashMap<T, (V, Option<u32>)>,     //Int- intilligence (mark it)
    BlLife: PhantomData<&'ph P>,
}

//For example:
impl<'ph, K, V, P> GlobalExpiredTime<'ph, K, V, P>
where
    //K must be the number! of block in program counted times.(as example)
    //Default- zero, ToPrimitive + std::cmp::Ord- requirement NumCast::from, Send- to use in threads
    K: std::default::Default
        + num_traits::NumCast
        + num_traits::ToPrimitive
        + Copy
        + Sync
        + std::cmp::Ord
        + std::fmt::Debug, //, TimeSpec:
    V: Sync
        + std::fmt::Debug
        + Clone
        + Display
        + num_traits::NumCast
        + num_traits::ToPrimitive
        + std::cmp::Ord,

    P: std::marker::Send,
{
    fn new(t_kind: Option<String>) -> Self {
        if let Some(kind_of_time) = t_kind {
            if kind_of_time.to_uppercase() == "UTC" {
                let now: DateTime<Utc> = Utc::now();
                println!("UTC now is: {}, so you had instantiated time in program TBC_eq(TransferBurguerCorrection_eq)", now.to_rfc2822());
            } else if kind_of_time.to_uppercase() == "LOCAL" {
                //How impl |"LOC" ... etc?
                let now: DateTime<Local> = Local::now();
                println!("Local time now is: {}, so you had instantiated time in program TBC_eq(TransferBurguerCorrection_eq)", now.to_rfc2822());
            }
        }
        //Now only create fields and push them in structs
        let mut date_vec = Vec::<DateTime<Local>>::new();
        date_vec.push(Local::now());
        let LBlockT = Arc::new(Mutex::new(date_vec));
        let IntInfo = HashMap::new();
        Self {
            LBlockT, //equiv LBlockT: LBlockT
            IntInfo, //because we know only start point- no other info to push
            BlLife: PhantomData,
        }
    }
}
fn main() {
    //....
    let mut time: GlobalExpiredTime<'_, u32, i32, i32> =
        GlobalExpiredTime::new(Some("UTC".to_owned()));
}

it does compile if you replace GlobalExpiredTime<u32, (i32, i32)> with GlobalExpiredTime<u32, i32, i32>.

The important error message was

error[E0107]: wrong number of type arguments: expected 3, found 2
  --> src/main.rs:75:19
   |
75 |     let mut time: GlobalExpiredTime<u32, (i32, i32)> =
   |                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected 3 type arguments

Unless this isn’t what you meant and you want to use (i32, i32) as a numeric type (which it really isn’t; that’s why you get the other error message).

Note that I haven’t tried to understand the actual code yet, so it might still not behave as intended. (But getting it to compile is probably a good first step.)

1 Like

How did I not notice, that's no need to understand, as i at first try to explain, but also question: i want to create structure and pass this instance to another program, how?
And in equality

if *= "a"{// if string is long, want to also compare
//*="b"|"c"| ....
...
}

Alternatively, how i can choose?