Skip to content

Commit

Permalink
fix attributes
Browse files Browse the repository at this point in the history
most importantly, several incorrect "in" attributes have been replaced with "inout", allowing reference types to be used.

also, a missing const was added, and the order was corrected.
  • Loading branch information
Moth-Tolias committed Jul 21, 2022
1 parent a997ade commit 64a87e9
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions source/ringbuffer.d
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ struct RingBuffer(DataType, size_t maxLength)
private size_t writeIndex;
private DataType[maxLength] data;

private auto _length() const
private auto _length() const @nogc nothrow pure @safe
{
//todo: there is almost certainly a better way to go about this
if (writeIndex == readIndex)
Expand Down Expand Up @@ -43,30 +43,30 @@ struct RingBuffer(DataType, size_t maxLength)

invariant(_length <= maxLength);

private auto next(in size_t rhs) @safe @nogc pure nothrow const
private auto next(in size_t rhs) const @nogc nothrow pure @safe
{
return rhs % (maxLength * 2);
}

private auto sanitize(in size_t rhs) @safe @nogc pure nothrow const
private auto sanitize(in size_t rhs) const @nogc nothrow pure @safe
{
return rhs % maxLength;
}

///
@property auto length() const
@property auto length() const @nogc nothrow pure @safe
{
return _length;
}

///
@property auto capacity() const
@property auto capacity() const @nogc nothrow pure @safe
{
return maxLength - length;
}

/// assignment
void opAssign(in DataType[] rhs)
void opAssign(inout DataType[] rhs)
in (rhs.length <= maxLength)
{
data[0 .. rhs.length] = rhs;
Expand All @@ -75,14 +75,14 @@ struct RingBuffer(DataType, size_t maxLength)
}

/// push to buffer
void push(in DataType rhs)
void push(inout DataType rhs)
{
data[sanitize(writeIndex)] = rhs;
writeIndex = next(writeIndex + 1);
}

/// ditto
void push(in DataType[] rhs)
void push(inout DataType[] rhs)
{
foreach(DataType d; rhs)
{
Expand All @@ -91,15 +91,15 @@ struct RingBuffer(DataType, size_t maxLength)
}

/// ditto
void opOpAssign(string op)(in DataType rhs)
void opOpAssign(string op)(inout DataType rhs)
if (op == "~")
in (length + 1 <= maxLength)
{
push(rhs);
}

/// ditto
void opOpAssign(string op)(in DataType[] rhs)
void opOpAssign(string op)(inout DataType[] rhs)
if (op == "~")
in (length + rhs.length <= maxLength)
{
Expand All @@ -110,7 +110,7 @@ struct RingBuffer(DataType, size_t maxLength)
DataType shift()
in (length > 0)
{
immutable result = data[sanitize(readIndex)];
auto result = data[sanitize(readIndex)];
readIndex = next(readIndex + 1);
return result;
}
Expand Down Expand Up @@ -142,7 +142,7 @@ struct RingBuffer(DataType, size_t maxLength)
{
RingBuffer!(int, 5) buff;
buff.push(69);
buff ~= 420; // equivilent to the push syntax
buff ~= 420; // equivalent to the push syntax
assert(buff.shift == 69);
assert(buff.shift == 420);

Expand Down

0 comments on commit 64a87e9

Please # to comment.