khenaidoo | d948f77 | 2021-08-11 17:49:24 -0400 | [diff] [blame] | 1 | // @generated Code generated by gen-atomicwrapper. |
| 2 | |
| 3 | // Copyright (c) 2020 Uber Technologies, Inc. |
| 4 | // |
| 5 | // Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | // of this software and associated documentation files (the "Software"), to deal |
| 7 | // in the Software without restriction, including without limitation the rights |
| 8 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | // copies of the Software, and to permit persons to whom the Software is |
| 10 | // furnished to do so, subject to the following conditions: |
| 11 | // |
| 12 | // The above copyright notice and this permission notice shall be included in |
| 13 | // all copies or substantial portions of the Software. |
| 14 | // |
| 15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 | // THE SOFTWARE. |
| 22 | |
| 23 | package atomic |
| 24 | |
| 25 | import ( |
| 26 | "encoding/json" |
| 27 | "time" |
| 28 | ) |
| 29 | |
| 30 | // Duration is an atomic type-safe wrapper for time.Duration values. |
| 31 | type Duration struct { |
| 32 | _ nocmp // disallow non-atomic comparison |
| 33 | |
| 34 | v Int64 |
| 35 | } |
| 36 | |
| 37 | var _zeroDuration time.Duration |
| 38 | |
| 39 | // NewDuration creates a new Duration. |
| 40 | func NewDuration(v time.Duration) *Duration { |
| 41 | x := &Duration{} |
| 42 | if v != _zeroDuration { |
| 43 | x.Store(v) |
| 44 | } |
| 45 | return x |
| 46 | } |
| 47 | |
| 48 | // Load atomically loads the wrapped time.Duration. |
| 49 | func (x *Duration) Load() time.Duration { |
| 50 | return time.Duration(x.v.Load()) |
| 51 | } |
| 52 | |
| 53 | // Store atomically stores the passed time.Duration. |
| 54 | func (x *Duration) Store(v time.Duration) { |
| 55 | x.v.Store(int64(v)) |
| 56 | } |
| 57 | |
| 58 | // CAS is an atomic compare-and-swap for time.Duration values. |
| 59 | func (x *Duration) CAS(o, n time.Duration) bool { |
| 60 | return x.v.CAS(int64(o), int64(n)) |
| 61 | } |
| 62 | |
| 63 | // Swap atomically stores the given time.Duration and returns the old |
| 64 | // value. |
| 65 | func (x *Duration) Swap(o time.Duration) time.Duration { |
| 66 | return time.Duration(x.v.Swap(int64(o))) |
| 67 | } |
| 68 | |
| 69 | // MarshalJSON encodes the wrapped time.Duration into JSON. |
| 70 | func (x *Duration) MarshalJSON() ([]byte, error) { |
| 71 | return json.Marshal(x.Load()) |
| 72 | } |
| 73 | |
| 74 | // UnmarshalJSON decodes a time.Duration from JSON. |
| 75 | func (x *Duration) UnmarshalJSON(b []byte) error { |
| 76 | var v time.Duration |
| 77 | if err := json.Unmarshal(b, &v); err != nil { |
| 78 | return err |
| 79 | } |
| 80 | x.Store(v) |
| 81 | return nil |
| 82 | } |