@@ -513,12 +513,6 @@ template <> class SPSSerializationTraits<SPSString, std::string_view> {
513513// / SPS tag type for errors.
514514class SPSError ;
515515
516- // / SPS tag type for expecteds, which are either a T or a string representing
517- // / an error.
518- template <typename SPSTagT> class SPSExpected ;
519-
520- namespace detail {
521-
522516// / Helper type for serializing Errors.
523517// /
524518// / llvm::Errors are move-only, and not inspectable except by consuming them.
@@ -529,139 +523,141 @@ namespace detail {
529523// / The SPSSerializableError type is a helper that can be
530524// / constructed from an llvm::Error, but inspected more than once.
531525struct SPSSerializableError {
532- bool HasError = false ;
533- std::string ErrMsg;
534- };
535-
536- // / Helper type for serializing Expected<T>s.
537- // /
538- // / See SPSSerializableError for more details.
539- // /
540- // FIXME: Use std::variant for storage once we have c++17.
541- template <typename T> struct SPSSerializableExpected {
542- bool HasValue = false ;
543- T Value{};
544- std::string ErrMsg;
545- };
546-
547- inline SPSSerializableError toSPSSerializable (Error Err) {
548- if (Err)
549- return {true , toString (std::move (Err))};
550- return {false , {}};
551- }
552-
553- inline Error fromSPSSerializable (SPSSerializableError BSE) {
554- if (BSE.HasError )
555- return make_error<StringError>(BSE.ErrMsg );
556- return Error::success ();
557- }
558-
559- template <typename T>
560- SPSSerializableExpected<T> toSPSSerializable (Expected<T> E) {
561- if (E)
562- return {true , std::move (*E), {}};
563- else
564- return {false , {}, toString (E.takeError ())};
565- }
526+ SPSSerializableError () = default ;
527+ SPSSerializableError (Error Err) {
528+ if (Err)
529+ Msg = toString (std::move (Err));
530+ }
566531
567- template <typename T>
568- Expected<T> fromSPSSerializable (SPSSerializableExpected<T> BSE) {
569- if (BSE.HasValue )
570- return std::move (BSE.Value );
571- else
572- return make_error<StringError>(BSE.ErrMsg );
573- }
532+ Error toError () {
533+ if (Msg)
534+ return make_error<StringError>(std::move (*Msg));
535+ return Error::success ();
536+ }
574537
575- } // namespace detail
538+ std::optional<std::string> Msg;
539+ };
576540
577- // / Serialize to a SPSError from a detail::SPSSerializableError.
578- template <>
579- class SPSSerializationTraits <SPSError, detail::SPSSerializableError> {
541+ template <> class SPSSerializationTraits <SPSError, SPSSerializableError> {
580542public:
581- static size_t size (const detail:: SPSSerializableError &BSE ) {
582- size_t Size = SPSArgList< bool >:: size (BSE. HasError );
583- if (BSE. HasError )
584- Size += SPSArgList<SPSString>:: size (BSE. ErrMsg );
585- return Size ;
543+ static size_t size (const SPSSerializableError &E ) {
544+ if (E. Msg )
545+ return SPSArgList< bool , SPSString>:: size ( true , *E. Msg );
546+ else
547+ return SPSArgList< bool >:: size ( false ) ;
586548 }
587549
588- static bool serialize (SPSOutputBuffer &OB,
589- const detail::SPSSerializableError &BSE) {
590- if (!SPSArgList<bool >::serialize (OB, BSE.HasError ))
550+ static bool serialize (SPSOutputBuffer &OB, const SPSSerializableError &E) {
551+ if (E.Msg )
552+ return SPSArgList<bool , SPSString>::serialize (OB, true , *E.Msg );
553+ else
554+ return SPSArgList<bool >::serialize (OB, false );
555+ }
556+
557+ static bool deserialize (SPSInputBuffer &IB, SPSSerializableError &E) {
558+ bool HasError = false ;
559+ if (!SPSArgList<bool >::deserialize (IB, HasError))
591560 return false ;
592- if (BSE.HasError )
593- if (!SPSArgList<SPSString>::serialize (OB, BSE.ErrMsg ))
561+ if (HasError) {
562+ std::string Msg;
563+ if (!SPSArgList<SPSString>::deserialize (IB, Msg))
594564 return false ;
565+ E.Msg = std::move (Msg);
566+ } else
567+ E.Msg = std::nullopt ;
595568 return true ;
596569 }
570+ };
597571
598- static bool deserialize (SPSInputBuffer &IB,
599- detail::SPSSerializableError &BSE) {
600- if (!SPSArgList<bool >::deserialize (IB, BSE.HasError ))
601- return false ;
572+ // / SPS tag type for expecteds, which are either a T or a string representing
573+ // / an error.
574+ template <typename SPSTagT> class SPSExpected ;
602575
603- if (!BSE.HasError )
604- return true ;
576+ // / Helper type for serializing Expected<T>s.
577+ // /
578+ // / See SPSSerializableError for more details.
579+ template <typename T> struct SPSSerializableExpected {
580+ SPSSerializableExpected () = default ;
581+ SPSSerializableExpected (Expected<T> E) {
582+ if (E)
583+ Val = decltype (Val)(std::in_place_index<0 >, std::move (*E));
584+ else
585+ Val = decltype (Val)(std::in_place_index<1 >, toString (E.takeError ()));
586+ }
587+ SPSSerializableExpected (Error E) {
588+ assert (E && " Cannot create Expected from Error::success()" );
589+ Val = decltype (Val)(std::in_place_index<1 >, toString (std::move (E)));
590+ }
605591
606- return SPSArgList<SPSString>::deserialize (IB, BSE.ErrMsg );
592+ Expected<T> toExpected () {
593+ if (Val.index () == 0 )
594+ return Expected<T>(std::move (std::get<0 >(Val)));
595+ return Expected<T>(make_error<StringError>(std::move (std::get<1 >(Val))));
607596 }
597+
598+ std::variant<T, std::string> Val{std::in_place_index<0 >, T ()};
608599};
609600
610- // / Serialize to a SPSExpected<SPSTagT> from a
611- // / detail::SPSSerializableExpected<T>.
601+ template <typename T>
602+ SPSSerializableExpected<T> toSPSSerializableExpected (Expected<T> E) {
603+ return std::move (E);
604+ }
605+
606+ template <typename T>
607+ SPSSerializableExpected<T> toSPSSerializableExpected (Error E) {
608+ return std::move (E);
609+ }
610+
612611template <typename SPSTagT, typename T>
613- class SPSSerializationTraits <SPSExpected<SPSTagT>,
614- detail::SPSSerializableExpected<T>> {
612+ class SPSSerializationTraits <SPSExpected<SPSTagT>, SPSSerializableExpected<T>> {
615613public:
616- static size_t size (const detail::SPSSerializableExpected<T> &BSE) {
617- size_t Size = SPSArgList<bool >::size (BSE.HasValue );
618- if (BSE.HasValue )
619- Size += SPSArgList<SPSTagT>::size (BSE.Value );
614+ static size_t size (const SPSSerializableExpected<T> &E) {
615+ if (E.Val .index () == 0 )
616+ return SPSArgList<bool , SPSTagT>::size (true , std::get<0 >(E.Val ));
620617 else
621- Size += SPSArgList<SPSString>::size (BSE.ErrMsg );
622- return Size;
618+ return SPSArgList<bool , SPSString>::size (false , std::get<1 >(E.Val ));
623619 }
624620
625621 static bool serialize (SPSOutputBuffer &OB,
626- const detail::SPSSerializableExpected<T> &BSE) {
627- if (!SPSArgList<bool >::serialize (OB, BSE.HasValue ))
628- return false ;
629-
630- if (BSE.HasValue )
631- return SPSArgList<SPSTagT>::serialize (OB, BSE.Value );
632-
633- return SPSArgList<SPSString>::serialize (OB, BSE.ErrMsg );
622+ const SPSSerializableExpected<T> &E) {
623+ if (E.Val .index () == 0 )
624+ return SPSArgList<bool , SPSTagT>::serialize (OB, true , std::get<0 >(E.Val ));
625+ else
626+ return SPSArgList<bool , SPSString>::serialize (OB, false ,
627+ std::get<1 >(E.Val ));
634628 }
635629
636- static bool deserialize (SPSInputBuffer &IB,
637- detail::SPSSerializableExpected<T> &BSE) {
638- if (!SPSArgList<bool >::deserialize (IB, BSE. HasValue ))
630+ static bool deserialize (SPSInputBuffer &IB, SPSSerializableExpected<T> &E) {
631+ bool HasValue = false ;
632+ if (!SPSArgList<bool >::deserialize (IB, HasValue))
639633 return false ;
640-
641- if (BSE.HasValue )
642- return SPSArgList<SPSTagT>::deserialize (IB, BSE.Value );
643-
644- return SPSArgList<SPSString>::deserialize (IB, BSE.ErrMsg );
634+ if (HasValue) {
635+ T Val;
636+ if (!SPSArgList<SPSTagT>::deserialize (IB, Val))
637+ return false ;
638+ E.Val = decltype (E.Val ){std::in_place_index<0 >, std::move (Val)};
639+ } else {
640+ std::string Msg;
641+ if (!SPSArgList<SPSString>::deserialize (IB, Msg))
642+ return false ;
643+ E.Val = decltype (E.Val ){std::in_place_index<1 >, std::move (Msg)};
644+ }
645+ return true ;
645646 }
646647};
647648
648- // / Serialize to a SPSExpected<SPSTagT> from a detail:: SPSSerializableError.
649+ // / Serialize to a SPSExpected<SPSTagT> from a SPSSerializableError.
649650template <typename SPSTagT>
650- class SPSSerializationTraits <SPSExpected<SPSTagT>,
651- detail::SPSSerializableError> {
651+ class SPSSerializationTraits <SPSExpected<SPSTagT>, SPSSerializableError> {
652652public:
653- static size_t size (const detail::SPSSerializableError &BSE) {
654- assert (BSE.HasError && " Cannot serialize expected from a success value" );
655- return SPSArgList<bool >::size (false ) +
656- SPSArgList<SPSString>::size (BSE.ErrMsg );
653+ static size_t size (const SPSSerializableError &SE) {
654+ assert (SE.Msg && " Cannot serialize expected from a success value" );
655+ return SPSArgList<bool , SPSString>::size (false , *SE.Msg );
657656 }
658657
659- static bool serialize (SPSOutputBuffer &OB,
660- const detail::SPSSerializableError &BSE) {
661- assert (BSE.HasError && " Cannot serialize expected from a success value" );
662- if (!SPSArgList<bool >::serialize (OB, false ))
663- return false ;
664- return SPSArgList<SPSString>::serialize (OB, BSE.ErrMsg );
658+ static bool serialize (SPSOutputBuffer &OB, const SPSSerializableError &SE) {
659+ assert (SE.Msg && " Cannot serialize expected from a success value" );
660+ return SPSArgList<bool , SPSString>::serialize (OB, false , *SE.Msg );
665661 }
666662};
667663
@@ -674,9 +670,7 @@ class SPSSerializationTraits<SPSExpected<SPSTagT>, T> {
674670 }
675671
676672 static bool serialize (SPSOutputBuffer &OB, const T &Value) {
677- if (!SPSArgList<bool >::serialize (OB, true ))
678- return false ;
679- return SPSArgList<SPSTagT>::serialize (Value);
673+ return SPSArgList<bool , SPSTagT>::serialize (OB, true , Value);
680674 }
681675};
682676
0 commit comments