Debian Codegraph @

server/elastic/elastic.go

raw
1 // Copyright 2025 The NATS Authors
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package elastic
15
16 import (
17 "weak"
18 )
19
20 func Make[T any](ptr *T) *Pointer[T] {
21 return &Pointer[T]{
22 weak: weak.Make(ptr),
23 }
24 }
25
26 type Pointer[T any] struct {
27 weak weak.Pointer[T]
28 strong *T
29 }
30
31 func (e *Pointer[T]) Set(ptr *T) {
32 e.weak = weak.Make(ptr)
33 if e.strong != nil {
34 e.strong = ptr
35 }
36 }
37
38 func (e *Pointer[T]) Strengthen() {
39 if e == nil || e.strong != nil {
40 return
41 }
42 e.strong = e.weak.Value()
43 }
44
45 func (e *Pointer[T]) Weaken() {
46 if e == nil || e.strong == nil {
47 return
48 }
49 e.strong = nil
50 }
51
52 func (e *Pointer[T]) Value() *T {
53 if e == nil {
54 return nil
55 }
56 if e.strong != nil {
57 return e.strong
58 }
59 return e.weak.Value()
60 }