Skip to content

Instantly share code, notes, and snippets.

juju gliheng

Block or report user

Report or block gliheng

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@gliheng
gliheng / main.rs
Created Jan 14, 2019
Send closures to anther thread
View main.rs
use std::sync::mpsc::channel;
use std::thread;
fn main() {
let (tx, rx) = channel::<Box<dyn Fn() + Send>>();
thread::spawn(move || {
let _ = tx.send(Box::new(|| {
println!("wtf");
}));
let _ = tx.send(Box::new(|| {
@gliheng
gliheng / main.rs
Created Jan 12, 2019
rust closure to C style function
View main.rs
use lazy_static::lazy_static;
use std::os::raw::{c_int};
use std::sync::Mutex;
use std::ffi::c_void;
lazy_static! {
static ref REAL_CALLBACK: Mutex<Option<Box<FnMut(c_int, c_int) -> c_int + Send>>> = Default::default();
}
@gliheng
gliheng / main.rs
Last active Dec 5, 2018
Mutable lazy_static with a Mutex
View main.rs
#[macro_use]
extern crate lazy_static;
use std::sync::Mutex;
use std::collections::HashMap;
lazy_static! {
static ref HASHMAP: Mutex<HashMap<u32, String>> = Mutex::new({
let mut m = HashMap::new();
m.insert(0, String::from("foo"));
m.insert(1, String::from("bar"));
@gliheng
gliheng / main.rs
Created Dec 4, 2018
implement Send and Sync on ffi pointer type
View main.rs
#[macro_use]
extern crate lazy_static;
use std::collections::HashMap;
use std::sync::{Mutex, Arc};
use std::thread;
lazy_static! {
static ref bank: Mutex<HashMap<Man, u32>> = Mutex::new({
let mut m = HashMap::new();
@gliheng
gliheng / lib.c
Created Nov 26, 2018
Rust ffi with C
View lib.c
#include <stdio.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
void hello()
{
printf("Hello world!\n");
}
@gliheng
gliheng / anination.dart
Last active Aug 8, 2018
Seperating concerns with AnimatedBuilder
View anination.dart
import 'package:flutter/material.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
@gliheng
gliheng / animation.dart
Created Aug 8, 2018
AnimatedWidget demo
View animation.dart
import 'package:flutter/material.dart';
import 'radial_logo.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return new MaterialApp(
@gliheng
gliheng / animation.dart
Created Aug 8, 2018
Tween with a AnimationController
View animation.dart
import 'package:flutter/material.dart';
import 'radial_logo.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return new MaterialApp(
@gliheng
gliheng / animation.dart
Created Aug 8, 2018
AnimationController demo
View animation.dart
import 'package:flutter/material.dart';
import 'radial_logo.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return new MaterialApp(
@gliheng
gliheng / radial_logo.dart
Created Aug 7, 2018
CustomMultiChildLayout
View radial_logo.dart
import 'dart:math';
import 'package:flutter/material.dart';
class RadialLayoutDelegate extends MultiChildLayoutDelegate {
int itemCount;
double radius;
RadialLayoutDelegate({this.itemCount = 0, this.radius});
@override
performLayout(Size size) {
You can’t perform that action at this time.