Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc3a0902

Browse files
jackpappaslatkin
jackpappas
authored andcommitted
Use generic WeakReference<'T> instead of WeakReference to enforce type safety.
1 parentbb67118 commitc3a0902

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

‎src/fsharp/InternalCollections.fs‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@ open System.Collections.Generic
77
#nowarn"44"// This construct is deprecated. This F# library function has been renamed. Use 'isSome' instead
88

99
[<StructuralEquality; NoComparison>]
10-
typeinternalValueStrength<'T>=
10+
typeinternalValueStrength<'Twhen'T:notstruct>=
1111
| Strongof'T
12+
#if FX_NO_GENERIC_WEAKREFERENCE
1213
| WeakofWeakReference
14+
#else
15+
| WeakofWeakReference<'T>
16+
#endif
1317

14-
typeinternalAgedLookup<'TKey,'TValue>(keepStrongly:int,areSame,?onStrongDiscard:('TValue->unit),?keepMax: int)=
18+
typeinternalAgedLookup<'TKey,'TValuewhen'TValue:notstruct>(keepStrongly:int,areSame,?onStrongDiscard:('TValue->unit),?keepMax: int)=
1519
/// The list of items stored. Youngest is at the end of the list.
1620
/// The choice of order is somewhat aribtrary. If the other way then adding
1721
/// items would be O(1) and removing O(N).
@@ -69,10 +73,15 @@ type internal AgedLookup<'TKey,'TValue>(keepStrongly:int, areSame, ?onStrongDisc
6973
match valuewith
7074
| Strong(value)->yield(key,value)
7175
| Weak(weakReference)->
76+
#if FX_NO_GENERIC_WEAKREFERENCE
7277
match weakReference.Targetwith
7378
|null->assert onStrongDiscard.IsNone;()
7479
| value->yield key,(value:?>'TValue)]
75-
80+
#else
81+
match weakReference.TryGetTarget()with
82+
|false,_->assert onStrongDiscard.IsNone;()
83+
|true, value->yield key, value]
84+
#endif
7685

7786
letAssignWithStrength(newdata,discard1)=
7887
letactualLength= List.length newdata
@@ -87,7 +96,11 @@ type internal AgedLookup<'TKey,'TValue>(keepStrongly:int, areSame, ?onStrongDisc
8796
lethandle=
8897
if n<weakThreshholdthen
8998
assert onStrongDiscard.IsNone;// it disappeared, we can't dispose
99+
#if FX_NO_GENERIC_WEAKREFERENCE
90100
Weak(WeakReference(v))
101+
#else
102+
Weak(WeakReference<_>(v))
103+
#endif
91104
else
92105
Strong(v)
93106
k,handle)
@@ -136,7 +149,7 @@ type internal AgedLookup<'TKey,'TValue>(keepStrongly:int, areSame, ?onStrongDisc
136149

137150

138151

139-
typeinternalMruCache<'TKey,'TValue>(keepStrongly,compute,areSame,?isStillValid: 'TKey*'TValue->bool,?areSameForSubsumption,?logComputedNewValue,?logUsedCachedValue,?onStrongDiscard,?keepMax)=
152+
typeinternalMruCache<'TKey,'TValuewhen'TValue:notstruct>(keepStrongly,compute,areSame,?isStillValid: 'TKey*'TValue->bool,?areSameForSubsumption,?logComputedNewValue,?logUsedCachedValue,?onStrongDiscard,?keepMax)=
140153

141154
/// Default behavior of areSameForSubsumption function is areSame
142155
letareSameForSubsumption= defaultArg areSameForSubsumption areSame

‎src/fsharp/InternalCollections.fsi‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace Internal.Utilities.Collections
55
/// Simple aging lookup table. When a member is accessed it's
66
/// moved to the top of the list and when there are too many elements
77
/// the least-recently-accessed element falls of the end.
8-
typeinternalAgedLookup<'TKey,'TValue>=
8+
typeinternalAgedLookup<'TKey,'TValuewhen'TValue:notstruct>=
99
new: keepStrongly:int
1010
* areSame:('TKey* 'TKey-> bool)
1111
* ?onStrongDiscard:('TValue-> unit)// this may only be set if keepTotal=keepStrongly, i.e. not weak entries
@@ -32,7 +32,7 @@ namespace Internal.Utilities.Collections
3232
/// This cache may age-out results that have been Set by the caller.
3333
/// Because of this, the caller must be able to tolerate values
3434
/// that aren't what was originally passed to the Set function.
35-
type internal MruCache<'TKey,'TValue>=
35+
type internal MruCache<'TKey,'TValue when 'TValue: not struct>=
3636
new: keepStrongly:int
3737
* compute:('TKey-> 'TValue)
3838
* areSame:('TKey* 'TKey-> bool)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp