File Coverage

blib/lib/Future/IO/Impl/AnyEvent.pm
Criterion Covered Total %
statement 55 74 74.3
branch 5 6 83.3
condition n/a
subroutine 17 24 70.8
pod 3 5 60.0
total 80 109 73.3


line stmt bran cond sub pod time code
1             package Future::IO::Impl::AnyEvent;
2              
3 7     7   1296682 use strict;
  7         7  
  7         192  
4 7     7   22 use warnings;
  7         16  
  7         269  
5 7     7   90 use 5.014;
  7         14  
6              
7 7     7   28 use base 'Future::IO::ImplBase';
  7         7  
  7         1086  
8              
9 7     7   39008 use AnyEvent;
  7         4507  
  7         162  
10 7     7   2629 use Future::IO::Impl::AnyEvent::Future;
  7         18  
  7         186  
11 7     7   22 use IO::Poll qw(POLLIN POLLOUT);
  7         10  
  7         3226  
12              
13             our $VERSION = 0.03;
14              
15             __PACKAGE__->APPLY;
16              
17             sub sleep { ## no critic (ProhibitBuiltinHomonyms)
18 36     36 1 17820 my (undef, $sec) = @_;
19 36         135 my $f = Future::IO::Impl::AnyEvent::Future->new;
20 36         222 my $w;
21 36     30   216 $w = AE::timer $sec, 0, sub { undef $w; $f->done };
  30         5991552  
  30         519  
22 36     6   384 $f->on_cancel(sub { undef $w });
  6         159  
23 36         636 return $f;
24             }
25              
26             # Since version 0.19, Future::IO::ImplBase implements sysread, syswrite, accept,
27             # connect, etc. on top of a lower-level ->poll method, so we must provide it. We
28             # still keep ready_for_read and ready_for_write below for compatibility with
29             # older versions of Future::IO which used those instead.
30             sub poll {
31 72     72 0 1248603 my (undef, $fh, $events) = @_;
32 72         393 my $f = Future::IO::Impl::AnyEvent::Future->new;
33 72         480 my ($rw, $ww);
34             my $done = sub {
35 60 50   60   153 return if $f->is_ready;
36 60         324 undef $rw;
37 60         243 undef $ww;
38 60         405 $f->done($_[0]);
39 72         378 };
40             # AnyEvent only knows about readable (0) and writable (1); there is no way to
41             # watch for POLLPRI, but callers always request it together with POLLIN or
42             # POLLOUT (e.g. connect polls POLLOUT|POLLPRI), so this is good enough.
43 72 100       201 if ($events & POLLIN) {
44 30     24   219 $rw = AE::io $fh, 0, sub { $done->(POLLIN) };
  24         1419  
45             }
46 72 100       441 if ($events & POLLOUT) {
47 42     36   204 $ww = AE::io $fh, 1, sub { $done->(POLLOUT) };
  36         1629  
48             }
49 72     12   669 $f->on_cancel(sub { undef $rw; undef $ww });
  12         747  
  12         93  
50 72         1509 return $f;
51             }
52              
53             sub ready_for_read {
54 0     0 1 0 my (undef, $fh) = @_;
55 0         0 my $f = Future::IO::Impl::AnyEvent::Future->new;
56 0         0 my $w;
57 0     0   0 $w = AE::io $fh, 0, sub { undef $w; $f->done };
  0         0  
  0         0  
58 0     0   0 $f->on_cancel(sub { undef $w });
  0         0  
59 0         0 return $f;
60             }
61              
62             sub ready_for_write {
63 0     0 1 0 my (undef, $fh) = @_;
64 0         0 my $f = Future::IO::Impl::AnyEvent::Future->new;
65 0         0 my $w;
66 0     0   0 $w = AE::io $fh, 1, sub { undef $w; $f->done };
  0         0  
  0         0  
67 0     0   0 $f->on_cancel(sub { undef $w });
  0         0  
68 0         0 return $f;
69             }
70              
71             sub waitpid { ## no critic (ProhibitBuiltinHomonyms)
72 6     6 0 419922 my (undef, $pid) = @_;
73 6         289 my $f = Future::IO::Impl::AnyEvent::Future->new;
74 6         429 my $w;
75 6     6   1402 $w = AE::child $pid, sub { undef $w; $f->done($_[1]) };
  6         1138617  
  6         308  
76 6     0   11506 $f->on_cancel(sub { undef $w });
  0         0  
77 6         251 return $f;
78             }
79              
80             1;
81              
82             __END__