restore behavior of Future.as[T] and .asSilently[T] (fixes #1088)
- implement both methods equivalently directly on Future - remove implicit conversion futureToAnyOptionAsTypedOption - update docs accordingly
This commit is contained in:
parent
f894ae1818
commit
cdae21e07c
3 changed files with 36 additions and 13 deletions
|
|
@ -377,6 +377,37 @@ sealed trait Future[+T] extends japi.Future[T] {
|
|||
*/
|
||||
def await(atMost: Duration): Future[T]
|
||||
|
||||
/**
|
||||
* Await completion of this Future and return its value if it conforms to A's
|
||||
* erased type. Will throw a ClassCastException if the value does not
|
||||
* conform, or any exception the Future was completed with. Will return None
|
||||
* in case of a timeout.
|
||||
*/
|
||||
def as[A](implicit m: Manifest[A]): Option[A] = {
|
||||
try await catch { case _: FutureTimeoutException ⇒ }
|
||||
value match {
|
||||
case None ⇒ None
|
||||
case Some(Left(ex)) ⇒ throw ex
|
||||
case Some(Right(v)) ⇒ Some(BoxedType(m.erasure).cast(v).asInstanceOf[A])
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Await completion of this Future and return its value if it conforms to A's
|
||||
* erased type, None otherwise. Will throw any exception the Future was
|
||||
* completed with. Will return None in case of a timeout.
|
||||
*/
|
||||
def asSilently[A](implicit m: Manifest[A]): Option[A] = {
|
||||
try await catch { case _: FutureTimeoutException ⇒ }
|
||||
value match {
|
||||
case None ⇒ None
|
||||
case Some(Left(ex)) ⇒ throw ex
|
||||
case Some(Right(v)) ⇒
|
||||
try Some(BoxedType(m.erasure).cast(v).asInstanceOf[A])
|
||||
catch { case _: ClassCastException ⇒ None }
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests whether this Future has been completed.
|
||||
*/
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue