Skip to content

Commit

Permalink
Properly hint for notnull type requirements
Browse files Browse the repository at this point in the history
  • Loading branch information
smoogipoo committed Jan 29, 2024
1 parent 5ee40fd commit 8ff8f8c
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -301,10 +301,7 @@ public void TestDependenciesOverrideParent(string name, Type parent)
dependencies = new DependencyContainer(dependencies);
dependencies.CacheAs(2, info);

Assert.Multiple(() =>
{
Assert.AreEqual(2, dependencies.Get<int>(info));
});
Assert.Multiple(() => { Assert.AreEqual(2, dependencies.Get<int>(info)); });
}

[Test]
Expand Down Expand Up @@ -333,8 +330,19 @@ public void TestResolveNullStruct()
Assert.That(new DependencyContainer().Get<CancellationToken?>(), Is.Null);
}

[Test]
public void TestModifyBoxedStruct()
{
var dependencies = new DependencyContainer();
dependencies.CacheAs<IBaseInterface>(new BaseStructObject { TestValue = 1 });
dependencies.Get<IBaseInterface>().TestValue = 2;

Assert.That(dependencies.Get<IBaseInterface>().TestValue, Is.EqualTo(2));
}

private interface IBaseInterface
{
int TestValue { get; set; }
}

private class BaseObject
Expand All @@ -344,6 +352,7 @@ private class BaseObject

private struct BaseStructObject : IBaseInterface
{
public int TestValue { get; set; }
}

private class DerivedObject : BaseObject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,10 +304,7 @@ public void TestDependenciesOverrideParent(string name, Type parent)
dependencies = new DependencyContainer(dependencies);
dependencies.CacheAs(2, info);

Assert.Multiple(() =>
{
Assert.AreEqual(2, dependencies.Get<int>(info));
});
Assert.Multiple(() => { Assert.AreEqual(2, dependencies.Get<int>(info)); });
}

[Test]
Expand Down Expand Up @@ -336,8 +333,19 @@ public void TestResolveNullStruct()
Assert.That(new DependencyContainer().Get<CancellationToken?>(), Is.Null);
}

[Test]
public void TestModifyBoxedStruct()
{
var dependencies = new DependencyContainer();
dependencies.CacheAs<IBaseInterface>(new BaseStructObject { TestValue = 1 });
dependencies.Get<IBaseInterface>().TestValue = 2;

Assert.That(dependencies.Get<IBaseInterface>().TestValue, Is.EqualTo(2));
}

private interface IBaseInterface
{
int TestValue { get; set; }
}

private class BaseObject
Expand All @@ -347,6 +355,7 @@ private class BaseObject

private struct BaseStructObject : IBaseInterface
{
public int TestValue { get; set; }
}

private class DerivedObject : BaseObject
Expand Down
21 changes: 21 additions & 0 deletions osu.Framework/Allocation/DependencyContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,29 @@ public void Cache(object instance, CacheInfo info)
CacheAs(instance.GetType(), info, instance);
}

/// <summary>
/// Caches an instance of a type as its most derived type. This instance will be returned each time you <see cref="Get{T}()"/>.
/// </summary>
/// <param name="instance">The instance to cache.</param>
public void Cache<T>(T instance)
where T : notnull
=> Cache(instance, default);

/// <summary>
/// Caches an instance of a type as its most derived type. This instance will be returned each time you <see cref="Get{T}()"/>.
/// </summary>
/// <param name="instance">The instance to cache.</param>
/// <param name="info">Extra information to identify <paramref name="instance"/> in the cache.</param>
public void Cache<T>(T instance, CacheInfo info)
where T : notnull
=> CacheAs(instance.GetType(), info, instance);

/// <summary>
/// Caches an instance of a type as a type of <typeparamref name="T"/>. This instance will be returned each time you <see cref="Get{T}()"/>.
/// </summary>
/// <param name="instance">The instance to cache. Must be or derive from <typeparamref name="T"/>.</param>
public void CacheAs<T>(T instance)
where T : notnull
=> CacheAs(instance, default);

/// <summary>
Expand All @@ -57,6 +75,7 @@ public void CacheAs<T>(T instance)
/// <param name="instance">The instance to cache. Must be or derive from <typeparamref name="T"/>.</param>
/// <param name="info">Extra information to identify <paramref name="instance"/> in the cache.</param>
public void CacheAs<T>(T instance, CacheInfo info)
where T : notnull
=> CacheAs(typeof(T), info, instance);

/// <summary>
Expand All @@ -65,6 +84,7 @@ public void CacheAs<T>(T instance, CacheInfo info)
/// <param name="type">The type to cache <paramref name="instance"/> as.</param>
/// <param name="instance">The instance to cache. Must be or derive from <paramref name="type"/>.</param>
public void CacheAs<T>(Type type, T instance)
where T : notnull
=> CacheAs(type, instance, default);

/// <summary>
Expand All @@ -74,6 +94,7 @@ public void CacheAs<T>(Type type, T instance)
/// <param name="instance">The instance to cache. Must be or derive from <paramref name="type"/>.</param>
/// <param name="info">Extra information to identify <paramref name="instance"/> in the cache.</param>
public void CacheAs<T>(Type type, T instance, CacheInfo info)
where T : notnull
=> CacheAs(type, info, instance);

/// <summary>
Expand Down

0 comments on commit 8ff8f8c

Please sign in to comment.