GithubHelp home page GithubHelp logo

context-rs's Introduction

Context-rs

Build Status Build Status License

Context utilities in Rust

[dependencies]
context = "*"

or use the dev version on master

[dependencies.context]
git = "https://github.com/zonyitoo/context-rs.git"

Usage

#![feature(rt, fnbox, box_raw)]

extern crate context;
extern crate libc;

use std::rt::util::min_stack;
use std::mem;
use std::boxed::FnBox;

use context::{Context, Stack};

extern "C" fn init_fn(arg: usize, f: *mut libc::c_void) {
    // Transmute it back to the Box<Box<FnBox()>>
    {
        let func: Box<Box<FnBox()>> = unsafe {
            Box::from_raw(f as *mut Box<FnBox()>)
        };

        // Call it
        func();

        // The `func` must be destroyed here,
        // or it will cause memory leak.
    }

    // The argument is the context of the main function
    let ctx: &Context = unsafe { mem::transmute(arg) };

    // Switch back to the main function and will never comeback here
    Context::load(ctx);

    unreachable!("Never reach here");
}

fn main() {
    // Initialize an empty context
    let mut cur = Context::empty();

    let mut stk = Stack::new(min_stack());
    let ctx = Context::new(init_fn, unsafe { mem::transmute(&cur) }, Box::new(move|| {
        println!("Inside your function!");
    }), &mut stk);

    println!("Before switch");

    // Switch!
    Context::swap(&mut cur, &ctx);

    println!("Back to main function");
}

Use cargo run --example simple to run this code snippet.

Notices

  • You have to drop the boxed function inside the initialize function!!

  • The resources allocated inside the initialize function must be released before the last context switch.

  • If you context switch inside your callback function, if you decided not to come back, you must release all your resources allocated inside your function.

  • This crate supports platforms in

    • arm
    • i686
    • mips
    • mipsel
    • x86_64
  • The assembly code is in AT&T-style, so currently it only supports *-gnu target on Windows.

context-rs's People

Contributors

gereeter avatar therustmonk avatar zonyitoo avatar

Watchers

 avatar

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.