@@ -86,6 +86,8 @@ trait Random extends Serializable {
86
86
87
87
object Random extends Serializable {
88
88
89
+ val tag : Tag [Random ] = Tag [Random ]
90
+
89
91
object RandomLive extends Random {
90
92
91
93
def nextBoolean (implicit trace : Trace ): UIO [Boolean ] =
@@ -171,110 +173,95 @@ object Random extends Serializable {
171
173
Random .shuffleWith(unsafeNextIntBounded(_), collection)
172
174
}
173
175
174
- val any : ZLayer [Random , Nothing , Random ] = {
175
- ZLayer .service[Random ](Tag [Random ], Tracer .newTrace)
176
- }
177
-
178
- val live : Layer [Nothing , Random ] = {
179
- ZLayer .succeed[Random ](RandomLive )(Tag [Random ], Tracer .newTrace)
180
- }
181
-
182
176
/**
183
- * Constructs a `Random` service from a `scala.util.Random`.
177
+ * An implementation of the `Random` service backed by a `scala.util.Random`.
184
178
*/
185
- val scalaRandom : ZLayer [scala.util.Random , Nothing , Random ] = {
186
- implicit val trace = Tracer .newTrace
187
- ZLayer {
188
- for {
189
- random <- ZIO .service[scala.util.Random ]
190
- } yield new Random {
191
- def nextBoolean (implicit trace : Trace ): UIO [Boolean ] =
192
- ZIO .succeed(unsafeNextBoolean())
193
- def nextBytes (length : => Int )(implicit trace : Trace ): UIO [Chunk [Byte ]] =
194
- ZIO .succeed(unsafeNextBytes(length))
195
- def nextDouble (implicit trace : Trace ): UIO [Double ] =
196
- ZIO .succeed(unsafeNextDouble())
197
- def nextDoubleBetween (minInclusive : => Double , maxExclusive : => Double )(implicit
198
- trace : Trace
199
- ): UIO [Double ] =
200
- ZIO .succeed(unsafeNextDoubleBetween(minInclusive, maxExclusive))
201
- def nextFloat (implicit trace : Trace ): UIO [Float ] =
202
- ZIO .succeed(unsafeNextFloat())
203
- def nextFloatBetween (minInclusive : => Float , maxExclusive : => Float )(implicit
204
- trace : Trace
205
- ): UIO [Float ] =
206
- ZIO .succeed(unsafeNextFloatBetween(minInclusive, maxExclusive))
207
- def nextGaussian (implicit trace : Trace ): UIO [Double ] =
208
- ZIO .succeed(unsafeNextGaussian())
209
- def nextInt (implicit trace : Trace ): UIO [Int ] =
210
- ZIO .succeed(unsafeNextInt())
211
- def nextIntBetween (minInclusive : => Int , maxExclusive : => Int )(implicit trace : Trace ): UIO [Int ] =
212
- ZIO .succeed(unsafeNextIntBetween(minInclusive, maxExclusive))
213
- def nextIntBounded (n : => Int )(implicit trace : Trace ): UIO [Int ] =
214
- ZIO .succeed(unsafeNextIntBounded(n))
215
- def nextLong (implicit trace : Trace ): UIO [Long ] =
216
- ZIO .succeed(unsafeNextLong())
217
- def nextLongBetween (minInclusive : => Long , maxExclusive : => Long )(implicit trace : Trace ): UIO [Long ] =
218
- ZIO .succeed(unsafeNextLongBetween(minInclusive, maxExclusive))
219
- def nextLongBounded (n : => Long )(implicit trace : Trace ): UIO [Long ] =
220
- ZIO .succeed(unsafeNextLongBounded(n))
221
- def nextPrintableChar (implicit trace : Trace ): UIO [Char ] =
222
- ZIO .succeed(unsafeNextPrintableChar())
223
- def nextString (length : => Int )(implicit trace : Trace ): UIO [String ] =
224
- ZIO .succeed(unsafeNextString(length))
225
- def nextUUID (implicit trace : Trace ): UIO [UUID ] =
226
- ZIO .succeed(unsafeNextUUID())
227
- def setSeed (seed : => Long )(implicit trace : Trace ): UIO [Unit ] =
228
- ZIO .succeed(unsafeSetSeed(seed))
229
- def shuffle [A , Collection [+ Element ] <: Iterable [Element ]](
230
- collection : => Collection [A ]
231
- )(implicit bf : BuildFrom [Collection [A ], A , Collection [A ]], trace : Trace ): UIO [Collection [A ]] =
232
- ZIO .succeed(unsafeShuffle(collection))
233
- override private [zio] def unsafeNextBoolean (): Boolean =
234
- random.nextBoolean()
235
- override private [zio] def unsafeNextBytes (length : Int ): Chunk [Byte ] = {
236
- val array = Array .ofDim[Byte ](length)
237
- random.nextBytes(array)
238
- Chunk .fromArray(array)
239
- }
240
- override private [zio] def unsafeNextDouble (): Double =
241
- random.nextDouble()
242
- override private [zio] def unsafeNextDoubleBetween (minInclusive : Double , maxExclusive : Double ): Double =
243
- nextDoubleBetweenWith(minInclusive, maxExclusive)(() => unsafeNextDouble())
244
- override private [zio] def unsafeNextFloat (): Float =
245
- random.nextFloat()
246
- override private [zio] def unsafeNextFloatBetween (minInclusive : Float , maxExclusive : Float ): Float =
247
- nextFloatBetweenWith(minInclusive, maxExclusive)(() => unsafeNextFloat())
248
- override private [zio] def unsafeNextGaussian (): Double =
249
- random.nextGaussian()
250
- override private [zio] def unsafeNextInt (): Int =
251
- random.nextInt()
252
- override private [zio] def unsafeNextIntBetween (minInclusive : Int , maxExclusive : Int ): Int =
253
- nextIntBetweenWith(minInclusive, maxExclusive)(() => unsafeNextInt(), unsafeNextIntBounded(_))
254
- override private [zio] def unsafeNextIntBounded (n : Int ): Int =
255
- random.nextInt(n)
256
- override private [zio] def unsafeNextLong (): Long =
257
- random.nextLong()
258
- override private [zio] def unsafeNextLongBetween (minInclusive : Long , maxExclusive : Long ): Long =
259
- nextLongBetweenWith(minInclusive, maxExclusive)(() => unsafeNextLong(), unsafeNextLongBounded(_))
260
- override private [zio] def unsafeNextLongBounded (n : Long ): Long =
261
- Random .nextLongBoundedWith(n)(() => unsafeNextLong())
262
- override private [zio] def unsafeNextPrintableChar (): Char =
263
- random.nextPrintableChar()
264
- override private [zio] def unsafeNextString (length : Int ): String =
265
- random.nextString(length)
266
- override private [zio] def unsafeNextUUID (): UUID =
267
- Random .nextUUIDWith(() => unsafeNextLong())
268
- override private [zio] def unsafeSetSeed (seed : Long ): Unit =
269
- random.setSeed(seed)
270
- override private [zio] def unsafeShuffle [A , Collection [+ Element ] <: Iterable [Element ]](
271
- collection : Collection [A ]
272
- )(implicit
273
- bf : BuildFrom [Collection [A ], A , Collection [A ]]
274
- ): Collection [A ] =
275
- Random .shuffleWith(unsafeNextIntBounded(_), collection)
276
- }
179
+ final case class RandomScala (random : scala.util.Random ) extends Random {
180
+ def nextBoolean (implicit trace : Trace ): UIO [Boolean ] =
181
+ ZIO .succeed(unsafeNextBoolean())
182
+ def nextBytes (length : => Int )(implicit trace : Trace ): UIO [Chunk [Byte ]] =
183
+ ZIO .succeed(unsafeNextBytes(length))
184
+ def nextDouble (implicit trace : Trace ): UIO [Double ] =
185
+ ZIO .succeed(unsafeNextDouble())
186
+ def nextDoubleBetween (minInclusive : => Double , maxExclusive : => Double )(implicit
187
+ trace : Trace
188
+ ): UIO [Double ] =
189
+ ZIO .succeed(unsafeNextDoubleBetween(minInclusive, maxExclusive))
190
+ def nextFloat (implicit trace : Trace ): UIO [Float ] =
191
+ ZIO .succeed(unsafeNextFloat())
192
+ def nextFloatBetween (minInclusive : => Float , maxExclusive : => Float )(implicit
193
+ trace : Trace
194
+ ): UIO [Float ] =
195
+ ZIO .succeed(unsafeNextFloatBetween(minInclusive, maxExclusive))
196
+ def nextGaussian (implicit trace : Trace ): UIO [Double ] =
197
+ ZIO .succeed(unsafeNextGaussian())
198
+ def nextInt (implicit trace : Trace ): UIO [Int ] =
199
+ ZIO .succeed(unsafeNextInt())
200
+ def nextIntBetween (minInclusive : => Int , maxExclusive : => Int )(implicit trace : Trace ): UIO [Int ] =
201
+ ZIO .succeed(unsafeNextIntBetween(minInclusive, maxExclusive))
202
+ def nextIntBounded (n : => Int )(implicit trace : Trace ): UIO [Int ] =
203
+ ZIO .succeed(unsafeNextIntBounded(n))
204
+ def nextLong (implicit trace : Trace ): UIO [Long ] =
205
+ ZIO .succeed(unsafeNextLong())
206
+ def nextLongBetween (minInclusive : => Long , maxExclusive : => Long )(implicit trace : Trace ): UIO [Long ] =
207
+ ZIO .succeed(unsafeNextLongBetween(minInclusive, maxExclusive))
208
+ def nextLongBounded (n : => Long )(implicit trace : Trace ): UIO [Long ] =
209
+ ZIO .succeed(unsafeNextLongBounded(n))
210
+ def nextPrintableChar (implicit trace : Trace ): UIO [Char ] =
211
+ ZIO .succeed(unsafeNextPrintableChar())
212
+ def nextString (length : => Int )(implicit trace : Trace ): UIO [String ] =
213
+ ZIO .succeed(unsafeNextString(length))
214
+ def nextUUID (implicit trace : Trace ): UIO [UUID ] =
215
+ ZIO .succeed(unsafeNextUUID())
216
+ def setSeed (seed : => Long )(implicit trace : Trace ): UIO [Unit ] =
217
+ ZIO .succeed(unsafeSetSeed(seed))
218
+ def shuffle [A , Collection [+ Element ] <: Iterable [Element ]](
219
+ collection : => Collection [A ]
220
+ )(implicit bf : BuildFrom [Collection [A ], A , Collection [A ]], trace : Trace ): UIO [Collection [A ]] =
221
+ ZIO .succeed(unsafeShuffle(collection))
222
+ override private [zio] def unsafeNextBoolean (): Boolean =
223
+ random.nextBoolean()
224
+ override private [zio] def unsafeNextBytes (length : Int ): Chunk [Byte ] = {
225
+ val array = Array .ofDim[Byte ](length)
226
+ random.nextBytes(array)
227
+ Chunk .fromArray(array)
277
228
}
229
+ override private [zio] def unsafeNextDouble (): Double =
230
+ random.nextDouble()
231
+ override private [zio] def unsafeNextDoubleBetween (minInclusive : Double , maxExclusive : Double ): Double =
232
+ nextDoubleBetweenWith(minInclusive, maxExclusive)(() => unsafeNextDouble())
233
+ override private [zio] def unsafeNextFloat (): Float =
234
+ random.nextFloat()
235
+ override private [zio] def unsafeNextFloatBetween (minInclusive : Float , maxExclusive : Float ): Float =
236
+ nextFloatBetweenWith(minInclusive, maxExclusive)(() => unsafeNextFloat())
237
+ override private [zio] def unsafeNextGaussian (): Double =
238
+ random.nextGaussian()
239
+ override private [zio] def unsafeNextInt (): Int =
240
+ random.nextInt()
241
+ override private [zio] def unsafeNextIntBetween (minInclusive : Int , maxExclusive : Int ): Int =
242
+ nextIntBetweenWith(minInclusive, maxExclusive)(() => unsafeNextInt(), unsafeNextIntBounded(_))
243
+ override private [zio] def unsafeNextIntBounded (n : Int ): Int =
244
+ random.nextInt(n)
245
+ override private [zio] def unsafeNextLong (): Long =
246
+ random.nextLong()
247
+ override private [zio] def unsafeNextLongBetween (minInclusive : Long , maxExclusive : Long ): Long =
248
+ nextLongBetweenWith(minInclusive, maxExclusive)(() => unsafeNextLong(), unsafeNextLongBounded(_))
249
+ override private [zio] def unsafeNextLongBounded (n : Long ): Long =
250
+ Random .nextLongBoundedWith(n)(() => unsafeNextLong())
251
+ override private [zio] def unsafeNextPrintableChar (): Char =
252
+ random.nextPrintableChar()
253
+ override private [zio] def unsafeNextString (length : Int ): String =
254
+ random.nextString(length)
255
+ override private [zio] def unsafeNextUUID (): UUID =
256
+ Random .nextUUIDWith(() => unsafeNextLong())
257
+ override private [zio] def unsafeSetSeed (seed : Long ): Unit =
258
+ random.setSeed(seed)
259
+ override private [zio] def unsafeShuffle [A , Collection [+ Element ] <: Iterable [Element ]](
260
+ collection : Collection [A ]
261
+ )(implicit
262
+ bf : BuildFrom [Collection [A ], A , Collection [A ]]
263
+ ): Collection [A ] =
264
+ Random .shuffleWith(unsafeNextIntBounded(_), collection)
278
265
}
279
266
280
267
private [zio] def nextDoubleBetweenWith (minInclusive : Double , maxExclusive : Double )(nextDouble : () => Double ): Double =
0 commit comments