Skip to content
This repository has been archived by the owner on Feb 15, 2021. It is now read-only.
/ rustracts Public archive

Rust crate for voidable insurance contracts over a context in async/await rust

License

Notifications You must be signed in to change notification settings

hyyking/rustracts

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

55 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

rustracts Latest Version Rust Documentation

Rust crate for voidable insurance contracts over a context in async/await rust

rustracts = "0.2.0"

Features

Available

  • FuturesContract: Will produce a value at expiration if the contract was not voided
  • OnKillContract: Will produce a value if the context is invalidated
  • OptionContract: Will produce value at expiration if the secondary context has realised and the contract was not voided before

Examples

use std::time::Duration;
    
use crate::context::cmp::GtContext;
use crate::{ContractExt, Status, FuturesContract};

fn simple_contract() {
	let context: usize = 3;
	let c = FuturesContract::new(Duration::from_secs(1), context, |con| -> usize { con + 5 });

	if let Status::Completed(value) = futures::executor::block_on(c) {
		assert_eq!(value, 8)
	} else {
		assert!(false)
	}
}

fn voided_contract() {
	let context = GtContext(3, 2); // Context is true if self.0 > self.1

	let c = FuturesContract::new(Duration::from_secs(4), context, |con| -> usize {
		con.0 + 5
	});

	let handle = std::thread::spawn({
		let mcontext = c.get_context();
		move || {
			(*mcontext.lock().unwrap()).0 = 1; // Modify context before contract ends
		}
	});

	if let Status::Completed(val) = futures::executor::block_on(c) {
		assert_ne!(val, 1);
	} else {
		assert!(true); // Contract should be voided because updated value is 1 which is < 2
	}

	handle.join().unwrap();
}

fn updated_contract() {
	let context = GtContext(3, 2); // Context is valid if self.0 > self.1

	let c = FuturesContract::new(Duration::from_secs(1), context, |con| -> usize {
		con.0 + 5
	});

	let handle = std::thread::spawn({
		let mcontext = c.get_context();
		move || {
			(*mcontext.lock().unwrap()).0 += 2;
		}
	});

	if let Status::Completed(value) = futures::executor::block_on(c) {
		assert_eq!(value, 10);
	} else {
		assert!(false);
	}

	handle.join().unwrap();
}

About

Rust crate for voidable insurance contracts over a context in async/await rust

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages