11const std = @import ("std" );
22const builtin = @import ("builtin" );
33
4+ /// This variable is `true` if an atomic reference-counter is used for `Arc`, `false` otherwise.
5+ ///
6+ /// If the target is single-threaded, `Arc` is optimized to a regular `Rc`.
7+ pub const atomic_arc = ! builtin .single_threaded or (builtin .target .isWasm () and std .Target .wasm .featureSetHas (builtin .cpu .features , .atomics ));
8+
49/// A single threaded, strong reference to a reference-counted value.
510pub fn Rc (comptime T : type ) type {
611 return struct {
@@ -128,15 +133,13 @@ pub fn Rc(comptime T: type) type {
128133 }
129134
130135 /// Total size (in bytes) of the reference counted value on the heap.
131- /// This value accounts for the extra memory required to count the references,
132- /// and is valid for single and multi-threaded refrence counters.
136+ /// This value accounts for the extra memory required to count the references.
133137 pub fn innerSize () comptime_int {
134138 return Inner .innerSize ();
135139 }
136140
137141 /// Alignment (in bytes) of the reference counted value on the heap.
138- /// This value accounts for the extra memory required to count the references,
139- /// and is valid for single and multi-threaded refrence counters.
142+ /// This value accounts for the extra memory required to count the references.
140143 pub fn innerAlign () comptime_int {
141144 return Inner .innerAlign ();
142145 }
@@ -147,7 +150,7 @@ pub fn Rc(comptime T: type) type {
147150
148151 /// A single threaded, weak reference to a reference-counted value.
149152 pub const Weak = struct {
150- inner : ? * align ( @alignOf ( Inner )) anyopaque = null ,
153+ inner : ? * Inner = null ,
151154 alloc : std.mem.Allocator ,
152155
153156 /// Creates a new weak reference.
@@ -236,7 +239,7 @@ pub fn Rc(comptime T: type) type {
236239
237240/// A multi-threaded, strong reference to a reference-counted value.
238241pub fn Arc (comptime T : type ) type {
239- if (builtin . single_threaded ) {
242+ if (! atomic_arc ) {
240243 return Rc (T );
241244 }
242245
@@ -355,15 +358,13 @@ pub fn Arc(comptime T: type) type {
355358 }
356359
357360 /// Total size (in bytes) of the reference counted value on the heap.
358- /// This value accounts for the extra memory required to count the references,
359- /// and is valid for single and multi-threaded refrence counters.
361+ /// This value accounts for the extra memory required to count the references.
360362 pub fn innerSize () comptime_int {
361363 return Inner .innerSize ();
362364 }
363365
364366 /// Alignment (in bytes) of the reference counted value on the heap.
365- /// This value accounts for the extra memory required to count the references,
366- /// and is valid for single and multi-threaded refrence counters.
367+ /// This value accounts for the extra memory required to count the references.
367368 pub fn innerAlign () comptime_int {
368369 return Inner .innerAlign ();
369370 }
@@ -374,7 +375,7 @@ pub fn Arc(comptime T: type) type {
374375
375376 /// A multi-threaded, weak reference to a reference-counted value.
376377 pub const Weak = struct {
377- inner : ? * align ( @alignOf ( Inner )) anyopaque = null ,
378+ inner : ? * Inner = null ,
378379 alloc : std.mem.Allocator ,
379380
380381 /// Creates a new weak reference.
@@ -449,15 +450,13 @@ pub fn Arc(comptime T: type) type {
449450 }
450451
451452 /// Total size (in bytes) of the reference counted value on the heap.
452- /// This value accounts for the extra memory required to count the references,
453- /// and is valid for single and multi-threaded refrence counters.
453+ /// This value accounts for the extra memory required to count the references.
454454 pub fn innerSize () comptime_int {
455455 return Inner .innerSize ();
456456 }
457457
458458 /// Alignment (in bytes) of the reference counted value on the heap.
459- /// This value accounts for the extra memory required to count the references,
460- /// and is valid for single and multi-threaded refrence counters.
459+ /// This value accounts for the extra memory required to count the references.
461460 pub fn innerAlign () comptime_int {
462461 return Inner .innerAlign ();
463462 }
@@ -468,3 +467,13 @@ pub fn Arc(comptime T: type) type {
468467 };
469468 };
470469}
470+
471+ /// Creates a new `Rc` inferring the type of `value`
472+ pub fn rc (alloc : std.mem.Allocator , value : anytype ) std.mem.Allocator.Error ! Rc (@TypeOf (value )) {
473+ return Rc (@TypeOf (value )).init (alloc , value );
474+ }
475+
476+ /// Creates a new `Arc` inferring the type of `value`
477+ pub fn arc (alloc : std.mem.Allocator , value : anytype ) std.mem.Allocator.Error ! Arc (@TypeOf (value )) {
478+ return Arc (@TypeOf (value )).init (alloc , value );
479+ }
0 commit comments