Skip to content

Commit 0aeb423

Browse files
Delete Layers That Output Default Services (zio#6783)
* delete layers that output default services * fix warning * fix compile issue * one more try * yay Co-authored-by: Kit Langton <[email protected]>
1 parent f28ac8d commit 0aeb423

File tree

19 files changed

+134
-203
lines changed

19 files changed

+134
-203
lines changed

core-tests/shared/src/test/scala/zio/stm/TRandomSpec.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package zio.stm
33
import zio.stm.TRandom._
44
import zio.test.Assertion.{isGreaterThanEqualTo, isLessThan}
55
import zio.test._
6-
import zio.{Random, ZIOBaseSpec}
6+
import zio.ZIOBaseSpec
77

88
object TRandomSpec extends ZIOBaseSpec {
99

@@ -46,7 +46,7 @@ object TRandomSpec extends ZIOBaseSpec {
4646
assert(n)(isLessThan(max))
4747
}
4848
}
49-
).provideCustomLayer(Random.live >>> TRandom.live)
49+
).provideCustomLayer(TRandom.live)
5050

5151
val genDoubles: Gen[Any, (Double, Double)] =
5252
for {

core/shared/src/main/scala/zio/Clock.scala

+1-17
Original file line numberDiff line numberDiff line change
@@ -61,23 +61,7 @@ trait Clock extends Serializable { self =>
6161

6262
object Clock extends ClockPlatformSpecific with Serializable {
6363

64-
val any: ZLayer[Clock, Nothing, Clock] =
65-
ZLayer.service[Clock](Tag[Clock], Tracer.newTrace)
66-
67-
/**
68-
* Constructs a `Clock` service from a `java.time.Clock`.
69-
*/
70-
val javaClock: ZLayer[java.time.Clock, Nothing, Clock] = {
71-
implicit val trace = Tracer.newTrace
72-
ZLayer[java.time.Clock, Nothing, Clock] {
73-
for {
74-
clock <- ZIO.service[java.time.Clock]
75-
} yield ClockJava(clock)
76-
}
77-
}
78-
79-
val live: Layer[Nothing, Clock] =
80-
ZLayer.succeed[Clock](ClockLive)(Tag[Clock], Tracer.newTrace)
64+
val tag: Tag[Clock] = Tag[Clock]
8165

8266
/**
8367
* An implementation of the `Clock` service backed by a `java.time.Clock`.

core/shared/src/main/scala/zio/Console.scala

+1-5
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,7 @@ trait Console extends Serializable {
5555

5656
object Console extends Serializable {
5757

58-
val any: ZLayer[Console, Nothing, Console] =
59-
ZLayer.service[Console](Tag[Console], Tracer.newTrace)
60-
61-
val live: Layer[Nothing, Console] =
62-
ZLayer.succeed[Console](ConsoleLive)(Tag[Console], Tracer.newTrace)
58+
val tag: Tag[Console] = Tag[Console]
6359

6460
object ConsoleLive extends Console {
6561

core/shared/src/main/scala/zio/DefaultServices.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ object DefaultServices {
3030
Console.ConsoleLive,
3131
System.SystemLive,
3232
Random.RandomLive
33-
)
33+
)(Clock.tag, Console.tag, System.tag, Random.tag)
3434

3535
private[zio] val currentServices: FiberRef.WithPatch[ZEnvironment[
3636
Clock with Console with System with Random

core/shared/src/main/scala/zio/Random.scala

+88-101
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ trait Random extends Serializable {
8686

8787
object Random extends Serializable {
8888

89+
val tag: Tag[Random] = Tag[Random]
90+
8991
object RandomLive extends Random {
9092

9193
def nextBoolean(implicit trace: Trace): UIO[Boolean] =
@@ -171,110 +173,95 @@ object Random extends Serializable {
171173
Random.shuffleWith(unsafeNextIntBounded(_), collection)
172174
}
173175

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-
182176
/**
183-
* Constructs a `Random` service from a `scala.util.Random`.
177+
* An implementation of the `Random` service backed by a `scala.util.Random`.
184178
*/
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)
277228
}
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)
278265
}
279266

280267
private[zio] def nextDoubleBetweenWith(minInclusive: Double, maxExclusive: Double)(nextDouble: () => Double): Double =

core/shared/src/main/scala/zio/Runtime.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ object Runtime extends RuntimePlatformSpecific {
504504
}
505505
}
506506

507-
Runtime.Scoped(runtime.environment, runtime.fiberRefs, () => shutdown)
507+
Runtime.Scoped(runtime.environment, runtime.fiberRefs, () => shutdown())
508508
}
509509

510510
class Proxy[+R](underlying: Runtime[R]) extends Runtime[R] {

core/shared/src/main/scala/zio/System.scala

+1-9
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,7 @@ trait System extends Serializable {
7777

7878
object System extends Serializable {
7979

80-
val any: ZLayer[System, Nothing, System] = {
81-
implicit val trace = Tracer.newTrace
82-
ZLayer.service[System]
83-
}
84-
85-
val live: Layer[Nothing, System] = {
86-
implicit val trace = Tracer.newTrace
87-
ZLayer.succeed[System](SystemLive)
88-
}
80+
val tag: Tag[System] = Tag[System]
8981

9082
object SystemLive extends System {
9183
def env(variable: => String)(implicit trace: Trace): IO[SecurityException, Option[String]] =

core/shared/src/main/scala/zio/ZIO.scala

+11-11
Original file line numberDiff line numberDiff line change
@@ -2650,17 +2650,17 @@ object ZIO extends ZIOCompanionPlatformSpecific {
26502650
new ZIO.CheckInterrupt(f, trace)
26512651

26522652
/**
2653-
* Retreives the `Clock` service for this workflow.
2653+
* Retrieves the `Clock` service for this workflow.
26542654
*/
26552655
def clock(implicit trace: Trace): UIO[Clock] =
26562656
ZIO.clockWith(ZIO.succeedNow)
26572657

26582658
/**
2659-
* Retreives the `Clock` service for this workflow and uses it to run the
2659+
* Retrieves the `Clock` service for this workflow and uses it to run the
26602660
* specified workflow.
26612661
*/
26622662
def clockWith[R, E, A](f: Clock => ZIO[R, E, A])(implicit trace: Trace): ZIO[R, E, A] =
2663-
DefaultServices.currentServices.getWith(services => f(services.get[Clock]))
2663+
DefaultServices.currentServices.getWith(services => f(services.get(Clock.tag)))
26642664

26652665
/**
26662666
* Evaluate each effect in the structure from left to right, collecting the
@@ -2859,17 +2859,17 @@ object ZIO extends ZIOCompanionPlatformSpecific {
28592859
ZIO.suspendSucceed(if (predicate) ZIO.succeedNow(result) else ZIO.fail(error))
28602860

28612861
/**
2862-
* Retreives the `Console` service for this workflow.
2862+
* Retrieves the `Console` service for this workflow.
28632863
*/
28642864
def console(implicit trace: Trace): UIO[Console] =
28652865
ZIO.consoleWith(ZIO.succeedNow)
28662866

28672867
/**
2868-
* Retreives the `Console` service for this workflow and uses it to run the
2868+
* Retrieves the `Console` service for this workflow and uses it to run the
28692869
* specified workflow.
28702870
*/
28712871
def consoleWith[R, E, A](f: Console => ZIO[R, E, A])(implicit trace: Trace): ZIO[R, E, A] =
2872-
DefaultServices.currentServices.getWith(services => f(services.get[Console]))
2872+
DefaultServices.currentServices.getWith(services => f(services.get(Console.tag)))
28732873

28742874
/**
28752875
* Prints the specified message to the console for debugging purposes.
@@ -4016,11 +4016,11 @@ object ZIO extends ZIOCompanionPlatformSpecific {
40164016
ZIO.randomWith(ZIO.succeedNow)
40174017

40184018
/**
4019-
* Retreives the `Random` service for this workflow and uses it to run the
4019+
* Retrieves the `Random` service for this workflow and uses it to run the
40204020
* specified workflow.
40214021
*/
40224022
def randomWith[R, E, A](f: Random => ZIO[R, E, A])(implicit trace: Trace): ZIO[R, E, A] =
4023-
DefaultServices.currentServices.getWith(services => f(services.get[Random]))
4023+
DefaultServices.currentServices.getWith(services => f(services.get(Random.tag)))
40244024

40254025
/**
40264026
* Reduces an `Iterable[IO]` to a single `IO`, working sequentially.
@@ -4254,17 +4254,17 @@ object ZIO extends ZIOCompanionPlatformSpecific {
42544254
new ZIO.Suspend(() => zio, trace)
42554255

42564256
/**
4257-
* Retreives the `System` service for this workflow.
4257+
* Retrieves the `System` service for this workflow.
42584258
*/
42594259
def system(implicit trace: Trace): UIO[System] =
42604260
ZIO.systemWith(ZIO.succeedNow)
42614261

42624262
/**
4263-
* Retreives the `System` service for this workflow and uses it to run the
4263+
* Retrieves the `System` service for this workflow and uses it to run the
42644264
* specified workflow.
42654265
*/
42664266
def systemWith[R, E, A](f: System => ZIO[R, E, A])(implicit trace: Trace): ZIO[R, E, A] =
4267-
DefaultServices.currentServices.getWith(services => f(services.get[System]))
4267+
DefaultServices.currentServices.getWith(services => f(services.get(System.tag)))
42684268

42694269
/**
42704270
* Capture ZIO trace at the current point

0 commit comments

Comments
 (0)