Filename | /Users/ap13/perl5/lib/perl5/darwin-2level/Moose/Meta/TypeConstraint/Union.pm |
Statements | Executed 16 statements in 1.20ms |
Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
---|---|---|---|---|---|
1 | 1 | 1 | 365µs | 1.04ms | BEGIN@8 | Moose::Meta::TypeConstraint::Union::
1 | 1 | 1 | 18µs | 66µs | BEGIN@10 | Moose::Meta::TypeConstraint::Union::
1 | 1 | 1 | 13µs | 26µs | BEGIN@4 | Moose::Meta::TypeConstraint::Union::
1 | 1 | 1 | 11µs | 15µs | BEGIN@5 | Moose::Meta::TypeConstraint::Union::
1 | 1 | 1 | 8µs | 32µs | BEGIN@12 | Moose::Meta::TypeConstraint::Union::
1 | 1 | 1 | 8µs | 588µs | BEGIN@6 | Moose::Meta::TypeConstraint::Union::
0 | 0 | 0 | 0s | 0s | __ANON__[:136] | Moose::Meta::TypeConstraint::Union::
0 | 0 | 0 | 0s | 0s | __ANON__[:157] | Moose::Meta::TypeConstraint::Union::
0 | 0 | 0 | 0s | 0s | __ANON__[:163] | Moose::Meta::TypeConstraint::Union::
0 | 0 | 0 | 0s | 0s | __ANON__[:169] | Moose::Meta::TypeConstraint::Union::
0 | 0 | 0 | 0s | 0s | __ANON__[:16] | Moose::Meta::TypeConstraint::Union::
0 | 0 | 0 | 0s | 0s | __ANON__[:71] | Moose::Meta::TypeConstraint::Union::
0 | 0 | 0 | 0s | 0s | _actually_compile_type_constraint | Moose::Meta::TypeConstraint::Union::
0 | 0 | 0 | 0s | 0s | _inline_check | Moose::Meta::TypeConstraint::Union::
0 | 0 | 0 | 0s | 0s | can_be_inlined | Moose::Meta::TypeConstraint::Union::
0 | 0 | 0 | 0s | 0s | coercion | Moose::Meta::TypeConstraint::Union::
0 | 0 | 0 | 0s | 0s | create_child_type | Moose::Meta::TypeConstraint::Union::
0 | 0 | 0 | 0s | 0s | equals | Moose::Meta::TypeConstraint::Union::
0 | 0 | 0 | 0s | 0s | find_type_for | Moose::Meta::TypeConstraint::Union::
0 | 0 | 0 | 0s | 0s | has_coercion | Moose::Meta::TypeConstraint::Union::
0 | 0 | 0 | 0s | 0s | inline_environment | Moose::Meta::TypeConstraint::Union::
0 | 0 | 0 | 0s | 0s | is_a_type_of | Moose::Meta::TypeConstraint::Union::
0 | 0 | 0 | 0s | 0s | is_subtype_of | Moose::Meta::TypeConstraint::Union::
0 | 0 | 0 | 0s | 0s | new | Moose::Meta::TypeConstraint::Union::
0 | 0 | 0 | 0s | 0s | parent | Moose::Meta::TypeConstraint::Union::
0 | 0 | 0 | 0s | 0s | validate | Moose::Meta::TypeConstraint::Union::
Line | State ments |
Time on line |
Calls | Time in subs |
Code |
---|---|---|---|---|---|
1 | package Moose::Meta::TypeConstraint::Union; | ||||
2 | 1 | 700ns | our $VERSION = '2.1403'; | ||
3 | |||||
4 | 2 | 23µs | 2 | 39µs | # spent 26µs (13+13) within Moose::Meta::TypeConstraint::Union::BEGIN@4 which was called:
# once (13µs+13µs) by Moose::Util::TypeConstraints::BEGIN@25 at line 4 # spent 26µs making 1 call to Moose::Meta::TypeConstraint::Union::BEGIN@4
# spent 13µs making 1 call to strict::import |
5 | 2 | 22µs | 2 | 19µs | # spent 15µs (11+4) within Moose::Meta::TypeConstraint::Union::BEGIN@5 which was called:
# once (11µs+4µs) by Moose::Util::TypeConstraints::BEGIN@25 at line 5 # spent 15µs making 1 call to Moose::Meta::TypeConstraint::Union::BEGIN@5
# spent 4µs making 1 call to warnings::import |
6 | 2 | 27µs | 2 | 1.17ms | # spent 588µs (8+579) within Moose::Meta::TypeConstraint::Union::BEGIN@6 which was called:
# once (8µs+579µs) by Moose::Util::TypeConstraints::BEGIN@25 at line 6 # spent 588µs making 1 call to Moose::Meta::TypeConstraint::Union::BEGIN@6
# spent 579µs making 1 call to metaclass::import |
7 | |||||
8 | 2 | 124µs | 1 | 1.04ms | # spent 1.04ms (365µs+678µs) within Moose::Meta::TypeConstraint::Union::BEGIN@8 which was called:
# once (365µs+678µs) by Moose::Util::TypeConstraints::BEGIN@25 at line 8 # spent 1.04ms making 1 call to Moose::Meta::TypeConstraint::Union::BEGIN@8 |
9 | |||||
10 | 3 | 45µs | 3 | 90µs | # spent 66µs (18+49) within Moose::Meta::TypeConstraint::Union::BEGIN@10 which was called:
# once (18µs+49µs) by Moose::Util::TypeConstraints::BEGIN@25 at line 10 # spent 66µs making 1 call to Moose::Meta::TypeConstraint::Union::BEGIN@10
# spent 12µs making 1 call to UNIVERSAL::VERSION
# spent 11µs making 1 call to List::Util::import |
11 | |||||
12 | 2 | 941µs | 2 | 55µs | # spent 32µs (8+24) within Moose::Meta::TypeConstraint::Union::BEGIN@12 which was called:
# once (8µs+24µs) by Moose::Util::TypeConstraints::BEGIN@25 at line 12 # spent 32µs making 1 call to Moose::Meta::TypeConstraint::Union::BEGIN@12
# spent 24µs making 1 call to parent::import |
13 | |||||
14 | __PACKAGE__->meta->add_attribute('type_constraints' => ( | ||||
15 | accessor => 'type_constraints', | ||||
16 | default => sub { [] }, | ||||
17 | 1 | 10µs | 3 | 515µs | Class::MOP::_definition_context(), # spent 485µs making 1 call to Class::MOP::Mixin::HasAttributes::add_attribute
# spent 22µs making 1 call to Moose::Meta::TypeConstraint::Union::meta
# spent 8µs making 1 call to Class::MOP::_definition_context |
18 | )); | ||||
19 | |||||
20 | sub new { | ||||
21 | my ($class, %options) = @_; | ||||
22 | |||||
23 | my $name = join '|' => sort { $a cmp $b } | ||||
24 | map { $_->name } @{ $options{type_constraints} }; | ||||
25 | |||||
26 | my $self = $class->SUPER::new( | ||||
27 | name => $name, | ||||
28 | %options, | ||||
29 | ); | ||||
30 | |||||
31 | $self->_set_constraint( $self->_compiled_type_constraint ); | ||||
32 | |||||
33 | return $self; | ||||
34 | } | ||||
35 | |||||
36 | # XXX - this is a rather gross implementation of laziness for the benefit of | ||||
37 | # MX::Types. If we try to call ->has_coercion on the objects during object | ||||
38 | # construction, this does not work when defining a recursive constraint with | ||||
39 | # MX::Types. | ||||
40 | sub coercion { | ||||
41 | my $self = shift; | ||||
42 | |||||
43 | return $self->{coercion} if exists $self->{coercion}; | ||||
44 | |||||
45 | # Using any instead of grep here causes a weird error with some corner | ||||
46 | # cases when MX::Types is in use. See RT #61001. | ||||
47 | if ( grep { $_->has_coercion } @{ $self->type_constraints } ) { | ||||
48 | return $self->{coercion} = Moose::Meta::TypeCoercion::Union->new( | ||||
49 | type_constraint => $self ); | ||||
50 | } | ||||
51 | else { | ||||
52 | return $self->{coercion} = undef; | ||||
53 | } | ||||
54 | } | ||||
55 | |||||
56 | sub has_coercion { | ||||
57 | return defined $_[0]->coercion; | ||||
58 | } | ||||
59 | |||||
60 | sub _actually_compile_type_constraint { | ||||
61 | my $self = shift; | ||||
62 | |||||
63 | my @constraints = @{ $self->type_constraints }; | ||||
64 | |||||
65 | return sub { | ||||
66 | my $value = shift; | ||||
67 | foreach my $type (@constraints) { | ||||
68 | return 1 if $type->check($value); | ||||
69 | } | ||||
70 | return undef; | ||||
71 | }; | ||||
72 | } | ||||
73 | |||||
74 | sub can_be_inlined { | ||||
75 | my $self = shift; | ||||
76 | |||||
77 | # This was originally done with all() from List::MoreUtils, but that | ||||
78 | # caused some sort of bizarro parsing failure under 5.10. | ||||
79 | for my $tc ( @{ $self->type_constraints } ) { | ||||
80 | return 0 unless $tc->can_be_inlined; | ||||
81 | } | ||||
82 | |||||
83 | return 1; | ||||
84 | } | ||||
85 | |||||
86 | sub _inline_check { | ||||
87 | my $self = shift; | ||||
88 | my $val = shift; | ||||
89 | |||||
90 | return '(' | ||||
91 | . ( | ||||
92 | join ' || ', map { '(' . $_->_inline_check($val) . ')' } | ||||
93 | @{ $self->type_constraints } | ||||
94 | ) | ||||
95 | . ')'; | ||||
96 | } | ||||
97 | |||||
98 | sub inline_environment { | ||||
99 | my $self = shift; | ||||
100 | |||||
101 | return { map { %{ $_->inline_environment } } | ||||
102 | @{ $self->type_constraints } }; | ||||
103 | } | ||||
104 | |||||
105 | sub equals { | ||||
106 | my ( $self, $type_or_name ) = @_; | ||||
107 | |||||
108 | my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name); | ||||
109 | |||||
110 | return unless $other->isa(__PACKAGE__); | ||||
111 | |||||
112 | my @self_constraints = @{ $self->type_constraints }; | ||||
113 | my @other_constraints = @{ $other->type_constraints }; | ||||
114 | |||||
115 | return unless @self_constraints == @other_constraints; | ||||
116 | |||||
117 | # FIXME presort type constraints for efficiency? | ||||
118 | constraint: foreach my $constraint ( @self_constraints ) { | ||||
119 | for ( my $i = 0; $i < @other_constraints; $i++ ) { | ||||
120 | if ( $constraint->equals($other_constraints[$i]) ) { | ||||
121 | splice @other_constraints, $i, 1; | ||||
122 | next constraint; | ||||
123 | } | ||||
124 | } | ||||
125 | } | ||||
126 | |||||
127 | return @other_constraints == 0; | ||||
128 | } | ||||
129 | |||||
130 | sub parent { | ||||
131 | my $self = shift; | ||||
132 | |||||
133 | my ($first, @rest) = @{ $self->type_constraints }; | ||||
134 | |||||
135 | for my $parent ( $first->_collect_all_parents ) { | ||||
136 | return $parent if all { $_->is_a_type_of($parent) } @rest; | ||||
137 | } | ||||
138 | |||||
139 | return; | ||||
140 | } | ||||
141 | |||||
142 | sub validate { | ||||
143 | my ($self, $value) = @_; | ||||
144 | my $message; | ||||
145 | foreach my $type (@{$self->type_constraints}) { | ||||
146 | my $err = $type->validate($value); | ||||
147 | return unless defined $err; | ||||
148 | $message .= ($message ? ' and ' : '') . $err | ||||
149 | if defined $err; | ||||
150 | } | ||||
151 | return ($message . ' in (' . $self->name . ')') ; | ||||
152 | } | ||||
153 | |||||
154 | sub find_type_for { | ||||
155 | my ($self, $value) = @_; | ||||
156 | |||||
157 | return first { $_->check($value) } @{ $self->type_constraints }; | ||||
158 | } | ||||
159 | |||||
160 | sub is_a_type_of { | ||||
161 | my ($self, $type_name) = @_; | ||||
162 | |||||
163 | return all { $_->is_a_type_of($type_name) } @{ $self->type_constraints }; | ||||
164 | } | ||||
165 | |||||
166 | sub is_subtype_of { | ||||
167 | my ($self, $type_name) = @_; | ||||
168 | |||||
169 | return all { $_->is_subtype_of($type_name) } @{ $self->type_constraints }; | ||||
170 | } | ||||
171 | |||||
172 | sub create_child_type { | ||||
173 | my ( $self, %opts ) = @_; | ||||
174 | |||||
175 | my $constraint | ||||
176 | = Moose::Meta::TypeConstraint->new( %opts, parent => $self ); | ||||
177 | |||||
178 | # if we have a type constraint union, and no | ||||
179 | # type check, this means we are just aliasing | ||||
180 | # the union constraint, which means we need to | ||||
181 | # handle this differently. | ||||
182 | # - SL | ||||
183 | if ( not( defined $opts{constraint} ) | ||||
184 | && $self->has_coercion ) { | ||||
185 | $constraint->coercion( | ||||
186 | Moose::Meta::TypeCoercion::Union->new( | ||||
187 | type_constraint => $self, | ||||
188 | ) | ||||
189 | ); | ||||
190 | } | ||||
191 | |||||
192 | return $constraint; | ||||
193 | } | ||||
194 | |||||
195 | 1 | 8µs | 1; | ||
196 | |||||
197 | # ABSTRACT: A union of Moose type constraints | ||||
198 | |||||
199 | __END__ |