File tree Expand file tree Collapse file tree 2 files changed +63
-0
lines changed Expand file tree Collapse file tree 2 files changed +63
-0
lines changed Original file line number Diff line number Diff line change @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77
88## [ Unreleased]  
99
10+ ### Added  
11+ 
12+ -  Option-like API for ` generic::Variant ` 
13+ 
1014## [ v0.16.1]  - 2019-08-17 
1115
1216### Fixed  
Original file line number Diff line number Diff line change @@ -227,3 +227,62 @@ pub enum Variant<U, T> {
227227    ///Raw bits 
228228Res ( 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+ } 
 
 
   
 
     
   
   
          
    
    
     
    
      
     
     
    You can’t perform that action at this time.
  
 
    
  
    
      
        
     
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments