@@ -118,25 +118,25 @@ class CancellableResult {
118118
119119 // / Return the result kind this \c CancellableResult represents: success,
120120 // / failure or cancelled.
121- CancellableResultKind getKind () { return Kind; }
121+ CancellableResultKind getKind () const { return Kind; }
122122
123123 // / Assuming that the result represents success, return the underlying result
124124 // / value.
125- ResultType &getResult () {
125+ const ResultType &getResult () const {
126126 assert (getKind () == CancellableResultKind::Success);
127127 return Result;
128128 }
129129
130130 // / Assuming that the result represents success, retrieve members of the
131131 // / underlying result value.
132- ResultType *operator ->() { return &getResult (); }
132+ const ResultType *operator ->() { return &getResult (); }
133133
134134 // / Assuming that the result represents success, return the underlying result
135135 // / value.
136- ResultType &operator *() { return getResult (); }
136+ const ResultType &operator *() { return getResult (); }
137137
138138 // / Assuming that the result represents a failure, return the error message.
139- std::string getError () {
139+ std::string getError () const {
140140 assert (getKind () == CancellableResultKind::Failure);
141141 return Error;
142142 }
@@ -152,7 +152,7 @@ class CancellableResult {
152152 template <typename NewResultType>
153153 void
154154 mapAsync (llvm::function_ref<
155- void (ResultType &,
155+ void (const ResultType &,
156156 llvm::function_ref<void (CancellableResult<NewResultType>)>)>
157157 Transform,
158158 llvm::function_ref<void(CancellableResult<NewResultType>)> Handle) {
@@ -170,6 +170,24 @@ class CancellableResult {
170170 break ;
171171 }
172172 }
173+
174+ // / If the result represents success, invoke \p Transform to create a success
175+ // / result containing new backing data.
176+ // /
177+ // / If the result represents error or cancelled, propagate that kind without
178+ // / modification.
179+ template <typename NewResultType>
180+ CancellableResult<NewResultType>
181+ map (llvm::function_ref<NewResultType(const ResultType &)> Transform) {
182+ switch (getKind ()) {
183+ case CancellableResultKind::Success:
184+ return CancellableResult<NewResultType>::success (Transform (getResult ()));
185+ case CancellableResultKind::Failure:
186+ return CancellableResult<NewResultType>::failure (getError ());
187+ case CancellableResultKind::Cancelled:
188+ return CancellableResult<NewResultType>::cancelled ();
189+ }
190+ }
173191};
174192
175193} // namespace ide
0 commit comments