File: type_constraints.pl

package info (click to toggle)
libmouse-perl 2.5.11-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 2,160 kB
  • sloc: perl: 14,614; ansic: 218; makefile: 8
file content (194 lines) | stat: -rw-r--r-- 4,068 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!perl
use strict;
use Benchmark qw(:all);
use Config; printf "Perl/%vd in $Config{archname}\n\n", $^V;
use warnings;
no warnings 'once';

my $cxsa_is_loaded = eval q{
    package CXSA;
    use Class::XSAccessor
        constructor => 'new',
        accessors   => {
            simple => 'simple',
        },
    ;
    1;
};

{
    package Foo;
    sub new { bless {}, shift }
}

{
    package MouseOne;
    use Mouse;
    use Mouse::Util::TypeConstraints;
    has simple => (
        is => 'rw',
    );
    has with_lazy => (
        is      => 'rw',
        lazy    => 1,
        default => 42,
    );
    has with_tc => (
        is  => 'rw',
        isa => 'Int',
    );

    has with_tc_class_type => (
        is  => 'rw',
        isa => 'Foo',
    );

    has with_tc_array_of_int => (
        is  => 'rw',
        isa => 'ArrayRef[Int]',
    );

    has with_tc_duck_type => (
        is  => 'rw',
        isa => duck_type([qw(simple)]),
    );
    __PACKAGE__->meta->make_immutable;
}
{
    package MooseOne;
    use Moose;
    use Moose::Util::TypeConstraints;
    has simple => (
        is => 'rw',
    );
    has with_lazy => (
        is      => 'rw',
        lazy    => 1,
        default => 42,
    );
    has with_tc => (
        is  => 'rw',
        isa => 'Int',
    );
    has with_tc_class_type => (
        is  => 'rw',
        isa => 'Foo',
    );
    has with_tc_array_of_int => (
        is  => 'rw',
        isa => 'ArrayRef[Int]',
    );
    has with_tc_duck_type => (
        is  => 'rw',
        isa => duck_type([qw(simple)]),
    );

    __PACKAGE__->meta->make_immutable;
}

use B qw(svref_2object);

print "Moose/$Moose::VERSION (Class::MOP/$Class::MOP::VERSION)\n";
print "Mouse/$Mouse::VERSION\n";
print "Class::XSAccessor/$Class::XSAccessor::VERSION\n" if $cxsa_is_loaded;

my $mi = MouseOne->new();
my $mx = MooseOne->new();
my $cx;
$cx = CXSA->new       if $cxsa_is_loaded;

print "\nSETTING for simple attributes\n";
cmpthese -1 => {
    'Mouse' => sub{
        $mi->simple(10);
        $mi->simple(10);
    },
    'Moose' => sub{
        $mx->simple(10);
        $mx->simple(10);
    },
    $cxsa_is_loaded ? (
    'C::XSAccessor' => sub{
        $cx->simple(10);
        $cx->simple(10);
    },
    ) : (),

};

print "\nSETTING for attributes with type constraints 'Int' (except for C::XSAccessor)\n";
cmpthese -1 => {
    'Mouse' => sub{
        $mi->with_tc(10);
        $mi->with_tc(10);
    },
    'Moose' => sub{
        $mx->with_tc(10);
        $mx->with_tc(10);
    },
    $cxsa_is_loaded ? (
    'C::XSAccessor' => sub{
        $cx->simple(10);
        $cx->simple(10);
    },
    ) : (),
};

print "\nSETTING for attributes with type constraints 'Foo' (except for C::XSAccessor)\n";
my $foo = Foo->new;
cmpthese -1 => {
    'Mouse' => sub{
        $mi->with_tc_class_type($foo);
        $mi->with_tc_class_type($foo);
    },
    'Moose' => sub{
        $mx->with_tc_class_type($foo);
        $mx->with_tc_class_type($foo);
    },
    $cxsa_is_loaded ? (
    'C::XSAccessor' => sub{
        $cx->simple($foo);
        $cx->simple($foo);
    },
    ) : (),
};

print "\nSETTING for attributes with type constraints 'ArrayRef[Int]' (except for C::XSAccessor)\n";

$foo = [10, 20];
cmpthese -1 => {
    'Mouse' => sub{
        $mi->with_tc_array_of_int($foo);
        $mi->with_tc_array_of_int($foo);
    },
    'Moose' => sub{
        $mx->with_tc_array_of_int($foo);
        $mx->with_tc_array_of_int($foo);
    },
    $cxsa_is_loaded ? (
    'C::XSAccessor' => sub{
        $cx->simple($foo);
        $cx->simple($foo);
    },
    ) : (),
};

print "\nSETTING for attributes with type constraints duck_type() (except for C::XSAccessor)\n";

$foo = MouseOne->new();
cmpthese -1 => {
    'Mouse' => sub{
        $mi->with_tc_duck_type($foo);
        $mi->with_tc_duck_type($foo);
    },
    'Moose' => sub{
        $mx->with_tc_duck_type($foo);
        $mx->with_tc_duck_type($foo);
    },
    $cxsa_is_loaded ? (
    'C::XSAccessor' => sub{
        $cx->simple($foo);
        $cx->simple($foo);
    },
    ) : (),
};