Skip to content

Commit

Permalink
added Weak.fromValuePtr
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Andreba committed Jun 25, 2023
1 parent 9611fac commit d5fc419
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,15 @@ pub fn Rc(comptime T: type) type {
return Weak{ .inner = ptr, .alloc = parent.alloc };
}

/// Creates a new weak reference object from a pointer to it's underlying value,
/// without increasing the weak count.
pub fn fromValuePtr(value: *T, alloc: std.mem.Allocator) Weak {
return .{
.inner = @fieldParentPtr(Inner, "value", value),
.alloc = alloc
};
}

/// Gets the number of strong references to this value.
pub fn strongCount(self: *const Weak) usize {
return (self.innerPtr() orelse return 0).strong;
Expand Down Expand Up @@ -385,6 +394,15 @@ pub fn Arc(comptime T: type) type {
return Weak{ .inner = ptr, .alloc = parent.alloc };
}

/// Creates a new weak reference object from a pointer to it's underlying value,
/// without increasing the weak count.
pub fn fromValuePtr(value: *T, alloc: std.mem.Allocator) Weak {
return .{
.inner = @fieldParentPtr(Inner, "value", value),
.alloc = alloc
};
}

/// Gets the number of strong references to this value.
pub fn strongCount(self: *const Weak) usize {
const ptr = self.innerPtr() orelse return 0;
Expand Down

0 comments on commit d5fc419

Please sign in to comment.