Import Upstream version 20180207
[hcoop/debian/mlton.git] / basis-library / mlton / weak.sml
1 (* Copyright (C) 2003-2006 Henry Cejtin, Matthew Fluet, Suresh
2 * Jagannathan, and Stephen Weeks.
3 *
4 * MLton is released under a BSD-style license.
5 * See the file MLton-LICENSE for details.
6 *)
7
8 structure MLtonWeak =
9 struct
10 structure Weak = Primitive.MLton.Weak
11
12 type 'a t = 'a Weak.t
13
14 val new = Weak.new
15
16 fun get (w: 'a t): 'a option =
17 let
18 (* Need to do the canGet after the get. If you did the canGet first,
19 * there could be a GC that invalidates the pointer between the
20 * canGet and the get.
21 *)
22 val x = Weak.get w
23 in
24 if Weak.canGet w
25 then SOME x
26 else NONE
27 end
28 end