-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
PoolExtensions.cs
46 lines (42 loc) · 1.25 KB
/
PoolExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using System;
using MackySoft.XPool.Internal;
namespace MackySoft.XPool {
public static partial class PoolExtensions {
/// <summary>
/// Release the all pooled instances.
/// </summary>
/// <exception cref="ArgumentNullException"></exception>
public static void Clear (this IPool pool) {
if (pool == null) {
throw Error.ArgumentNullException(nameof(pool));
}
pool.ReleaseInstances(0);
}
/// <summary>
/// Return the instance to the pool and set reference to null.
/// </summary>
/// <exception cref="ArgumentNullException"></exception>
public static void Return<T> (this IPool<T> pool,ref T instance) {
if (pool == null) {
throw Error.ArgumentNullException(nameof(pool));
}
pool.Return(instance);
instance = default;
}
/// <summary>
/// Temporary rent an instance from pool. By using the using statement, you can safely return instance.
/// <code>
/// using (myPool.RentTemporary(out var instance)) {
/// // Use instance...
/// }
/// </code>
/// </summary>
public static RentInstance<T> RentTemporary<T> (this IPool<T> pool,out T instance) {
if (pool == null) {
throw Error.ArgumentNullException(nameof(pool));
}
instance = pool.Rent();
return new RentInstance<T>(pool,instance);
}
}
}