Skip to content

Commit

Permalink
Removed Contains
Browse files Browse the repository at this point in the history
  • Loading branch information
stephen-hawley committed Dec 11, 2024
1 parent efdec90 commit f7026b2
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 39 deletions.
21 changes: 7 additions & 14 deletions src/Swift.Runtime/src/Metadata/ITypeMetadataCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,20 @@ namespace Swift.Runtime;
public interface ITypeMetadataCache
{
/// <summary>
/// Returns true if and only if the cache contains an entry for t
/// </summary>
/// <param name="type">The type to look up in the cache</param>
/// <returns>true if and only if the cache contains t, false otherwise</returns>
bool Contains(Type type);

/// <summary>
/// Returns true if the cache contains an entry for t and sets metadata to the resulting value,
/// Returns true if the cache contains an entry for type and sets metadata to the resulting value,
/// otherwise it returns false and metadata will be null.
/// </summary>
/// <param name="type"></param>
/// <param name="metadata"></param>
/// <param name="type">The type to look up in the cache</param>
/// <param name="metadata">The resulting metadata if found</param>
/// <returns>true if the lookup was successful, false otherwise</returns>
bool TryGet(Type type, [NotNullWhen(true)]out TypeMetadata? metadata);

/// <summary>
/// Gets the TypeMetadata for the given Type t or if it is not present,
/// Gets the TypeMetadata for the given Type type or if it is not present,
/// adds it to the cache using the given factory to generate the value.
/// </summary>
/// <param name="type"></param>
/// <param name="metadataFactory"></param>
/// <returns>The TypeMetadata associated with the give Type t</returns>
/// <param name="type">The type to look up in the cache</param>
/// <param name="metadataFactory">a factory to generate the TypeMetadata if not present</param>
/// <returns>The TypeMetadata associated with the give Type type</returns>
TypeMetadata GetOrAdd(Type type, Func<Type, TypeMetadata> metadataFactory);
}
25 changes: 7 additions & 18 deletions src/Swift.Runtime/src/Metadata/TypeMetadataCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,12 @@ public TypeMetadataCache(IEnumerable<(Type, TypeMetadata)> initialValues)
}
}


/// <summary>
/// Returns true if and only if the cache contains an entry for t.
/// </summary>
/// <param name="type">The type to look up in the cache</param>
/// <returns>true if and only if the cache contains t, false otherwise</returns>
public bool Contains(Type type)
{
return cache.ContainsKey(type);
}

/// <summary>
/// Returns true if the cache contains an entry for t and sets metadata to the resulting value,
/// Returns true if the cache contains an entry for type and sets metadata to the resulting value,
/// otherwise it returns false and metadata will be null.
/// </summary>
/// <param name="type"></param>
/// <param name="metadata"></param>
/// <param name="type">The type to look up in the cache</param>
/// <param name="metadata">The resulting metadata if found</param>
/// <returns>true if the lookup was successful, false otherwise</returns>
public bool TryGet(Type type, [NotNullWhen(true)] out TypeMetadata? metadata)
{
Expand All @@ -68,12 +57,12 @@ public bool TryGet(Type type, [NotNullWhen(true)] out TypeMetadata? metadata)
}

/// <summary>
/// Gets the TypeMetadata for the given Type t or if it is not present,
/// Gets the TypeMetadata for the given Type type or if it is not present,
/// adds it to the cache using the given factory to generate the value.
/// </summary>
/// <param name="type"></param>
/// <param name="metadataFactory"></param>
/// <returns>The TypeMetadata associated with the give Type t</returns>
/// <param name="type">The type to look up in the cache</param>
/// <param name="metadataFactory">a factory to generate the TypeMetadata if not present</param>
/// <returns>The TypeMetadata associated with the give Type type</returns>
public TypeMetadata GetOrAdd(Type type, Func<Type, TypeMetadata> metadataFactory)
{
return cache.GetOrAdd(type, metadataFactory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,6 @@ static TypeMetadata MakePhonyMetadata(int value)
return (TypeMetadata)(ci.Invoke(new object[] { p }));
}

[Fact]
public static void CacheDoesntContain()
{
Assert.False(TypeMetadata.Cache.Contains(typeof(System.EventArgs)));
}

[Fact]
public static void CacheWorks()
{
Expand All @@ -51,7 +45,7 @@ public static void CacheWorks()
{
return fakeMeta;
});
Assert.True(TypeMetadata.Cache.Contains(typeof(System.Convert)));
Assert.True(TypeMetadata.Cache.TryGet(typeof(System.Convert), out var result));
}

[Fact]
Expand Down

0 comments on commit f7026b2

Please sign in to comment.