Skip to content

Commit

Permalink
Add traits to forbid cloning and serialization (#388)
Browse files Browse the repository at this point in the history
  • Loading branch information
trowski authored Apr 9, 2022
1 parent f37d18c commit 3f679db
Show file tree
Hide file tree
Showing 11 changed files with 53 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/CompositeCancellation.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

final class CompositeCancellation implements Cancellation
{
use ForbidCloning;
use ForbidSerialization;

/** @var array<int, array{Cancellation, string}> */
private array $cancellations = [];

Expand Down
3 changes: 3 additions & 0 deletions src/DeferredCancellation.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@
*/
final class DeferredCancellation
{
use ForbidCloning;
use ForbidSerialization;

private readonly Internal\Cancellable $source;
private readonly Cancellation $cancellation;

Expand Down
3 changes: 3 additions & 0 deletions src/DeferredFuture.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
*/
final class DeferredFuture
{
use ForbidCloning;
use ForbidSerialization;

/** @var Internal\FutureState<T> */
private readonly Internal\FutureState $state;

Expand Down
11 changes: 11 additions & 0 deletions src/ForbidCloning.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Amp;

trait ForbidCloning
{
final protected function __clone()
{
throw new \Error(__CLASS__ . ' does not support cloning');
}
}
11 changes: 11 additions & 0 deletions src/ForbidSerialization.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Amp;

trait ForbidSerialization
{
final public function __serialize(): never
{
throw new \Error(__CLASS__ . ' does not support serialization');
}
}
3 changes: 3 additions & 0 deletions src/Future.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
*/
final class Future
{
use ForbidCloning;
use ForbidSerialization;

/**
* Iterate over the given futures in completion order.
*
Expand Down
5 changes: 5 additions & 0 deletions src/Internal/Cancellable.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Amp\Cancellation;
use Amp\CancelledException;
use Amp\ForbidCloning;
use Amp\ForbidSerialization;
use Revolt\EventLoop;

/**
Expand All @@ -13,6 +15,9 @@
*/
final class Cancellable implements Cancellation
{
use ForbidCloning;
use ForbidSerialization;

private string $nextId = "a";

/** @var \Closure[] */
Expand Down
5 changes: 5 additions & 0 deletions src/Internal/FutureIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Amp\Internal;

use Amp\Cancellation;
use Amp\ForbidCloning;
use Amp\ForbidSerialization;
use Amp\Future;
use Amp\NullCancellation;
use Revolt\EventLoop;
Expand All @@ -15,6 +17,9 @@
*/
final class FutureIterator
{
use ForbidCloning;
use ForbidSerialization;

/**
* @var FutureIteratorQueue<Tk, Tv>
*/
Expand Down
5 changes: 5 additions & 0 deletions src/Internal/WrappedCancellation.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@
namespace Amp\Internal;

use Amp\Cancellation;
use Amp\ForbidCloning;
use Amp\ForbidSerialization;

/**
* @internal
*/
final class WrappedCancellation implements Cancellation
{
use ForbidCloning;
use ForbidSerialization;

public function __construct(
private readonly Cancellation $cancellation
) {
Expand Down
3 changes: 3 additions & 0 deletions src/TimeoutCancellation.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
*/
final class TimeoutCancellation implements Cancellation
{
use ForbidCloning;
use ForbidSerialization;

private readonly string $watcher;

private readonly Cancellation $cancellation;
Expand Down
2 changes: 1 addition & 1 deletion test/Cancellation/CompositeCancellationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function testBenchmark(): void

// Asserts
if ($firstMemoryMeasure > 0) {
self::assertEquals($firstMemoryMeasure, \memory_get_usage(true));
self::assertLessThanOrEqual($firstMemoryMeasure, \memory_get_usage(true));
}
}
}
Expand Down

0 comments on commit 3f679db

Please sign in to comment.