Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
53 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package utils | ||
|
||
import com.twitter.{util => twitter} | ||
import scala.concurrent.{ExecutionContext, Promise, Future} | ||
import scala.util.{Failure, Success, Try} | ||
import language.implicitConversions | ||
|
||
|
||
/** | ||
* http://stackoverflow.com/questions/30317473/convert-scala-future-to-twitter-future | ||
* */ | ||
object TwitterConverters { | ||
implicit def scalaToTwitterTry[T](t: Try[T]): twitter.Try[T] = t match { | ||
case Success(r) => twitter.Return(r) | ||
case Failure(ex) => twitter.Throw(ex) | ||
} | ||
|
||
implicit def twitterToScalaTry[T](t: twitter.Try[T]): Try[T] = t match { | ||
case twitter.Return(r) => Success(r) | ||
case twitter.Throw(ex) => Failure(ex) | ||
} | ||
|
||
implicit def scalaToTwitterFuture[T](f: Future[T])(implicit ec: ExecutionContext): twitter.Future[T] = { | ||
val promise = twitter.Promise[T]() | ||
f.onComplete(promise update _) | ||
promise | ||
} | ||
|
||
implicit def twitterToScalaFuture[T](f: twitter.Future[T]): Future[T] = { | ||
val promise = Promise[T]() | ||
f.respond(promise complete _) | ||
promise.future | ||
} | ||
} |