duration.go 2.5 KB

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