File Coverage

blib/lib/Sub/HandlesVia/XS.pm
Criterion Covered Total %
statement 30 61 49.1
branch 8 50 16.0
condition 2 18 11.1
subroutine 8 11 72.7
pod 0 3 0.0
total 48 143 33.5


line stmt bran cond sub pod time code
1 45     45   10173188 use 5.008008;
  45         213  
2 45     45   295 use strict;
  45         115  
  45         1643  
3 45     45   241 use warnings;
  45         131  
  45         4136  
4              
5             package Sub::HandlesVia::XS;
6              
7 45     45   22006 use Types::Common -is, -types;
  45         13823455  
  45         607  
8              
9             BEGIN {
10 45     45   1347209 our $AUTHORITY = 'cpan:TOBYINK';
11 45         181 our $VERSION = '0.003004';
12            
13 45         555 require XSLoader;
14 45         82167 __PACKAGE__->XSLoader::load( $VERSION );
15             };
16              
17             sub ArraySource {
18 0     0 0 0 my ( $class, $string ) = @_;
19 0         0 my $method = 'ARRAY_SRC_' . uc $string;
20 0         0 return $class->$method;
21             }
22              
23             sub ReturnPattern {
24 0     0 0 0 my ( $class, $string ) = @_;
25 0         0 my $method = 'SHOULD_RETURN_' . uc $string;
26 0         0 return $class->$method;
27             }
28              
29             sub TypeInfo {
30 6     6 0 859913 my ( $class, $type ) = @_;
31            
32 6 50       36 die "This method returns a ( coderef, flags ) pair" unless wantarray;
33            
34 6         12 my $coderef;
35 6 50 33     81 if ( is_Object $type and $type->can('compiled_check') ) {
    0 0        
    0          
    0          
36 6         105 $coderef = $type->compiled_check;
37             }
38             elsif ( is_Object $type and $type->can('check') ) {
39 0 0   0   0 $coderef = sub { $type->check( @_ ? $_[0] : $_ ) };
  0         0  
40             }
41             elsif ( is_CodeRef $type ) {
42 0         0 $coderef = $type;
43             }
44             elsif ( is_CodeLike $type ) {
45 0         0 $coderef = \&$type;
46             }
47             else {
48 0         0 die "Not a type?";
49             }
50            
51 6         229 my $type_flags = _type_to_number( $type );
52 6         32 return ( $coderef, $type_flags );
53             }
54              
55             sub _type_to_number {
56 6     6   20 my ( $given_type, $no_recurse ) = @_;
57            
58 6 50       31 if ( is_TypeTiny $given_type ) {
59 6         115 my $type = $given_type->find_constraining_type;
60            
61 6 50 33     140 if ( $type == Any or $type == Item ) {
    50 0        
    50 0        
    50 0        
    50          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
    0          
62 0         0 return TYPE_BASE_ANY;
63             }
64             elsif ( $type == Defined ) {
65 0         0 return TYPE_BASE_DEFINED;
66             }
67             elsif ( $type == Ref ) {
68 0         0 return TYPE_BASE_REF;
69             }
70             elsif ( $type == Bool ) {
71 0         0 return TYPE_BASE_BOOL;
72             }
73             elsif ( $type == Int ) {
74 6         28688 return TYPE_BASE_INT;
75             }
76             elsif ( $type == PositiveOrZeroInt ) {
77 0           return TYPE_BASE_PZINT;
78             }
79             elsif ( $type == Num ) {
80 0           return TYPE_BASE_NUM;
81             }
82             elsif ( $type == PositiveOrZeroNum ) {
83 0           return TYPE_BASE_PZNUM;
84             }
85             elsif ( $type == Str ) {
86 0           return TYPE_BASE_STR;
87             }
88             elsif ( $type == NonEmptyStr ) {
89 0           return TYPE_BASE_NESTR;
90             }
91             elsif ( $type == ClassName ) {
92 0           return TYPE_BASE_CLASSNAME;
93             }
94             elsif ( $type == Object ) {
95 0           return TYPE_BASE_OBJECT;
96             }
97             elsif ( $type == ScalarRef ) {
98 0           return TYPE_BASE_SCALARREF;
99             }
100             elsif ( $type == CodeRef ) {
101 0           return TYPE_BASE_CODEREF;
102             }
103             elsif ( $type == ArrayRef ) {
104 0           return TYPE_ARRAYREF;
105             }
106             elsif ( $type == HashRef ) {
107 0           return TYPE_HASHREF;
108             }
109 0           elsif ( $type->is_parameterized and @{ $type->parameters } == 1 and (
110             $type->parameterized_from == ArrayRef
111             or $type->parameterized_from == HashRef
112             ) ) {
113 0           my $container_type = $type->parameterized_from;
114 0           my $element_type = $type->type_parameter;
115 0 0         return _type_to_number( $container_type, 1 ) | _type_to_number( $element_type, 1 ) unless $no_recurse;
116             }
117             }
118            
119 0           return TYPE_OTHER;
120             }
121              
122 45     45   453 no Types::Common;
  45         90  
  45         536  
123              
124             1;
125              
126             __END__