Skip to content

Commit

Permalink
added more assertions and updated doc comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ultd committed Aug 19, 2023
1 parent 090d47c commit 3e42928
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion src/sync/mux.zig
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ pub fn RwMux(comptime T: type) type {
/// `Const` type is a const pointer adapter for different types as explained below:
///
/// - T is a non-pointer type (example: bool): `Const` is a `*const bool`
/// - T is a non-pointer type (example: [100]u8): `Const` is a `*const [100]u8`
/// - T is a pointer to One type (example: *usize): `Const` is `*const usize`
/// - T is a pointer to Slice type (example: []Packet): `Const` is a `[]const Packet`
///
Expand All @@ -265,7 +266,7 @@ pub fn RwMux(comptime T: type) type {
/// assert(Const(*[100]u8) == *const [100]u8)
/// ```
///
/// This is used in conjuction with `createConst` function and is a way to
/// This is used in conjuction with `toConst` function and is a way to
/// avoid `@TypeOf()` return type.
pub fn Const(comptime T: type) type {
switch (@typeInfo(T)) {
Expand Down Expand Up @@ -304,6 +305,28 @@ fn toConst(val: anytype) Const(@TypeOf(val)) {
}
}

/// `Mutable` type is a pointer adapter for different types as explained below:
///
/// - T is a non-pointer type (example: bool): `Mutable` is a `*bool`
/// - T is a non-pointer type (example: [100]u8): `Mutable` is a `*[100]u8`
/// - T is a pointer to One type (example: *usize): `Mutable` is `*usize`
/// - T is a pointer to Slice type (example: []Packet): `Mutable` is a `[]Packet`
///
/// ### Assertions:
///
/// ```
/// assert(Mutable(*usize) == *usize)
/// assert(Mutable(usize) == *usize)
/// assert(Mutable(*[]u8) == *[]u8)
/// assert(Mutable([]u8) == []u8)
/// assert(Mutable(*Packet) == *Packet)
/// assert(Mutable(Packet) == *Packet)
/// assert(Mutable([100]u8) == *[100]u8)
/// assert(Mutable(*[100]u8) == *[100]u8)
/// ```
///
/// This is used as return type of `mut` function and is a way to
/// avoid `@TypeOf()` return type or block statements.
pub fn Mutable(comptime T: type) type {
switch (@typeInfo(T)) {
.Pointer => |info| {
Expand Down Expand Up @@ -356,6 +379,17 @@ test "sync.mux: Const is correct" {
assert(@TypeOf(toConst(slice_of_arr)) == Const([]u8));
}

test "sync.mux: Mux handles slices properly" {
var arr = [3]u8{ 0, 1, 2 };
var slice = Mux([]u8).init(&arr);

var slice_locked = slice.lock();
defer slice_locked.unlock();

slice_locked.mut()[1] = 2;
try testing.expectEqualSlices(u8, &[_]u8{ 0, 2, 2 }, slice_locked.get());
}

test "sync.mux: Mutable is correct" {
const Packet = struct {
buffer: [100]u8 = [_]u8{0} ** 100,
Expand Down

0 comments on commit 3e42928

Please sign in to comment.