Skip to content

Commit

Permalink
Add type constraints to port numbers in IO::Socket::Async
Browse files Browse the repository at this point in the history
Also remove todo to improve error message for invalid port numbers.
  • Loading branch information
jmaslak committed Oct 29, 2018
1 parent d73272e commit 547f28b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
22 changes: 13 additions & 9 deletions src/core/IO/Socket/Async.pm6
Expand Up @@ -8,11 +8,13 @@ my class IO::Socket::Async {
has $!close-promise;
has $!close-vow;

subset Port-Number of Int where { !defined($_) or $_ ~~ ^65536 };

has Str $.peer-host;
has Int $.peer-port;
has Port-Number $.peer-port;

has Str $.socket-host;
has Int $.socket-port;
has Port-Number $.socket-port;

method new() {
die "Cannot create an asynchronous socket directly; please use\n" ~
Expand Down Expand Up @@ -165,7 +167,7 @@ my class IO::Socket::Async {
$!close-vow.keep(True);
}

method connect(IO::Socket::Async:U: Str() $host, Int() $port,
method connect(IO::Socket::Async:U: Str() $host, Int() $port where * ~~ Port-Number,
:$enc = 'utf-8', :$scheduler = $*SCHEDULER) {
my $p = Promise.new;
my $v = $p.vow;
Expand Down Expand Up @@ -290,8 +292,8 @@ my class IO::Socket::Async {
method serial(--> True) { }
}

method listen(IO::Socket::Async:U: Str() $host, Int() $port, Int() $backlog = 128,
:$enc = 'utf-8', :$scheduler = $*SCHEDULER) {
method listen(IO::Socket::Async:U: Str() $host, Int() $port where * ~~ Port-Number,
Int() $backlog = 128, :$enc = 'utf-8', :$scheduler = $*SCHEDULER) {
my $encoding = Encoding::Registry.find($enc);
Supply.new: SocketListenerTappable.new:
:$host, :$port, :$backlog, :$encoding, :$scheduler
Expand Down Expand Up @@ -329,8 +331,8 @@ my class IO::Socket::Async {
await $p
}

method bind-udp(IO::Socket::Async:U: Str() $host, Int() $port, :$broadcast,
:$enc = 'utf-8', :$scheduler = $*SCHEDULER) {
method bind-udp(IO::Socket::Async:U: Str() $host, Int() $port where * ~~ Port-Number,
:$broadcast, :$enc = 'utf-8', :$scheduler = $*SCHEDULER) {
my $p = Promise.new;
my $encoding = Encoding::Registry.find($enc);
nqp::asyncudp(
Expand All @@ -355,11 +357,13 @@ my class IO::Socket::Async {
await $p
}

method print-to(IO::Socket::Async:D: Str() $host, Int() $port, Str() $str, :$scheduler = $*SCHEDULER) {
method print-to(IO::Socket::Async:D: Str() $host, Int() $port where * ~~ Port-Number,
Str() $str, :$scheduler = $*SCHEDULER) {
self.write-to($host, $port, $!encoder.encode-chars($str), :$scheduler)
}

method write-to(IO::Socket::Async:D: Str() $host, Int() $port, Blob $b, :$scheduler = $*SCHEDULER) {
method write-to(IO::Socket::Async:D: Str() $host, Int() $port where * ~~ Port-Number,
Blob $b, :$scheduler = $*SCHEDULER) {
my $p = Promise.new;
my $v = $p.vow;
nqp::asyncwritebytesto(
Expand Down
15 changes: 6 additions & 9 deletions t/05-messages/02-errors.t
Expand Up @@ -16,15 +16,12 @@ throws-like { for [:a] X [:b] -> ($i, $j) { } },
message => / '<anon>' /,
"anonymous subs get '<anon>' in arity error messages";

todo 'needs better error message';

skip 'crashes: https://github.com/rakudo/rakudo/issues/2402';
#throws-like {
# sub l { IO::Socket::Async.listen: "localhost", 111390 }
# react whenever l() {
# whenever l() {} # try to listen on already open sock
# }
#}, X::AdHoc, message => /'something good'/;
throws-like {
sub l { IO::Socket::Async.listen: "localhost", 111390 }
react whenever l() {
whenever l() {} # try to listen on already open sock
}
}, X::TypeCheck::Binding::Parameter, message => /'type check failed'/;

# RT #132283
is-deeply class { has $.bar }.^methods».name.sort, <BUILDALL bar>,
Expand Down

2 comments on commit 547f28b

@b2gills
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Int() $port where * ~~ Port-Number is probably better written as Int() $port where Port-Number as the where clause already does smartmatching.

@jmaslak
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@b2gills - I put your suggestion in #2481

Please sign in to comment.