-
Notifications
You must be signed in to change notification settings - Fork 0
/
Resource.scala
78 lines (63 loc) · 1.95 KB
/
Resource.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package recycle
package managed
import scala.util.control.NonFatal
trait Resource[A]:
def allocate: (A, () => Unit)
def use[B](f: A => B): B =
val (a, release) = allocate
var toThrow: Throwable = null
try f(a)
catch
case NonFatal(e) =>
toThrow = e
null.asInstanceOf[B]
finally
try release()
catch
case NonFatal(e) =>
if toThrow ne null then toThrow.addSuppressed(e)
else toThrow = e
if toThrow ne null then throw toThrow
end try
end use
def map[B](f: A => B): Resource[B] = Resource.Map(this, f)
def flatMap[B](f: A => Resource[B]): Resource[B] = Resource.Bind(this, f)
end Resource
object Resource:
def apply[A](acquire: => A)(release: A => Unit): Resource[A] = new Resource[A]:
def allocate: (A, () => Unit) =
val a = acquire
(a, () => release(a))
private[Resource] final class Map[A, B](underlying: Resource[A], f: A => B) extends Resource[B]:
def allocate: (B, () => Unit) =
val (a, release) = underlying.allocate
(f(a), release)
private[Resource] final class Bind[A, B](underlying: Resource[A], f: A => Resource[B]) extends Resource[B]:
def allocate: (B, () => Unit) =
val (a, releaseA) = underlying.allocate
try
val (b, releaseB) = f(a).allocate
val releaseBoth = () =>
var toThrow: Throwable = null
try releaseB()
catch case NonFatal(e) => toThrow = e
finally
try releaseA()
catch
case NonFatal(e) =>
if toThrow ne null then e.addSuppressed(toThrow)
toThrow = e
end try
if toThrow ne null then throw toThrow
(b, releaseBoth)
catch
case NonFatal(e) =>
try releaseA()
catch
case NonFatal(e2) =>
e.addSuppressed(e2)
throw e
end try
end allocate
end Bind
end Resource