Skip to content

Instantly share code, notes, and snippets.

@SteveGilham
SteveGilham / build.rs
Created July 4, 2016 18:46
A hacky resource builder script for Rust-MSVC
use std::process::Command;
use std::env;
use std::path::Path;
fn main() {
let out_dir = env::var("OUT_DIR").ok().expect("can't find out_dir");
Command::new("C:\\Program Files (x86)\\Windows Kits\\8.1\\bin\\x64\\rc.exe")
.args(&["/v", "/fo", "hello_rc.lib"]) // HACK HACK HACK
.args(&["..\\..\\..\\..\\..\\src\\hello.rc"])
override this.OnLoad(e:EventArgs) =
this.Visible <- false // Hide form window.
this.ShowInTaskbar <- false; // Remove from taskbar.
(this :> INetworkListManagerEvents).ConnectivityChanged(nlm.GetConnectivity())
let nlmGuid = typeof<INetworkListManagerEvents>.GUID
let icpc = (nlm :> obj) :?> IConnectionPointContainer
icp <- icpc.FindConnectionPoint(ref nlmGuid);
cookie <- icp.Advise(this);
base.OnLoad(e)
@SteveGilham
SteveGilham / Connectivity.fs
Created December 12, 2015 23:35
Getting connectivity state
open System
open System.Drawing
open System.Reflection
open System.Resources
open System.Runtime.InteropServices.ComTypes
open System.Windows.Forms
open NETWORKLIST
let OnExit (sender : obj) (e : EventArgs) = Application.Exit()
let FindIcon name = new Icon(Assembly.GetExecutingAssembly().GetManifestResourceStream(name))
internal static class $Program
{
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
internal static FSharpFunc<int, int> add2@11;
[DebuggerBrowsable(DebuggerBrowsableState.Never), DebuggerNonUserCode, CompilerGenerated]
internal static int init@;
public static void main@()
{
int n = 2;
$Program.add2@11 = new Program.add2@11(n);
@SteveGilham
SteveGilham / gist:a5f0ec2c7de7764fcfb9
Created May 14, 2015 18:38
Function serialization example
open System
open System.IO
open System.Runtime.Serialization.Formatters.Soap
open System.Text
let mhead(m : list<Object>) : list<Object> = [m.Head]
let addN n =
(fun x -> x + n)
internal static readonly Program.Card _unique_King = new Program.Card(0);
@SteveGilham
SteveGilham / gist:1a749242f0e541126704
Last active August 29, 2015 14:20
Get Union types -- generalised
let GetUnionTypes (t:Type) =
seq { yield t
if FSharpType.IsUnion t
then yield! FSharpType.GetUnionCases t
// implementation detail leaks here -- nested type name
|> Seq.map (fun x -> Type.GetType ((string t) + "+" + x.Name))
// except when the case has no data, so there's no such class
|> Seq.filter (fun x -> x <> null) }
@SteveGilham
SteveGilham / gist:6e7ea11c4cfd24303e6f
Created May 8, 2015 18:56
Bug in first version of the function
type Card = King | Queen| Jack | Spot of int;;
GetUnionTypes typeof<Card> |> Seq.iter (fun x -> printfn "%s" x.FullName);;
@SteveGilham
SteveGilham / gist:73cc1301d00fee013030
Last active August 29, 2015 14:20
Sample of serializing union type
open System.IO
open System.Runtime.Serialization.Json
open System.Text
let exprTypes = GetUnionTypes baseType
let ToJson<'t> (myObj:'t) =
use ms = new MemoryStream()
DataContractJsonSerializer(typeof<'t>, exprTypes).WriteObject(ms, myObj)
Encoding.Default.GetString(ms.ToArray())
@SteveGilham
SteveGilham / gist:58c18e5dedc5b4c95d3d
Created May 7, 2015 05:22
Getting the types in a union type.
open System
open Microsoft.FSharp.Reflection
let GetUnionTypes (t:Type) =
seq { yield t
if FSharpType.IsUnion t
then yield! FSharpType.GetUnionCases t
// implementation detail leaks here -- nested type name
|> Seq.map (fun x -> (string t) + "+" + x.Name)
|> Seq.map Type.GetType }