Giter VIP home page Giter VIP logo

actix-lua-test's Introduction

test01 actix-lua / actix 0.7 tokio 0.1

actix-lua

extern crate actix_lua;
extern crate actix;
extern crate futures;
use futures::{future, Future};
use actix::*;
use actix_lua::{LuaActorBuilder, LuaMessage};

fn print_type_of<T>(_: &T) {
    println!("{}", std::any::type_name::<T>())
}

fn main () {

    let sys = System::new("test");

    let addr = LuaActorBuilder::new()
        .on_handle_with_lua(r#"return ctx.msg + 42"#)
        .build()
        .unwrap()
        .start();

    let res = addr.send(LuaMessage::from(100));
    
    print_type_of(&res);

    Arbiter::spawn(res.then(|res| {
        match res {
            Ok(result) => println!("SUM: {:?}", result),
            _ => println!("Something wrong"),
        }
        
        System::current().stop();
        future::result(Ok(()))
    }));

    sys.run();

}

test02 actix-lua2 / actic 0.10 tokio 0.2

actix-lua2

extern crate actix_lua;
extern crate actix;
extern crate futures;
use actix::*;
use actix_lua::{LuaActorBuilder, LuaMessage};

fn print_type_of<T>(_: &T) {
    println!("{}", std::any::type_name::<T>())
}


#[actix_rt::main] 
async fn main () {

    let addr = LuaActorBuilder::new()
        .on_handle_with_lua(r#"return ctx.msg + 42"#)
        .build()
        .unwrap()
        .start();

    let res = addr.send(LuaMessage::from(100)).await;
    
    
    print_type_of(&res);
    match res.unwrap() {
            LuaMessage::String(s) => println!("String:{}",s),
            LuaMessage::Integer(s) => println!("Integer:{}",s),
            LuaMessage::Number(s) => println!("Number:{}",s),
            LuaMessage::Boolean(s) => println!("Boolean:{}",s),
            // ignore everything else
            _ => println!("unknown"),

    }


}

test03 actix-lua2 / actic 0.10 tokio 0.2

actix-lua2

extern crate actix_lua;
extern crate actix;
extern crate futures;
use actix::*;
use actix_lua::{LuaActorBuilder, LuaMessage};

use std::fs::File;
use std::io::prelude::*;


fn print_type_of<T>(_: &T) {
    println!("{}", std::any::type_name::<T>())
}

fn read_to_string(filename: &str) -> String {
    let mut f = File::open(filename).expect("File not found");
    let mut body = String::new();
    f.read_to_string(&mut body).expect("Failed to read file");

    body
}

fn main () {

    let mut system = actix::System::new("actix-lua-example");

    system.block_on(async { 
                  let script = read_to_string("./src/test.lua");
                  let addr = LuaActorBuilder::new()
                       .on_handle_with_lua(&script)
                       .build()
                       .unwrap()
                       .start();
    
                   let res = addr.send(LuaMessage::from(100)).await;
                   print_type_of(&res);
                   match res.unwrap() {
                           LuaMessage::String(s) => println!("String:{}",s),
                           LuaMessage::Integer(s) => println!("Integer:{}",s),
                           LuaMessage::Number(s) => println!("Number:{}",s),
                           LuaMessage::Boolean(s) => println!("Boolean:{}",s),
                           // ignore everything else
                           _ => println!("unknown"),

                   }
    
    });

    let _result = system.run();

}
-- test.lua

return ctx.msg + 45

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.