Generic vec of functionpointer (newB)

Hello world,

I am new and just starting codig in Rust.

I have a vec of function pointers and try to execute those.
Al the code "seems" to work eaccept the execution(Raise) of said fPointers.

I've tried casting and dyn enz, but i missing something obviously
Here is the code


use std::{env::Args, fmt::Debug};

struct Event<T:Sized>
    _queue: Vec< T >

impl<T> Event <T> where T:Sized
    fn new() -> Self 
      { Self{_queue:Vec::new()} }

    fn AddHandler (&mut self, fpointer:T)
      { self._queue.push ( fpointer) }
    fn RemoveHandler (&mut self, fpointer:T)
        // FIND AND DELETE!!!!  impl Eq???
        //  let index = self._queue.iter().position
        //    (|&r| r  == fpointer);
        //  self._queue.remove(index.unwrap());
    fn Raise<D>(&self,data:&D)
       for ifun in self._queue 
          // how to execute it ????
          // ive tried  
          //  <T as Handler<T>>::call (ifun,data)
          // HEEEEEEEEEELP what to put here


struct ButtonData {Clikstate:bool} //dummydata
struct Keydata {Key:i8} //dummydata

type Handler<T> =  fn (Data: &T) ->bool;

fn main() 
   // buttonclick
   let mut Buttonklik: Event<Handler<ButtonData>> = Event::new(); 
   let mylambda : Handler<ButtonData> = |e|  
            { println!("MYLambda"); e.Clikstate }; 
  Buttonklik.AddHandler (myfunchandler); 
  Buttonklik.AddHandler (mylambda);
  let but = ButtonData {Clikstate:true};

  Buttonklik.Raise(&but) ;

  // Keypres
  let mykeypres:Handler<Keydata> = |k| {println!("KeyPress") ; true };
  let mut onkeypres: Event<Handler<Keydata>> = Event::new();
  let kp = Keydata {Key:27}; 

  fn myfunchandler( b:&ButtonData)->bool 
      println!("MYFUNC"); true




The way you add constraints to functions is like this:

fn RemoveHandler(&mut self, fpointer: T)
    T: PartialEq,
    let index = self._queue.iter().position(|r| r == &fpointer);

fn Raise<D>(&self, data: &D)
    T: Fn(&D) -> bool,
    for ifun in &self._queue {

The first constraint says that T has an == operator. The second constraint says that T is some sort of function that can be called given only an immutable reference to the function.

1 Like

Thank you!!!!!

You can also put them on the impl block, and they will apply to all functions in it. I see you already found that for Sized (though please note that T: Sized is there by default). Besides that, you should consider using the function pointer directly type instead of being generic. Finally, I strongly recommend trying out rustfmt to format your code a bit nicer.

1 Like

yeay the idea was to accept more function pointers thats why the generic like

fn (Data:T} - > bool
fn (Data:T , Handled:bool} ->() or fn (Data:T} ->Result

So the puzzle is not completely solved but i am a bit closer now :wink:

I am not sure if i like what rustfmt does to the code.
for now i format the code for what I find readebable

Tnx again..... to be continuid...



This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.