| 1 | package IO::AIO::Promiser; | ||||||||||||
| 2 | |||||||||||||
| 3 | 5 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 5 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | |||||||||||||
| 6 | 5 | our $VERSION = '0.01_01'; | |||||||||||
| 7 | |||||||||||||
| 8 | =encoding utf-8 | ||||||||||||
| 9 | |||||||||||||
| 10 | =head1 NAME | ||||||||||||
| 11 | |||||||||||||
| 12 | IO::AIO::Promiser - Promise interface around L<IO::AIO> | ||||||||||||
| 13 | |||||||||||||
| 14 | =begin html | ||||||||||||
| 15 | |||||||||||||
| 16 | <a href='https://coveralls.io/github/FGasper/p5-IO-AIO-Promiser?branch=main'><img src='https://coveralls.io/repos/github/FGasper/p5-IO-AIO-Promiser/badge.svg?branch=main' alt='Coverage Status' /></a> | ||||||||||||
| 17 | |||||||||||||
| 18 | =end html | ||||||||||||
| 19 | |||||||||||||
| 20 | =head1 SYNOPSIS | ||||||||||||
| 21 | |||||||||||||
| 22 | (This example uses L<AnyEvent::AIO> for conciseness; it’s not difficult | ||||||||||||
| 23 | to adapt it for use with, e.g., L<IO::Async> or L<Mojolicious>.) | ||||||||||||
| 24 | |||||||||||||
| 25 | To slurp with L<AnyEvent> and L<Promise::AsyncAwait>: | ||||||||||||
| 26 | |||||||||||||
| 27 | use AnyEvent::AIO; | ||||||||||||
| 28 | use Promise::AsyncAwait; | ||||||||||||
| 29 | |||||||||||||
| 30 | use IO::AIO::Promiser ':all'; | ||||||||||||
| 31 | |||||||||||||
| 32 | async sub slurp ($abs_path) { | ||||||||||||
| 33 | my $fh = await aio_open($abs_path, Fcntl::O_RDONLY, 0); | ||||||||||||
| 34 | |||||||||||||
| 35 | my $buf = q<>; | ||||||||||||
| 36 | 1 while await aio_read($fh, undef, 65536, $buf, length $buf); | ||||||||||||
| 37 | |||||||||||||
| 38 | return $buf; | ||||||||||||
| 39 | } | ||||||||||||
| 40 | |||||||||||||
| 41 | … and now you can: | ||||||||||||
| 42 | |||||||||||||
| 43 | my $cv = AnyEvent->condvar(); | ||||||||||||
| 44 | |||||||||||||
| 45 | slurp("/etc/services")->then( | ||||||||||||
| 46 | sub { $cv->(@_) }, | ||||||||||||
| 47 | sub { $cv->croak(@_) }, | ||||||||||||
| 48 | ); | ||||||||||||
| 49 | |||||||||||||
| 50 | my $content = $cv->recv(); | ||||||||||||
| 51 | |||||||||||||
| 52 | It’s a bit like L<Coro::AIO>, but with async/await rather than L<Coro>, | ||||||||||||
| 53 | and more proactive error-checking. | ||||||||||||
| 54 | |||||||||||||
| 55 | See below for examples of setup with L<IO::Async> and L<Mojolicious>. | ||||||||||||
| 56 | |||||||||||||
| 57 | =head1 DESCRIPTION | ||||||||||||
| 58 | |||||||||||||
| 59 | L<IO::AIO> is great, but its callback-driven interface is less so. | ||||||||||||
| 60 | This module wraps IO::AIO so you can easily use promises with it instead. | ||||||||||||
| 61 | |||||||||||||
| 62 | =cut | ||||||||||||
| 63 | |||||||||||||
| 64 | #---------------------------------------------------------------------- | ||||||||||||
| 65 | |||||||||||||
| 66 | 5 | ▶ | use Carp (); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 67 | 5 | ▶ | use IO::AIO (); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 68 | |||||||||||||
| 69 | 5 | ▶ | use Promise::XS (); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 70 | |||||||||||||
| 71 | #defined below | ||||||||||||
| 72 | 5 | my %METADATA; | |||||||||||
| 73 | |||||||||||||
| 74 | #---------------------------------------------------------------------- | ||||||||||||
| 75 | |||||||||||||
| 76 | =head1 FUNCTIONS | ||||||||||||
| 77 | |||||||||||||
| 78 | This module doesn’t (yet?) cover everything IO::AIO can do. | ||||||||||||
| 79 | If there’s functionality you’d like to have, create a feature request. | ||||||||||||
| 80 | |||||||||||||
| 81 | The following are like their L<IO::AIO> counterparts, but the final | ||||||||||||
| 82 | callback argument is omitted, and a promise is returned instead. | ||||||||||||
| 83 | That promise’s resolution is the callback’s success-case return, and | ||||||||||||
| 84 | the rejection is the callback’s failure-case return. | ||||||||||||
| 85 | |||||||||||||
| 86 | =over | ||||||||||||
| 87 | |||||||||||||
| 88 | =item * C<wd> and C<realpath()> | ||||||||||||
| 89 | |||||||||||||
| 90 | =item * C<open()> (NB: The resolution is an oddity: a filehandle that is | ||||||||||||
| 91 | B<not> a GLOB reference.) | ||||||||||||
| 92 | |||||||||||||
| 93 | =item * C<seek()> | ||||||||||||
| 94 | |||||||||||||
| 95 | =item * C<close()> | ||||||||||||
| 96 | |||||||||||||
| 97 | =item * C<read()> and C<write()> | ||||||||||||
| 98 | |||||||||||||
| 99 | =item * C<readdir()> and C<readdirx()> | ||||||||||||
| 100 | |||||||||||||
| 101 | =item * C<stat()> and C<lstat()> | ||||||||||||
| 102 | |||||||||||||
| 103 | =item * C<mkdir()> and C<rmdir()> | ||||||||||||
| 104 | |||||||||||||
| 105 | =item * C<chown()> and C<chmod()> | ||||||||||||
| 106 | |||||||||||||
| 107 | =item * C<utime()> | ||||||||||||
| 108 | |||||||||||||
| 109 | =item * C<unlink()> | ||||||||||||
| 110 | |||||||||||||
| 111 | =item * C<link()>, C<symlink()>, and C<readlink()> | ||||||||||||
| 112 | |||||||||||||
| 113 | =item * C<rename()> and C<rename2()> | ||||||||||||
| 114 | |||||||||||||
| 115 | =back | ||||||||||||
| 116 | |||||||||||||
| 117 | The following are a bit different—but with good reason!—from the | ||||||||||||
| 118 | corresponding IO::AIO interface: | ||||||||||||
| 119 | |||||||||||||
| 120 | =over | ||||||||||||
| 121 | |||||||||||||
| 122 | =item * C<slurp()> - The promise resolves to the file content, | ||||||||||||
| 123 | so you don’t need to initialize a separate C<$data> scalar. | ||||||||||||
| 124 | |||||||||||||
| 125 | =cut | ||||||||||||
| 126 | |||||||||||||
| 127 | 2 | ▶ | sub slurp { | ||||||||||
|
Subroutine
slurp: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 128 | 2 | my $d = Promise::XS::deferred(); | |||||||||||
| 129 | |||||||||||||
| 130 | 2 | my $data; | |||||||||||
| 131 | |||||||||||||
| 132 | 2 | ▶ | &IO::AIO::aio_slurp( @_[0 .. 2], $data, sub { | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||
| 133 | 2 | ▶ | if ($_[0] >= 0) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 134 | 1 | $d->resolve($data); | |||||||||||
| 135 | } | ||||||||||||
| 136 | else { | ||||||||||||
| 137 | 1 | $d->reject($!); | |||||||||||
| 138 | } | ||||||||||||
| 139 | 2 | } ); | |||||||||||
| 140 | |||||||||||||
| 141 | 2 | $d->promise(); | |||||||||||
| 142 | } | ||||||||||||
| 143 | |||||||||||||
| 144 | =back | ||||||||||||
| 145 | |||||||||||||
| 146 | =cut | ||||||||||||
| 147 | |||||||||||||
| 148 | #---------------------------------------------------------------------- | ||||||||||||
| 149 | |||||||||||||
| 150 | =head1 EXPORT INTERFACE | ||||||||||||
| 151 | |||||||||||||
| 152 | Since it’s clunky to have to type C<IO::AIO::Promiser::open>, if you | ||||||||||||
| 153 | pass C<:all> to this module on import you’ll get C<aio_*> aliases | ||||||||||||
| 154 | exported into your namespace, so you can call, e.g., C<aio_open> instead. | ||||||||||||
| 155 | This matches IO::AIO’s own calling convention. | ||||||||||||
| 156 | |||||||||||||
| 157 | =cut | ||||||||||||
| 158 | |||||||||||||
| 159 | 115 | ▶ | sub _create_func { | ||||||||||
|
Subroutine
_create_func: called CC=4 SCAR=14.1
| |||||||||||||
| 160 | 115 | my $fn = shift; | |||||||||||
| 161 | |||||||||||||
| 162 | 115 | ▶ | my $metadata_ar = $METADATA{$fn} or do { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 163 | 0 | Carp::confess sprintf("Bad function: %s::%s", __PACKAGE__, $fn); | |||||||||||
| 164 | }; | ||||||||||||
| 165 | |||||||||||||
| 166 | 115 | my ($limit, $resolver_cr) = @$metadata_ar; | |||||||||||
| 167 | 115 | ▶ | $resolver_cr ||= \&_create_negative_resolver; | ||||||||||
|
Condition100%2/2
$resolver_cr ||= \&IO::AIO::Promiser::_create_negative_resolver
MC/DC100%1/1
$resolver_cr ||= \&IO::AIO::Promiser::_create_negative_resolver
$resolver_cr
Truth table100%2/2
A
A= $resolver_cr
| |||||||||||||
| 168 | |||||||||||||
| 169 | 115 | ▶ | my $ioaio_cr = IO::AIO->can("aio_$fn") or do { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 170 | 0 | Carp::confess "IO::AIO::aio_$fn is missing!"; | |||||||||||
| 171 | }; | ||||||||||||
| 172 | |||||||||||||
| 173 | 40 | ▶ | my $new_cr = sub { | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 174 | 40 | my $d = Promise::XS::deferred(); | |||||||||||
| 175 | |||||||||||||
| 176 | 40 | $ioaio_cr->( @_[0 .. $limit], $resolver_cr->($d) ); | |||||||||||
| 177 | |||||||||||||
| 178 | 40 | $d->promise(); | |||||||||||
| 179 | 115 | }; | |||||||||||
| 180 | |||||||||||||
| 181 | { | ||||||||||||
| 182 | 5 | ▶ | no strict 'refs'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 183 | 115 | *$fn = $new_cr; | |||||||||||
| 184 | } | ||||||||||||
| 185 | |||||||||||||
| 186 | 115 | return $new_cr; | |||||||||||
| 187 | } | ||||||||||||
| 188 | |||||||||||||
| 189 | #---------------------------------------------------------------------- | ||||||||||||
| 190 | |||||||||||||
| 191 | 9 | ▶ | sub _create_defined_resolver { | ||||||||||
|
Subroutine
_create_defined_resolver: called CC=1 SCAR=0.0
| |||||||||||||
| 192 | 9 | my $d = $_[0]; | |||||||||||
| 193 | |||||||||||||
| 194 | 9 | ▶ | return sub { | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||
| 195 | 9 | ▶ | if (defined $_[0]) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 196 | 3 | $d->resolve($_[0]); | |||||||||||
| 197 | } | ||||||||||||
| 198 | else { | ||||||||||||
| 199 | 6 | $d->reject($!); | |||||||||||
| 200 | } | ||||||||||||
| 201 | 9 | }; | |||||||||||
| 202 | } | ||||||||||||
| 203 | |||||||||||||
| 204 | 31 | ▶ | sub _create_negative_resolver { | ||||||||||
|
Subroutine
_create_negative_resolver: called CC=1 SCAR=0.0
| |||||||||||||
| 205 | 31 | my $d = $_[0]; | |||||||||||
| 206 | |||||||||||||
| 207 | 31 | ▶ | return sub { | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||
| 208 | 31 | ▶ | if ($_[0] >= 0) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 209 | 14 | $d->resolve($_[0]); | |||||||||||
| 210 | } | ||||||||||||
| 211 | else { | ||||||||||||
| 212 | 17 | $d->reject($!); | |||||||||||
| 213 | } | ||||||||||||
| 214 | 31 | }; | |||||||||||
| 215 | } | ||||||||||||
| 216 | |||||||||||||
| 217 | 5 | ▶ | BEGIN { | ||||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=6.9
| |||||||||||||
| 218 | 5 | %METADATA = ( | |||||||||||
| 219 | wd => [0, \&_create_defined_resolver], | ||||||||||||
| 220 | realpath => [0, \&_create_defined_resolver], | ||||||||||||
| 221 | |||||||||||||
| 222 | open => [2, \&_create_defined_resolver], | ||||||||||||
| 223 | close => [0, \&_create_defined_resolver], | ||||||||||||
| 224 | seek => [2], | ||||||||||||
| 225 | read => [4], | ||||||||||||
| 226 | readdir => [0, \&_create_defined_resolver], | ||||||||||||
| 227 | readdirx => [1, \&_create_defined_resolver], | ||||||||||||
| 228 | write => [4], | ||||||||||||
| 229 | stat => [0], | ||||||||||||
| 230 | lstat => [0], | ||||||||||||
| 231 | utime => [2], | ||||||||||||
| 232 | chown => [2], | ||||||||||||
| 233 | chmod => [1], | ||||||||||||
| 234 | unlink => [0], | ||||||||||||
| 235 | mkdir => [1], | ||||||||||||
| 236 | rmdir => [0], | ||||||||||||
| 237 | link => [1], | ||||||||||||
| 238 | rename => [1], | ||||||||||||
| 239 | rename2 => [2], | ||||||||||||
| 240 | symlink => [1], | ||||||||||||
| 241 | readlink => [0, \&_create_defined_resolver], | ||||||||||||
| 242 | truncate => [1], | ||||||||||||
| 243 | ); | ||||||||||||
| 244 | |||||||||||||
| 245 | 5 | _create_func($_) for keys %METADATA; | |||||||||||
| 246 | } | ||||||||||||
| 247 | |||||||||||||
| 248 | 5 | ▶ | sub import { | ||||||||||
|
Subroutine
import: called CC=4 SCAR=13.9
| |||||||||||||
| 249 | 5 | my $ns = (caller 1)[0]; | |||||||||||
| 250 | 5 | my $opt = $_[1]; | |||||||||||
| 251 | |||||||||||||
| 252 | 5 | ▶ | if ($opt) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 253 | 2 | my @to_import; | |||||||||||
| 254 | |||||||||||||
| 255 | 2 | ▶ | if ($opt eq ':all') { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 256 | 1 | @to_import = keys %METADATA; | |||||||||||
| 257 | } | ||||||||||||
| 258 | else { | ||||||||||||
| 259 | 1 | Carp::confess sprintf "%s: Bad import parameter: %s", __PACKAGE__, $opt; | |||||||||||
| 260 | } | ||||||||||||
| 261 | |||||||||||||
| 262 | 5 | ▶ | no strict 'refs'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 263 | 1 | *{"${ns}::aio_$_"} = __PACKAGE__->can($_) for @to_import; | |||||||||||
| 264 | } | ||||||||||||
| 265 | |||||||||||||
| 266 | 4 | return; | |||||||||||
| 267 | } | ||||||||||||
| 268 | |||||||||||||
| 269 | 5 | 1; | |||||||||||
| 270 | |||||||||||||
| 271 | #---------------------------------------------------------------------- | ||||||||||||
| 272 | |||||||||||||
| 273 | =head1 AUTHOR & COPYRIGHT | ||||||||||||
| 274 | |||||||||||||
| 275 | Copyright 2021 Gasper Software Consulting | ||||||||||||
| 276 | |||||||||||||
| 277 | =head1 LICENSE | ||||||||||||
| 278 | |||||||||||||
| 279 | This library is licensed under the same license as Perl. | ||||||||||||
| 280 | |||||||||||||
| 281 | =cut | ||||||||||||