Skip to content

Commit

Permalink
optimize RefinedTypeOps.unapply
Browse files Browse the repository at this point in the history
  • Loading branch information
xuwei-k committed Jan 18, 2025
1 parent 2dbc9b0 commit 8aed363
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ class RefinedTypeOps[FTP, T](implicit rt: RefinedType.AuxT[FTP, T]) extends Seri
def from(t: T): Either[String, FTP] =
rt.refine(t)

def unapply(t: T): Option[FTP] =
from(t).toOption
def unapply(t: T): Option[FTP] = {
val res = rt.validate.validate(t)
if (res.isPassed) Some(rt.refType.unsafeWrap(t).asInstanceOf[FTP])
else None
}

def unsafeFrom(t: T): FTP =
rt.unsafeRefine(t)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,11 @@ class RefinedTypeOps[FTP, T](implicit rt: RefinedType.AuxT[FTP, T]) extends Seri
def from(t: T): Either[String, FTP] =
rt.refine(t)

def unapply(t: T): Option[FTP] =
from(t).toOption
def unapply(t: T): Option[FTP] = {
val res = rt.validate.validate(t)
if (res.isPassed) Some(rt.refType.unsafeWrap(t).asInstanceOf[FTP])
else None
}

def unsafeFrom(t: T): FTP =
rt.unsafeRefine(t)
Expand Down

0 comments on commit 8aed363

Please sign in to comment.