seal_rs 0.3.2

Set of classic asynchronous primitives (Actors, Executors, Futures / Promises)
Documentation
#[macro_use]extern crate seal_rs;
#[macro_use]extern crate match_downcast;
#[macro_use] extern crate failure;

//use std::fs;
//use seal_rs::actors::default_dispatcher::DefaultDispatcher;
//use seal_rs::actors::dispatcher::Dispatcher;
//use seal_rs::actors::actor_cell::ActorCell;
use seal_rs::actors::props::Props;
//use seal_rs::actors::unbound_mailbox::UnboundMailbox;
//use seal_rs::actors::actor_path::ActorPath;
use seal_rs::actors::local_actor_system::LocalActorSystem;
//use seal_rs::actors::abstract_actor_ref::ActorRef;
use seal_rs::actors::actor_ref_factory::ActorRefFactory;
//use seal_rs::actors::abstract_actor_system::AbstractActorSystem;
//use seal_rs::actors::actor;
////use seal_rs::testkit::actors::test_local_actor_ref::TestLocalActorRef;
////use seal_rs::testkit::actors::test_local_actor_system::TestLocalActorSystem;
//use seal_rs::actors::envelope::Envelope;
////use seal_rs::exps::boo::{Pong};
////use seal_rs::exps::foo::{Foo, Ping, Zoom};
//use seal_rs::exps::bench::{Bench, Tick, FictionOne, FictionTwo, FictionThree};
////use seal_rs::examples;
use seal_rs::common::tsafe::TSafe;
//use seal_rs::executors::executor::ExecutionContext;
//use std::thread;
//use std::sync::{Mutex, Arc};
use std::rc::Rc;
//
//use std::any::Any;
use seal_rs::futures::future::{Future, WrappedFuture};
use seal_rs::futures::promise::Promise;
use seal_rs::futures::completable_promise::CompletablePromise;
use seal_rs::futures::async_promise::AsyncPromise;

use seal_rs::executors::thread_pinned_executor::{ThreadPinnedExecutor, DistributionStrategy, TaskOptions};
use seal_rs::executors::executor::Executor;
use std::thread;
use std::sync::{Mutex, Arc};
use std::time::{Duration, SystemTime};
use std::ops::{Deref, DerefMut};
use failure::Error;
use failure::Fail;

#[derive(Clone)]
struct Foo {
    value: u128
}

impl Drop for Foo {
    fn drop(&mut self) {
        println!("Dropped!")
    }
}


#[derive(Debug, Fail)]
enum ToolchainError {
    #[fail(display = "invalid toolchain name: {}", name)]
    InvalidToolchainName {
        name: String,
    },
    #[fail(display = "unknown toolchain version: {}", version)]
    UnknownToolchainVersion {
        version: String,
    }
}



fn main() {

    seal_rs::examples::futures::example::run();

//    {
//        let mut p: CompletablePromise<Foo, TSafe<Fail + Send>> = CompletablePromise::new();
//        let mut fut = p.future();
//
//        fut.map(|v| {
//            Ok(format!("{}", v.value))
//        }).recover(|v| {
//            //Ok("recovered".to_string())
//            Err(tsafe!(ToolchainError::InvalidToolchainName { name: String::from("Error at recover!")}))
//        }).on_complete(|v| {
//            if v.is_ok() {
//                println!("Final result = {}", v.as_ref().ok().unwrap());
//            } else {
//                println!("Error = {}", v.as_ref().err().unwrap().lock().unwrap());
//            }
//        });
//
//
//        thread::spawn(move || {
//            //p.failure(tsafe!(ToolchainError::InvalidToolchainName { name: String::from("Oops!")}));
//            p.success(Foo { value: 1000 });
//        });
//    }




    // !!!!!!!!!!!!!!1 THIS IS EXAMPLE!!!!!!!!!!!!!!
//    {
//        let mut executor = ThreadPinnedExecutor::new()
//            .set_threads_count(8)
//            .set_distribution_strategy(DistributionStrategy::Load)
//            .run();
//        let mut executor = tsafe!(executor);
//
//        let start = SystemTime::now();
//        let z = 500;
//
//
//        let mut executor0 = executor.clone();
//        let mut executor1 = executor.clone();
//
//        let mut fut: WrappedFuture<Foo, TSafe<Fail + Send>> =
//            Future::asyncp(move || Ok(Foo {value: 100}), executor0);
//
//        fut.flat_map(move |v| {
//            let e = executor1.clone();
//            let vt = v.value;
//
//            if v.value > 100 {
//                let mut new_fut: WrappedFuture<String, TSafe<Fail + Send>> =
//                    Future::asyncp(move || Ok(format!("{}", vt)), e);
//
//                Ok(new_fut)
//            } else {
//                Err(tsafe!(ToolchainError::InvalidToolchainName { name: String::from("Oops! v < 100")}))
//            }
//        }).map(|v| {
//            println!("Map string = {}", v);
//            Ok(true)
//        }).on_complete(|v| {
//            if v.is_ok() {
//                println!("Final result = {}", v.as_ref().ok().unwrap());
//            } else {
//                println!("Error = {}", v.as_ref().err().unwrap().lock().unwrap());
//            }
//        });
//    }






//        let p = {
//            let mut p: AsyncPromise<u32, TSafe<std::error::Error + Send>> =
//                AsyncPromise::new(Box::new(move || Ok(z + 100)));
//            let mut fut = p.future();
//
//
//            /*fut.map(|v: &u32| {
//                let r = v + 1000;
//               // println!("map");
//                Ok(r)
//            }).on_complete(move |v: &Result<u32, _>| {
//                let v = *v.as_ref().unwrap();
//                //println!("result={}", v);
//                println!("time={}", start.elapsed().unwrap().as_micros());
//            });*/
//
//            //let x = Foo { value: 100 };
//            //p.success(x);
//            p
//        };
//
//        thread::sleep_ms(4000);
//    }



    //thread::spawn(move || {
   //     p.lock().unwrap().success(Box::new(10));
    //});





//    let mut executor = ThreadPinnedExecutor::new()
//        .set_threads_count(8)
//        .set_distribution_strategy(DistributionStrategy::Load)
//        .run();
//
//        let f0 = Box::new( || { println!("Task on implicitly selected thread") });
//        executor.execute(f0, None);
//
//        let f1 = Box::new( || { println!("Task on explicitly selected thread with id 6") });
//        executor.execute(f1, Some( Box::new(TaskOptions { thread_id: Some(6) } )));

//    let mut executor = ThreadPinnedExecutor::new()
//        .set_threads_count(4)
//        .set_distribution_strategy(DistributionStrategy::Round)
//        .run();
//
//    let mut x0 = tsafe!(0);
//    let mut x1 = tsafe!(0);
//    let mut x2 = tsafe!(0);
//    let mut x3 = tsafe!(0);
//    let time0 = SystemTime::now();
//    let time1 = SystemTime::now();
//    let time2 = SystemTime::now();
//    let time3 = SystemTime::now();
//
//
//    let iterations = 2500;
//
//    let t0 = SystemTime::now();
//    for _ in 0..iterations {
//        let x = x0.clone();
//        let f = Box::new(move || {
//            let mut z = x.lock().unwrap();
//            *z = *z + num_cpus::get();
//            if *z >= iterations * 4 {
//                println!("U0={}", time0.elapsed().unwrap().as_millis());
//            }
//        });
//
//        executor.execute(f, Some( Box::new(TaskOptions { thread_id: Some(0) } )));
//    }
//    println!("L0={}", t0.elapsed().unwrap().as_millis());
//
//    let t1 = SystemTime::now();
//    for _ in 0..iterations {
//        let x = x1.clone();
//        let f = Box::new(move || {
//            let mut z = x.lock().unwrap();
//            *z = *z + num_cpus::get();
//            if *z >= iterations * 4 {
//                println!("U1={}", time1.elapsed().unwrap().as_millis());
//            }
//        });
//
//        executor.execute(f, Some( Box::new(TaskOptions { thread_id: Some(1) } )));
//    }
//    println!("L1={}", t1.elapsed().unwrap().as_millis());
//
//    let t2 = SystemTime::now();
//    for _ in 0..iterations {
//        let x = x2.clone();
//        let f = Box::new(move || {
//            let mut z = x.lock().unwrap();
//            *z = *z + num_cpus::get();
//            if *z >= iterations * 4 {
//                println!("U2={}", time2.elapsed().unwrap().as_millis());
//            }
//        });
//
//        executor.execute(f, Some( Box::new(TaskOptions { thread_id: Some(2) } )));
//    }
//    println!("L2={}", t2.elapsed().unwrap().as_millis());
//
//    let t3 = SystemTime::now();
//    for _ in 0..iterations {
//        let x = x3.clone();
//        let f = Box::new(move || {
//            let mut z = x.lock().unwrap();
//            *z = *z + 1;
//            if *z >= iterations {
//                println!("U3={}", time3.elapsed().unwrap().as_millis());
//            }
//        });
//
//        executor.execute(f, Some( Box::new(TaskOptions { thread_id: Some(3) } )));
//    }
//    println!("L3={}", t3.elapsed().unwrap().as_millis());

//    let f = Box::new( || { println!("OK") });
//    executor.execute(f, None);
//
//    thread::sleep_ms(10);
//
//    executor.stop();




    //let system: RefFactory<Any> + System = LocalSystem {};


//
//    let mut system = LocalActorSystem::new();
//    let aref = system.lock().unwrap().actor_of(Props::new(tsafe!(Bench::new())), None);
    //seal_rs::examples::executors::example::run();

//    let mut system = LocalActorSystem::new();
//    let mut  bench0 =  system.lock().unwrap().actor_of(Props::new(tsafe!(Bench::new())), None);
//    let mut  bench1 = system.lock().unwrap().actor_of(Props::new(tsafe!(Bench::new())), None);
//    let mut  bench2 = system.lock().unwrap().actor_of(Props::new(tsafe!(Bench::new())), None);
//    let mut  bench3 = system.lock().unwrap().actor_of(Props::new(tsafe!(Bench::new())), None);
//
//    thread::spawn(move || {
//        for n in 0..250000 {
//            bench0.tell(Box::new(Tick { }), Some(bench0.clone()));
//        }
//    });
//
//
//    thread::spawn(move || {
//        for n in 0..250000 {
//            bench1.tell(Box::new(Tick { }), Some(bench1.clone()));
//        }
//    });
//
//    thread::spawn(move || {
//        for n in 0..250000 {
//            bench2.tell(Box::new(Tick { }), Some(bench2.clone()));
//        }
//    });
//
//    thread::spawn(move || {
//        for n in 0..250000 {
//            bench3.tell(Box::new(Tick { }), Some(bench3.clone()));
//        }
//    });



    //thread::sleep(Duration::from_secs(100));

   /* bench0.tell(Box::new(FictionOne { x: String::from("xxx") }), None);
    bench0.tell(Box::new(FictionTwo { x: 10 }), None);
    bench0.tell(Box::new(FictionThree { x: 30 }), None);

    bench1.tell(Box::new(FictionOne { x: String::from("xxx") }), None);
    bench1.tell(Box::new(FictionTwo { x: 10 }), None);
    bench1.tell(Box::new(FictionThree { x: 30 }), None);

    bench2.tell(Box::new(FictionOne { x: String::from("xxx") }), None);
    bench2.tell(Box::new(FictionTwo { x: 10 }), None);
    bench2.tell(Box::new(FictionThree { x: 30 }), None);

    bench3.tell(Box::new(FictionOne { x: String::from("xxx") }), None);
    bench3.tell(Box::new(FictionTwo { x: 10 }), None);
    bench3.tell(Box::new(FictionThree { x: 30 }), None);*/





    //examples::basic::example::run();
    /*let mut system = ActorSystem::new();
    let mut system_clone = system.clone();

    let mut aref = {
        let mut usystem = system.lock().unwrap();
        usystem.run();
        usystem.actor_of(Props::new(tsafe!(Foo::new())), None)
    };

    let mut aref_clone = aref.clone();





    thread::spawn(move ||  {*/


        //aref.tell(Box::new(Ping { v: 0, xt: "My str".to_string()}), None); //
        //aref.tell(Box::new(Zoom { }), None); //
        //aref.tell(Box::new(actor::PoisonPill { }), None);
        //system_clone.lock().unwrap().stop(&aref);
        //aref.tell(Box::new(Ping { v: 1, xt: "My str".to_string()}), None);
        //aref.tell(Box::new(Ping { v: 1, xt: "My str".to_string()}), None);

        /*let mut d = dispatcher_copy.lock().unwrap();

        let task = DefaultDispatcher::new_task(Box::new(|| println!("XXX")));
        let envelope = Envelope::new(Box::new(Ping { v: 199, xt: "My str".to_string()}));
        d.dispatch(cell_copy, envelope);*/

   // });

    //aref_clone.tell(Box::new(Zoom {}), Some(aref_clone.clone()));

//    for n in 10..20 {
//        //aref_clone.tell(Box::new(Ping { v: n, xt: "My str".to_string()}), None)
//    }

    /*loop {
        {
            let mut d = dispatcher.lock().unwrap();
            let task = DefaultDispatcher::new_task(Box::new(|| println!("ZZZ")));

            d.register_for_execution(-1, task);
        }
        thread::sleep(Duration::from_secs(1));
    }*/

    thread::sleep(Duration::from_secs(500));


}