Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion core-tests/shared/src/test/scala/zio/stm/ZSTMSpec.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package zio
package stm

import scala.util.Try

import zio.duration._
import zio.test.Assertion._
import zio.test.TestAspect.nonFlaky
Expand Down Expand Up @@ -1019,7 +1021,7 @@ object ZSTMSpec extends ZIOBaseSpec {
result <- STM.atomically(for {
_ <- ref.set(2)
newVal1 <- ref.get
_ <- STM.partial(throw new RuntimeException).orElse(STM.unit)
_ <- STM.fromTry(Try(throw new RuntimeException)).orElse(STM.unit)
newVal2 <- ref.get
} yield (newVal1, newVal2))
} yield assert(result)(equalTo(2 -> 2))
Expand Down
4 changes: 2 additions & 2 deletions core/shared/src/main/scala/zio/ZIO.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3064,7 +3064,7 @@ object ZIO extends ZIOCompanionPlatformSpecific {
* Returns an effectful function that extracts out the second element of a
* tuple.
*/
def second[A, B]: ZIO[(A, B), Nothing, B] = fromFunction[(A, B), B](_._2)
def second[A, B]: URIO[(A, B), B] = fromFunction[(A, B), B](_._2)

/**
* Returns an effect that suspends for the specified duration. This method is
Expand All @@ -3088,7 +3088,7 @@ object ZIO extends ZIOCompanionPlatformSpecific {
/**
* Returns an effectful function that merely swaps the elements in a `Tuple2`.
*/
def swap[A, B]: ZIO[(A, B), Nothing, (B, A)] =
def swap[A, B]: URIO[(A, B), (B, A)] =
fromFunction[(A, B), (B, A)](_.swap)

/**
Expand Down
36 changes: 15 additions & 21 deletions core/shared/src/main/scala/zio/stm/STM.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ object STM {
/**
* @see See [[zio.stm.ZSTM.check]]
*/
def check(p: => Boolean): STM[Nothing, Unit] = ZSTM.check(p)
def check(p: => Boolean): USTM[Unit] = ZSTM.check(p)

/**
* @see See [[zio.stm.ZSTM.collectAll]]
Expand All @@ -48,13 +48,13 @@ object STM {
/**
* @see See [[zio.stm.ZSTM.die]]
*/
def die(t: => Throwable): STM[Nothing, Nothing] =
def die(t: => Throwable): USTM[Nothing] =
ZSTM.die(t)

/**
* @see See [[zio.stm.ZSTM.dieMessage]]
*/
def dieMessage(m: => String): STM[Nothing, Nothing] =
def dieMessage(m: => String): USTM[Nothing] =
ZSTM.dieMessage(m)

/**
Expand All @@ -72,7 +72,7 @@ object STM {
/**
* @see See [[zio.stm.ZSTM.fiberId]]
*/
val fiberId: STM[Nothing, Fiber.Id] =
val fiberId: USTM[Fiber.Id] =
ZSTM.fiberId

/**
Expand Down Expand Up @@ -120,7 +120,7 @@ object STM {
/**
* @see See [[zio.stm.ZSTM.fromFunction]]
*/
def fromFunction[A](f: Any => A): STM[Nothing, A] =
def fromFunction[A](f: Any => A): USTM[A] =
ZSTM.fromFunction(f)

/**
Expand All @@ -138,13 +138,13 @@ object STM {
/**
* @see See [[zio.stm.ZSTM.fromTry]]
*/
def fromTry[A](a: => Try[A]): STM[Throwable, A] =
def fromTry[A](a: => Try[A]): TaskSTM[A] =
ZSTM.fromTry(a)

/**
* @see See [[zio.stm.ZSTM.identity]]
*/
def identity: STM[Nothing, Any] = ZSTM.identity
def identity: USTM[Any] = ZSTM.identity

/**
* @see See [[zio.stm.ZSTM.ifM]]
Expand All @@ -161,7 +161,7 @@ object STM {
/**
* @see See [[zio.stm.ZSTM.left]]
*/
def left[A](a: => A): STM[Nothing, Either[A, Nothing]] =
def left[A](a: => A): USTM[Either[A, Nothing]] =
ZSTM.left(a)

/**
Expand Down Expand Up @@ -208,13 +208,7 @@ object STM {
/**
* @see See [[zio.stm.ZSTM.none]]
*/
val none: STM[Nothing, Option[Nothing]] = ZSTM.none

/**
* @see See [[zio.stm.ZSTM.partial]]
*/
def partial[A](a: => A): STM[Throwable, A] =
ZSTM.partial(a)
val none: USTM[Option[Nothing]] = ZSTM.none

/**
* @see See [[zio.stm.ZSTM.partition]]
Expand Down Expand Up @@ -246,25 +240,25 @@ object STM {
/**
* @see See [[zio.stm.ZSTM.retry]]
*/
val retry: STM[Nothing, Nothing] =
val retry: USTM[Nothing] =
ZSTM.retry

/**
* @see See [[zio.stm.ZSTM.right]]
*/
def right[A](a: => A): STM[Nothing, Either[Nothing, A]] =
def right[A](a: => A): USTM[Either[Nothing, A]] =
ZSTM.right(a)

/**
* @see See [[zio.stm.ZSTM.some]]
*/
def some[A](a: => A): STM[Nothing, Option[A]] =
def some[A](a: => A): USTM[Option[A]] =
ZSTM.some(a)

/**
* @see See [[zio.stm.ZSTM.succeed]]
*/
def succeed[A](a: => A): STM[Nothing, A] =
def succeed[A](a: => A): USTM[A] =
ZSTM.succeed(a)

/**
Expand All @@ -276,7 +270,7 @@ object STM {
/**
* @see See [[zio.stm.ZSTM.unit]]
*/
val unit: STM[Nothing, Unit] =
val unit: USTM[Unit] =
ZSTM.unit

/**
Expand Down Expand Up @@ -317,6 +311,6 @@ object STM {
*/
def whenM[E](b: STM[E, Boolean])(stm: => STM[E, Any]): STM[E, Unit] = ZSTM.whenM(b)(stm)

private[zio] def succeedNow[A](a: A): STM[Nothing, A] =
private[zio] def succeedNow[A](a: A): USTM[A] =
ZSTM.succeedNow(a)
}
54 changes: 27 additions & 27 deletions core/shared/src/main/scala/zio/stm/TArray.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ final class TArray[A] private[stm] (private[stm] val array: Array[TRef[A]]) exte
/**
* Extracts value from ref in array.
*/
def apply(index: Int): STM[Nothing, A] =
def apply(index: Int): USTM[A] =
if (0 <= index && index < array.length) array(index).get
else STM.die(new ArrayIndexOutOfBoundsException(index))

