github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/pkg/state/tests/integer.go (about)

     1  // Copyright 2018 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package tests
    16  
    17  import (
    18  	"github.com/SagerNet/gvisor/pkg/state"
    19  )
    20  
    21  // +stateify type
    22  type truncatingUint8 struct {
    23  	save uint64
    24  	load uint8 `state:"nosave"`
    25  }
    26  
    27  func (t *truncatingUint8) StateSave(m state.Sink) {
    28  	m.Save(0, &t.save)
    29  }
    30  
    31  func (t *truncatingUint8) StateLoad(m state.Source) {
    32  	m.Load(0, &t.load)
    33  	t.save = uint64(t.load)
    34  	t.load = 0
    35  }
    36  
    37  var _ state.SaverLoader = (*truncatingUint8)(nil)
    38  
    39  // +stateify type
    40  type truncatingUint16 struct {
    41  	save uint64
    42  	load uint16 `state:"nosave"`
    43  }
    44  
    45  func (t *truncatingUint16) StateSave(m state.Sink) {
    46  	m.Save(0, &t.save)
    47  }
    48  
    49  func (t *truncatingUint16) StateLoad(m state.Source) {
    50  	m.Load(0, &t.load)
    51  	t.save = uint64(t.load)
    52  	t.load = 0
    53  }
    54  
    55  var _ state.SaverLoader = (*truncatingUint16)(nil)
    56  
    57  // +stateify type
    58  type truncatingUint32 struct {
    59  	save uint64
    60  	load uint32 `state:"nosave"`
    61  }
    62  
    63  func (t *truncatingUint32) StateSave(m state.Sink) {
    64  	m.Save(0, &t.save)
    65  }
    66  
    67  func (t *truncatingUint32) StateLoad(m state.Source) {
    68  	m.Load(0, &t.load)
    69  	t.save = uint64(t.load)
    70  	t.load = 0
    71  }
    72  
    73  var _ state.SaverLoader = (*truncatingUint32)(nil)
    74  
    75  // +stateify type
    76  type truncatingInt8 struct {
    77  	save int64
    78  	load int8 `state:"nosave"`
    79  }
    80  
    81  func (t *truncatingInt8) StateSave(m state.Sink) {
    82  	m.Save(0, &t.save)
    83  }
    84  
    85  func (t *truncatingInt8) StateLoad(m state.Source) {
    86  	m.Load(0, &t.load)
    87  	t.save = int64(t.load)
    88  	t.load = 0
    89  }
    90  
    91  var _ state.SaverLoader = (*truncatingInt8)(nil)
    92  
    93  // +stateify type
    94  type truncatingInt16 struct {
    95  	save int64
    96  	load int16 `state:"nosave"`
    97  }
    98  
    99  func (t *truncatingInt16) StateSave(m state.Sink) {
   100  	m.Save(0, &t.save)
   101  }
   102  
   103  func (t *truncatingInt16) StateLoad(m state.Source) {
   104  	m.Load(0, &t.load)
   105  	t.save = int64(t.load)
   106  	t.load = 0
   107  }
   108  
   109  var _ state.SaverLoader = (*truncatingInt16)(nil)
   110  
   111  // +stateify type
   112  type truncatingInt32 struct {
   113  	save int64
   114  	load int32 `state:"nosave"`
   115  }
   116  
   117  func (t *truncatingInt32) StateSave(m state.Sink) {
   118  	m.Save(0, &t.save)
   119  }
   120  
   121  func (t *truncatingInt32) StateLoad(m state.Source) {
   122  	m.Load(0, &t.load)
   123  	t.save = int64(t.load)
   124  	t.load = 0
   125  }
   126  
   127  var _ state.SaverLoader = (*truncatingInt32)(nil)
   128  
   129  // +stateify type
   130  type truncatingFloat32 struct {
   131  	save float64
   132  	load float32 `state:"nosave"`
   133  }
   134  
   135  func (t *truncatingFloat32) StateSave(m state.Sink) {
   136  	m.Save(0, &t.save)
   137  }
   138  
   139  func (t *truncatingFloat32) StateLoad(m state.Source) {
   140  	m.Load(0, &t.load)
   141  	t.save = float64(t.load)
   142  	t.load = 0
   143  }
   144  
   145  var _ state.SaverLoader = (*truncatingFloat32)(nil)
   146  
   147  // +stateify type
   148  type truncatingComplex64 struct {
   149  	save complex128
   150  	load complex64 `state:"nosave"`
   151  }
   152  
   153  func (t *truncatingComplex64) StateSave(m state.Sink) {
   154  	m.Save(0, &t.save)
   155  }
   156  
   157  func (t *truncatingComplex64) StateLoad(m state.Source) {
   158  	m.Load(0, &t.load)
   159  	t.save = complex128(t.load)
   160  	t.load = 0
   161  }
   162  
   163  var _ state.SaverLoader = (*truncatingComplex64)(nil)