-
Notifications
You must be signed in to change notification settings - Fork 511
/
IOCompanionPlatform.scala
144 lines (130 loc) · 5.58 KB
/
IOCompanionPlatform.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* Copyright 2020-2024 Typelevel
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cats.effect
import cats.effect.std.Console
import cats.effect.tracing.Tracing
import java.time.Instant
import java.util.concurrent.{CompletableFuture, CompletionStage}
private[effect] abstract class IOCompanionPlatform { this: IO.type =>
private[this] val TypeDelay = Sync.Type.Delay
private[this] val TypeBlocking = Sync.Type.Blocking
private[this] val TypeInterruptibleOnce = Sync.Type.InterruptibleOnce
private[this] val TypeInterruptibleMany = Sync.Type.InterruptibleMany
/**
* Intended for thread blocking operations. `blocking` will shift the execution of the
* blocking operation to a separate threadpool to avoid blocking on the main execution
* context. See the thread-model documentation for more information on why this is necessary.
* Note that the created effect will be uncancelable; if you need cancelation then you should
* use [[interruptible[A](thunk:=>A):*]] or [[interruptibleMany]].
*
* {{{
* IO.blocking(scala.io.Source.fromFile("path").mkString)
* }}}
*
* @param thunk
* The side effect which is to be suspended in `IO` and evaluated on a blocking execution
* context
*
* Implements [[cats.effect.kernel.Sync.blocking]].
*/
def blocking[A](thunk: => A): IO[A] = {
val fn = () => thunk
Blocking(TypeBlocking, fn, Tracing.calculateTracingEvent(fn.getClass))
}
// this cannot be marked private[effect] because of static forwarders in Java
@deprecated("use interruptible / interruptibleMany instead", "3.3.0")
def interruptible[A](many: Boolean, thunk: => A): IO[A] = {
val fn = () => thunk
Blocking(
if (many) TypeInterruptibleMany else TypeInterruptibleOnce,
fn,
Tracing.calculateTracingEvent(fn.getClass))
}
/**
* Like [[blocking]] but will attempt to abort the blocking operation using thread interrupts
* in the event of cancelation. The interrupt will be attempted only once.
*
* Note the following tradeoffs:
* - this has slightly more overhead than [[blocking]] due to the machinery necessary for
* the interrupt coordination,
* - thread interrupts are very often poorly considered by Java (and Scala!) library
* authors, and it is possible for interrupts to result in resource leaks or invalid
* states. It is important to be certain that this will not be the case before using this
* mechanism.
*
* @param thunk
* The side effect which is to be suspended in `IO` and evaluated on a blocking execution
* context
*
* Implements [[cats.effect.kernel.Sync.interruptible[A](thunk:=>A):*]]
*/
def interruptible[A](thunk: => A): IO[A] = {
val fn = () => thunk
Blocking(TypeInterruptibleOnce, fn, Tracing.calculateTracingEvent(fn.getClass))
}
/**
* Like [[blocking]] but will attempt to abort the blocking operation using thread interrupts
* in the event of cancelation. The interrupt will be attempted repeatedly until the blocking
* operation completes or exits.
*
* Note the following tradeoffs:
* - this has slightly more overhead than [[blocking]] due to the machinery necessary for
* the interrupt coordination,
* - thread interrupts are very often poorly considered by Java (and Scala!) library
* authors, and it is possible for interrupts to result in resource leaks or invalid
* states. It is important to be certain that this will not be the case before using this
* mechanism.
*
* @param thunk
* The side effect which is to be suspended in `IO` and evaluated on a blocking execution
* context
*
* Implements [[cats.effect.kernel.Sync!.interruptibleMany]]
*/
def interruptibleMany[A](thunk: => A): IO[A] = {
val fn = () => thunk
Blocking(TypeInterruptibleMany, fn, Tracing.calculateTracingEvent(fn.getClass))
}
def suspend[A](hint: Sync.Type)(thunk: => A): IO[A] =
if (hint eq TypeDelay)
apply(thunk)
else {
val fn = () => thunk
Blocking(hint, fn, Tracing.calculateTracingEvent(fn.getClass))
}
def fromCompletableFuture[A](fut: IO[CompletableFuture[A]]): IO[A] =
asyncForIO.fromCompletableFuture(fut)
def fromCompletionStage[A](completionStage: IO[CompletionStage[A]]): IO[A] =
asyncForIO.fromCompletionStage(completionStage)
def realTimeInstant: IO[Instant] = asyncForIO.realTimeInstant
/**
* Reads a line as a string from the standard input using the platform's default charset, as
* per `java.nio.charset.Charset.defaultCharset()`.
*
* The effect can raise a `java.io.EOFException` if no input has been consumed before the EOF
* is observed. This should never happen with the standard input, unless it has been replaced
* with a finite `java.io.InputStream` through `java.lang.System#setIn` or similar.
*
* @see
* `cats.effect.std.Console#readLineWithCharset` for reading using a custom
* `java.nio.charset.Charset`
*
* @return
* an IO effect that describes reading the user's input from the standard input as a string
*/
def readLine: IO[String] =
Console[IO].readLine
}