fn enum_cast(
        &self,
        enum_type: CTypeId,
        enum_decl: CEnumId,      // ID of the enum declaration corresponding to the target type
        expr: CExprId,           // ID of initial C argument to cast
        val: WithStmts<P<Expr>>, // translated Rust argument to cast
        _source_ty: P<Ty>,        // source type of cast
        target_ty: P<Ty>,        // target type of cast
    ) -> WithStmts<P<Expr>> {

        // Extract the IDs of the `EnumConstant` decls underlying the enum.
        let variants = match self.ast_context.index(enum_decl).kind {
            CDeclKind::Enum { ref variants, .. } => variants,
            _ => panic!("{:?} does not point to an `enum` declaration")
        };

        match self.ast_context.index(expr).kind {
            // This is the case of finding a variable which is an `EnumConstant` of the same enum
            // we are casting to. Here, we can just remove the extraneous cast instead of generating
            // a new one.
            CExprKind::DeclRef(_, decl_id) if variants.contains(&decl_id) =>
                return val.map(|x| match x.node {
                    ast::ExprKind::Cast(ref e, _) => e.clone(),
                    _ => panic!(format!("DeclRef {:?} of enum {:?} is not cast", expr, enum_decl)),
                }),

            CExprKind::Literal(_, CLiteral::Integer(i,_)) => {
                let new_val = self.enum_for_i64(enum_type, i as i64);
                return WithStmts { stmts: val.stmts, val: new_val }
            }

            CExprKind::Unary(_, c_ast::UnOp::Negate, subexpr_id) => {
                if let &CExprKind::Literal(_, CLiteral::Integer(i,_)) = &self.ast_context[subexpr_id].kind {
                    let new_val = self.enum_for_i64(enum_type, -(i as i64));
                    return WithStmts { stmts: val.stmts, val: new_val }
                }
            }

            // In all other cases, a cast to an enum requires a `transmute` - Rust enums cannot be
            // converted into integral types as easily as C ones.
            _ => {},
        }

        val.map(|x| mk().cast_expr(x, target_ty))
    }
/// This handles translating casts when the target type in an `enum` type.
///
/// When translating variable references to `EnumConstant`'s, we always insert casts to the
/// expected type. In C, `EnumConstants` have some integral type, _not_ the enum type. However,
/// if we then immediately have a cast to convert this variable back into an enum type, we would
/// like to produce Rust with _no_ casts. This function handles this simplification.
This handles translating casts when the target type in an `enum` type.
When translating variable references to `EnumConstant`'s, we always insert casts to the
expected type. In C, `EnumConstants` have some integral type, _not_ the enum type. However,
if we then immediately have a cast to convert this variable back into an enum type, we would
like to produce Rust with _no_ casts. This function handles this simplification.
This handles translating casts when the target type in an ` enum ` type . When translating variable references to ` EnumConstant ` ' s we always insert casts to the expected type . In C ` EnumConstants ` have some integral type _not_ the enum type . However if we then immediately have a cast to convert this variable back into an enum type we would like to produce Rust with _no_ casts . This function handles this simplification .
fn enum_cast(
        &self,
        enum_type: CTypeId,
        enum_decl: CEnumId,      // ID of the enum declaration corresponding to the target type
        expr: CExprId,           // ID of initial C argument to cast
        val: WithStmts<P<Expr>>, // translated Rust argument to cast
        _source_ty: P<Ty>,        // source type of cast
        target_ty: P<Ty>,        // target type of cast
    ) -> WithStmts<P<Expr>> {
        // Extract the IDs of the `EnumConstant` decls underlying the enum.
        let variants = match self.ast_context.index(enum_decl).kind {
            CDeclKind::Enum { ref variants, .. } => variants,
            _ => panic!("{:?} does not point to an `enum` declaration")
        };
        match self.ast_context.index(expr).kind {
            // This is the case of finding a variable which is an `EnumConstant` of the same enum
            // we are casting to. Here, we can just remove the extraneous cast instead of generating
            // a new one.
            CExprKind::DeclRef(_, decl_id) if variants.contains(&decl_id) =>
                return val.map(|x| match x.node {
                    ast::ExprKind::Cast(ref e, _) => e.clone(),
                    _ => panic!(format!("DeclRef {:?} of enum {:?} is not cast", expr, enum_decl)),
                }),
            CExprKind::Literal(_, CLiteral::Integer(i,_)) => {
                let new_val = self.enum_for_i64(enum_type, i as i64);
                return WithStmts { stmts: val.stmts, val: new_val }
            }
            CExprKind::Unary(_, c_ast::UnOp::Negate, subexpr_id) => {
                if let &CExprKind::Literal(_, CLiteral::Integer(i,_)) = &self.ast_context[subexpr_id].kind {
                    let new_val = self.enum_for_i64(enum_type, -(i as i64));
                    return WithStmts { stmts: val.stmts, val: new_val }
                }
            }
            // In all other cases, a cast to an enum requires a `transmute` - Rust enums cannot be
            // converted into integral types as easily as C ones.
            _ => {},
        }
        val.map(|x| mk().cast_expr(x, target_ty))
    }
fn enum_cast ( & self , enum_type : CTypeId , enum_decl : CEnumId , // ID of the enum declaration corresponding to the target type expr : CExprId , // ID of initial C argument to cast val : WithStmts < P < Expr > > , // translated Rust argument to cast _source_ty : P < Ty > , // source type of cast target_ty : P < Ty > , // target type of cast ) -> WithStmts < P < Expr > > { // Extract the IDs of the `EnumConstant` decls underlying the enum. let variants = match self . ast_context . index ( enum_decl ) . kind { CDeclKind :: Enum { ref variants , .. } => variants , _ => panic ! ( " " ) } ; match self . ast_context . index ( expr ) . kind { // This is the case of finding a variable which is an `EnumConstant` of the same enum // we are casting to. Here, we can just remove the extraneous cast instead of generating // a new one. CExprKind :: DeclRef ( _ , decl_id ) if variants . contains ( & decl_id ) => return val . map ( | x | match x . node { ast :: ExprKind :: Cast ( ref e , _ ) => e . clone ( ) , _ => panic ! ( format ( " " expr enum_decl ) ) , } ) , CExprKind :: Literal ( _ , CLiteral :: Integer ( i , _ ) ) => { let new_val = self . enum_for_i64 ( enum_type , i as i64 ) ; return WithStmts { stmts : val . stmts , val : new_val } } CExprKind :: Unary ( _ , c_ast :: UnOp :: Negate , subexpr_id ) => { if let & CExprKind :: Literal ( _ , CLiteral :: Integer ( i , _ ) ) = & self . ast_context [ subexpr_id ] . kind { let new_val = self . enum_for_i64 ( enum_type , - ( i as i64 ) ) ; return WithStmts { stmts : val . stmts , val : new_val } } } // In all other cases, a cast to an enum requires a `transmute` - Rust enums cannot be // converted into integral types as easily as C ones. _ => { } , } val . map ( | x | mk ( ) . cast_expr ( x , target_ty ) ) }
This handles translating casts when the target type in an `enum` type.
This handles translating casts when the target type in an ` enum ` type .
&self
enum_type
CTypeId
enum_decl
CEnumId
expr
CExprId
val
WithStmts<P<Expr>>
_source_ty
P<Ty>
target_ty
P<Ty>
&self
enum_type
CTypeId
enum_decl
CEnumId
expr
CExprId
val
WithStmts<P<Expr>>
_source_ty
P<Ty>
target_ty
P<Ty>