/**
* Finds the result of applying a partial function to the first value in its domain.
*/
def collectFirst[B](pf: PartialFunction[A, B]): STM[Nothing, Option[B]] =
def collectFirst[B](pf: PartialFunction[A, B]): USTM[Option[B]] =
find(pf.isDefinedAt).map(_.map(pf))

/**
Expand All @@ -47,12 +47,12 @@ final class TArray[A] private[stm] (private[stm] val array: Array[TRef[A]]) exte
/**
* Determine if the array contains a specified value.
*/
def contains(a: A): STM[Nothing, Boolean] = exists(_ == a)
def contains(a: A): USTM[Boolean] = exists(_ == a)

/**
* Count the values in the array matching a predicate.
*/
def count(p: A => Boolean): STM[Nothing, Int] =
def count(p: A => Boolean): USTM[Int] =
fold(0)((n, a) => if (p(a)) n + 1 else n)

/**
Expand All @@ -64,7 +64,7 @@ final class TArray[A] private[stm] (private[stm] val array: Array[TRef[A]]) exte
/**
* Determine if the array contains a value satisfying a predicate.
*/
def exists(p: A => Boolean): STM[Nothing, Boolean] = find(p).map(_.isDefined)
def exists(p: A => Boolean): USTM[Boolean] = find(p).map(_.isDefined)

