@@ -227,3 +227,75 @@ pub enum Variant<U, T> {
227227 ///Raw bits
228228 Res ( U ) ,
229229}
230+
231+ use Variant :: * ;
232+ impl < U , T > Variant < U , T > {
233+ /// Check if the variant is expected
234+ pub fn is_value ( & self ) -> bool {
235+ match self {
236+ Val ( _) => true ,
237+ Res ( _) => false ,
238+ }
239+ }
240+
241+ /// Check if the variant is not expected
242+ pub fn is_reserved ( & self ) -> bool {
243+ match self {
244+ Val ( _) => false ,
245+ Res ( _) => true ,
246+ }
247+ }
248+
249+ /// Moves the value `v` out of the `Variant` if it is `Val(v)`.
250+ ///
251+ /// Panics if the self value equals `Res`
252+ #[ inline]
253+ pub fn unwrap ( self ) -> T {
254+ match self {
255+ Val ( v) => v,
256+ Res ( _) => panic ! ( "Unexpected variant" ) ,
257+ }
258+ }
259+
260+ /// Returns the contained value or a default
261+ #[ inline]
262+ pub fn unwrap_or ( self , def : T ) -> T {
263+ match self {
264+ Val ( v) => v,
265+ Res ( _) => def,
266+ }
267+ }
268+
269+ /// Returns the contained value or computes it from a closure
270+ #[ inline]
271+ pub fn unwrap_or_else < F : FnOnce ( U ) -> T > ( self , f : F ) -> T {
272+ match self {
273+ Val ( v) => v,
274+ Res ( u) => f ( u) ,
275+ }
276+ }
277+
278+ /// Unwraps a result, yielding the content of an `Val`.
279+ ///
280+ /// Panics if the value is an `Res`, with a panic message including the
281+ /// passed message, and the content of the `Res`.
282+ pub fn expect ( self , msg : & ' static str ) -> T {
283+ match self {
284+ Val ( v) => v,
285+ Res ( _) => panic ! ( msg) ,
286+ }
287+ }
288+ }
289+
290+ impl < U , T > Variant < U , T >
291+ where
292+ T : Into < U >
293+ {
294+ /// Get raw bits
295+ pub fn to_bits ( ) -> U {
296+ match self {
297+ Val ( v) => v. into ( ) ,
298+ Res ( u) => u,
299+ }
300+ }
301+ }
0 commit comments