Permalink
Browse files

Undo workaround for satisfying role requirements with multis

   Deal with some resulting flattening issues.  Probably overkilled there.
Avoid automatic punning when role is last item in compilation unit
  • Loading branch information...
1 parent 2af1bf7 commit d69a346b40f99ff0a01b794ca70ece9d4bc64335 @skids committed Feb 5, 2013
Showing with 40 additions and 32 deletions.
  1. +6 −5 lib/Sum.pm6
  2. +2 −0 lib/Sum/Adler.pm6
  3. +2 −0 lib/Sum/CRC.pm6
  4. +14 −12 lib/Sum/MD.pm6
  5. +8 −11 lib/Sum/MDPad.pm6
  6. +4 −2 lib/Sum/SHA.pm6
  7. +2 −0 lib/Sum/SipHash.pm6
  8. +2 −2 t/mdpad.t
View
@@ -370,7 +370,7 @@ role Sum::Marshal::Raw {
method push (*@addends --> Failure) {
# Pass the whole list to the class's add method, unprocessed.
- sink self.add(@addends).grep({$_.WHAT ~~ Failure }).map:
+ sink self.add(|@addends).grep({$_.WHAT ~~ Failure }).map:
{ return $_ };
my $res = Failure.new(X::Sum::Push::Usage.new());
$res.defined;
@@ -717,7 +717,7 @@ role Sum::Marshal::Block [::B :$BufT = Buf, :$elems = 64, ::b :$BitT = Bool]
# use multi/constrained method to workaround diamond problem
multi method emit ($self where {True}: *@addends) {
- @.accum.push(@addends);
+ @.accum.push(|@addends);
# Emit any completed blocks.
gather while (@.accum.elems > $elems) {
@@ -762,8 +762,7 @@ role Sum::Marshal::Block [::B :$BufT = Buf, :$elems = 64, ::b :$BitT = Bool]
gather do {
my $i = 0;
while ($addend.elems - $i + +@.accum >= $elems) {
- take B.new(flat @.accum,
- $addend[$i..^($i + $elems - +@.accum)]);
+ take B.new(|@.accum, $addend[$i..^($i + $elems - +@.accum)]);
$i += $elems - +@.accum;
@.accum = ();
}
@@ -774,7 +773,7 @@ role Sum::Marshal::Block [::B :$BufT = Buf, :$elems = 64, ::b :$BitT = Bool]
# use multi/constrained method to workaround diamond problem
multi method drain ($self where {True}:) {
$.drained = True;
- B.new(@.accum), ?<<@.bits
+ flat B.new(@.accum), ?<<@.bits
}
}
@@ -833,3 +832,5 @@ role Sum::Marshal::IO {
}
}
}
+
+1; # Avoid sink-punning of last role
View
@@ -226,6 +226,8 @@ role Sum::Fletcher32
role Sum::Fletcher64
does Sum::Fletcher[ :modulusA(4294967295), :columnsA(32) ] { }
+1; # Avoid sink-punning of last role
+
=begin pod
=head1 METHODS
View
@@ -1009,6 +1009,8 @@ role Sum::CRC_64_Jones
role Sum::CRC_82_DARC
does Sum::CRC[ :reflect, :columns(82), :poly(0x0308c0111011401440411) ] { }
+1; # Avoid sink-punning of last role
+
=begin pod
=head1 AUTHOR
View
@@ -441,7 +441,7 @@ role Sum::MD4_5 [ :$alg where (one <MD5 MD4 MD4ext RIPEMD-128 RIPEMD-160 RIPEMD-
@!s = 0xffffffff X+& @!s;
}
- multi method do_add (Buf $block where { .elems == 64 }) {
+ multi method add (Buf $block where { .elems == 64 }) {
# Update the length count and check for problems via Sum::MDPad
given self.pos_block_inc {
@@ -467,7 +467,7 @@ role Sum::MD4_5 [ :$alg where (one <MD5 MD4 MD4ext RIPEMD-128 RIPEMD-160 RIPEMD-
return $_ unless $_.exception.WHAT ~~ X::Sum::Push::Usage;
}
- self.add(self.drain) if self.^can("drain");
+ self.add(|self.drain) if self.^can("drain");
self.add(Buf.new()) unless $.final;
@@ -557,20 +557,21 @@ role Sum::MD2 does Sum {
has @!X is rw = 0 xx 48; # The digest state
has Bool $!final is rw = False; # whether pad/checksum is in state already
- multi method do_add (*@addends) {
- sink for (@addends) { self.add($_) }
+ proto method add (|cap) {*}
+ multi method add (*@addends) {
+ sink for @addends { self.add($_) }
}
- multi method do_add ($addend) {
+ multi method add ($addend) {
# TODO: Typed failure here?
die("Marshalling error. Addends must be Buf with 0..16 bytes.");
}
- multi method do_add (Buf $block where { -1 < .elems < 16 }) {
+ multi method add (Buf $block where { -1 < .elems < 16 }) {
my int $empty = 16 - $block.elems;
$!final = True;
- self.do_add(Buf.new($block.values, $empty xx $empty));
- self.do_add(Buf.new(@!C[]));
+ self.add(Buf.new($block.values, $empty xx $empty));
+ self.add(Buf.new(@!C[]));
}
- multi method do_add (Buf $block where { .elems == 16 }) {
+ multi method add (Buf $block where { .elems == 16 }) {
@!X[16..^32] = $block.values;
@!X[32..^48] = @!X[^16] Z+^ @!X[16..^32];
for 15,^15 Z ^16 -> $last, $x {
@@ -585,13 +586,12 @@ role Sum::MD2 does Sum {
return;
}
method size ( --> int) { 128 };
- method add (*@addends) { self.do_add(|@addends) }
method finalize(*@addends) {
- given self.push(@addends) {
+ given self.push(|@addends) {
return $_ unless $_.exception.WHAT ~~ X::Sum::Push::Usage;
}
- self.add(self.drain) if self.^can("drain");
+ self.add(|self.drain) if self.^can("drain");
self.add(Buf.new()) unless $!final;
@@ -605,6 +605,8 @@ role Sum::MD2 does Sum {
method Buf { self.buf8 }
}
+1; # Avoid sink-punning of role
+
=begin pod
=head1 AUTHOR
View
@@ -178,18 +178,16 @@ role Sum::MDPad [ int :$blocksize where { not $_ % 8 } = 512, :$lengthtype where
block. Slurpy lists may be C<.push>ed if C<Sum::Marshal::Block> roles
are mixed instead.
- As an interim workaround, these multi candidates are currently
- named C<.do_add> instead, and so should be the provided candidate.
-
=end pod
- multi method do_add ($addend) {
+ proto method add (|c) {*}
+ multi method add ($addend) {
fail(X::Sum::Marshal.new(:addend($addend.WHAT.^name)))
}
- multi method do_add () { }
- multi method do_add (Buf $block where { -1 < .elems < $bbytes },
- Bool $b7?, Bool $b6?, Bool $b5?, Bool $b4?,
- Bool $b3?, Bool $b2?, Bool $b1?) {
+ multi method add () { }
+ multi method add (Buf $block where { -1 < .elems < $bbytes },
+ Bool $b7?, Bool $b6?, Bool $b5?, Bool $b4?,
+ Bool $b3?, Bool $b2?, Bool $b1?) {
fail(X::Sum::Final.new()) if $.final;
my @bcat = ();
@@ -223,11 +221,10 @@ role Sum::MDPad [ int :$blocksize where { not $_ % 8 } = 512, :$lengthtype where
$.final = True;
}
- # Workaround for multis not satisfying prototypes in composed roles
- method add (*@addends) { self.do_add(|@addends) }
-
}
+1; # Avoid sink-punning of last role
+
=begin pod
=head1 AUTHOR
View
@@ -119,7 +119,7 @@ role Sum::SHA1 [ Bool :$insecure_sha0_obselete = False ]
$tmp;
}
- multi method do_add (Buf $block where { .elems == 64 }) {
+ multi method add (Buf $block where { .elems == 64 }) {
# Update the length count and check for problems via Sum::MDPad
given self.pos_block_inc {
@@ -211,7 +211,7 @@ role Sum::SHA2common {
has @.w is rw; # "Parsed" message gets bound here.
has @.s is rw = self.init(); # Current hash state. H in specification.
- multi method do_add (Buf $block where { .elems == self.bsize/8 }) {
+ multi method add (Buf $block where { .elems == self.bsize/8 }) {
# Update the length count and check for problems via Sum::MDPad
given self.pos_block_inc {
when Failure { return $_ };
@@ -409,6 +409,8 @@ role Sum::SHA2[ :$columns where 256 ] does Sum::SHA256 { }
role Sum::SHA2[ :$columns where 384 ] does Sum::SHA384 { }
role Sum::SHA2[ :$columns where 512 ] does Sum::SHA512 { }
+1; # Avoid sink-punning of last role
+
=begin pod
=head1 AUTHOR
View
@@ -215,6 +215,8 @@ role SipHash [ Int :$c = 2, Int :$d = 4, Int :$defkey = 0 ] does Sum::Partial {
method Buf () { self.buf8 }
}
+1; # Avoid sink-punning of last role
+
=begin pod
=head1 AUTHOR
View
@@ -13,7 +13,7 @@ class M1 does Sum::MDPad does Sum::Marshal::Raw {
has $.lastblock is rw;
method finalize { $.total }
method size { Inf }
- multi method do_add (Buf $block where { .elems == 64 }) {
+ multi method add (Buf $block where { .elems == 64 }) {
given self.pos_block_inc {
when Failure { return $_ };
}
@@ -87,7 +87,7 @@ class M2 does Sum::MDPad[:lengthtype<uint64_le>] does Sum::Marshal::Raw {
has $.lastblock is rw;
method finalize { $.total }
method size { Inf }
- multi method do_add (Buf $block where { .elems == 64 }) {
+ multi method add (Buf $block where { .elems == 64 }) {
given self.pos_block_inc {
when Failure { return $_ };
}

0 comments on commit d69a346

Please sign in to comment.