/**
* Determine if the array contains a value satisfying a transactional predicate.
Expand All @@ -75,7 +75,7 @@ final class TArray[A] private[stm] (private[stm] val array: Array[TRef[A]]) exte
/**
* Find the first element in the array matching a predicate.
*/
def find(p: A => Boolean): STM[Nothing, Option[A]] =
def find(p: A => Boolean): USTM[Option[A]] =
if (array.isEmpty) STM.succeedNow(None)
else
array.head.get.flatMap { a =>
Expand All @@ -86,7 +86,7 @@ final class TArray[A] private[stm] (private[stm] val array: Array[TRef[A]]) exte
/**
* Find the last element in the array matching a predicate.
*/
def findLast(p: A => Boolean): STM[Nothing, Option[A]] =
def findLast(p: A => Boolean): USTM[Option[A]] =
new TArray(array.reverse).find(p)

/**
Expand All @@ -111,13 +111,13 @@ final class TArray[A] private[stm] (private[stm] val array: Array[TRef[A]]) exte
/**
* The first entry of the array, if it exists.
*/
def firstOption: STM[Nothing, Option[A]] =
def firstOption: USTM[Option[A]] =
if (array.isEmpty) STM.succeedNow(None) else array.head.get.map(Some(_))

/**
* Atomically folds using a pure function.
*/
def fold[Z](acc: Z)(op: (Z, A) => Z): STM[Nothing, Z] =
def fold[Z](acc: Z)(op: (Z, A) => Z): USTM[Z] =
if (array.isEmpty) STM.succeedNow(acc)
else array.head.get.flatMap(a => new TArray(array.tail).fold(op(acc, a))(op))

Expand All @@ -133,7 +133,7 @@ final class TArray[A] private[stm] (private[stm] val array: Array[TRef[A]]) exte
* Atomically evaluate the conjunction of a predicate across the members
* of the array.
*/
def forall(p: A => Boolean): STM[Nothing, Boolean] = exists(a => !p(a)).map(!_)
def forall(p: A => Boolean): USTM[Boolean] = exists(a => !p(a)).map(!_)

/**
* Atomically evaluate the conjunction of a transactional predicate across
Expand All @@ -152,26 +152,26 @@ final class TArray[A] private[stm] (private[stm] val array: Array[TRef[A]]) exte
* Get the first index of a specific value in the array or -1 if it does
* not occur.
*/
def indexOf(a: A): STM[Nothing, Int] = indexOf(a, 0)
def indexOf(a: A): USTM[Int] = indexOf(a, 0)

/**
* Get the first index of a specific value in the array, starting at a specific
* index, or -1 if it does not occur.
*/
def indexOf(a: A, from: Int): STM[Nothing, Int] = indexWhere(_ == a, from)
def indexOf(a: A, from: Int): USTM[Int] = indexWhere(_ == a, from)

/**
* Get the index of the first entry in the array matching a predicate.
*/
def indexWhere(p: A => Boolean): STM[Nothing, Int] = indexWhere(p, 0)
def indexWhere(p: A => Boolean): USTM[Int] = indexWhere(p, 0)

/**
* Get the index of the first entry in the array, starting at a specific index,
* matching a predicate.
*/
def indexWhere(p: A => Boolean, from: Int): STM[Nothing, Int] = {
def indexWhere(p: A => Boolean, from: Int): USTM[Int] = {
val len = array.length
def forIndex(i: Int): STM[Nothing, Int] =
def forIndex(i: Int): USTM[Int] =
if (i >= len) STM.succeedNow(-1)
else apply(i).flatMap(a => if (p(a)) STM.succeedNow(i) else forIndex(i + 1))

Expand Down Expand Up @@ -200,15 +200,15 @@ final class TArray[A] private[stm] (private[stm] val array: Array[TRef[A]]) exte
/**
* Get the last index of a specific value in the array or -1 if it does not occur.
*/
def lastIndexOf(a: A): STM[Nothing, Int] =
def lastIndexOf(a: A): USTM[Int] =
if (array.isEmpty) STM.succeedNow(-1) else lastIndexOf(a, array.length - 1)

/**
* Get the first index of a specific value in the array, bounded above by a
* specific index, or -1 if it does not occur.
*/
def lastIndexOf(a: A, end: Int): STM[Nothing, Int] = {
def forIndex(i: Int): STM[Nothing, Int] =
def lastIndexOf(a: A, end: Int): USTM[Int] = {
def forIndex(i: Int): USTM[Int] =
if (i < 0) STM.succeedNow(-1)
else apply(i).flatMap(ai => if (ai == a) STM.succeedNow(i) else forIndex(i - 1))

Expand All @@ -218,25 +218,25 @@ final class TArray[A] private[stm] (private[stm] val array: Array[TRef[A]]) exte
/**
* The last entry in the array, if it exists.
*/
def lastOption: STM[Nothing, Option[A]] =
def lastOption: USTM[Option[A]] =
if (array.isEmpty) STM.succeedNow(None) else array.last.get.map(Some(_))

/**
* Atomically compute the greatest element in the array, if it exists.
*/
def maxOption(implicit ord: Ordering[A]): STM[Nothing, Option[A]] =
def maxOption(implicit ord: Ordering[A]): USTM[Option[A]] =
reduceOption((acc, a) => if (ord.gt(a, acc)) a else acc)

/**
* Atomically compute the least element in the array, if it exists.
*/
def minOption(implicit ord: Ordering[A]): STM[Nothing, Option[A]] =
def minOption(implicit ord: Ordering[A]): USTM[Option[A]] =
reduceOption((acc, a) => if (ord.lt(a, acc)) a else acc)

/**
* Atomically reduce the array, if non-empty, by a binary operator.
*/
def reduceOption(op: (A, A) => A): STM[Nothing, Option[A]] =
def reduceOption(op: (A, A) => A): USTM[Option[A]] =
if (array.isEmpty) STM.succeedNow(None)
else
array.head.get
Expand All @@ -257,7 +257,7 @@ final class TArray[A] private[stm] (private[stm] val array: Array[TRef[A]]) exte
/**
* Atomically updates all elements using a pure function.
*/
def transform(f: A => A): STM[Nothing, Unit] =
def transform(f: A => A): USTM[Unit] =
array.indices.foldLeft(STM.succeedNow(())) {
case (tx, idx) => array(idx).update(f) *> tx
}
Expand All @@ -275,7 +275,7 @@ final class TArray[A] private[stm] (private[stm] val array: Array[TRef[A]]) exte
/**
* Updates element in the array with given function.
*/
def update(index: Int, fn: A => A): STM[Nothing, Unit] =
def update(index: Int, fn: A => A): USTM[Unit] =
if (0 <= index && index < array.length) array(index).update(fn)
else STM.die(new ArrayIndexOutOfBoundsException(index))

Expand All @@ -297,16 +297,16 @@ object TArray {
/**
* Makes a new `TArray` that is initialized with specified values.
*/
def make[A](data: A*): STM[Nothing, TArray[A]] = fromIterable(data)
def make[A](data: A*): USTM[TArray[A]] = fromIterable(data)

/**
* Makes an empty `TArray`.
*/
def empty[A]: STM[Nothing, TArray[A]] = fromIterable(Nil)
def empty[A]: USTM[TArray[A]] = fromIterable(Nil)

/**
* Makes a new `TArray` initialized with provided iterable.
*/
def fromIterable[A](data: Iterable[A]): STM[Nothing, TArray[A]] =
def fromIterable[A](data: Iterable[A]): USTM[TArray[A]] =
STM.foreach(data)(TRef.make(_)).map(list => new TArray(list.toArray))
}
Loading