gvisor.dev/gvisor@v0.0.0-20240520182842-f9d4d51c7e0f/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  	"context"
    19  
    20  	"gvisor.dev/gvisor/pkg/state"
    21  )
    22  
    23  // +stateify type
    24  type truncatingUint8 struct {
    25  	save uint64
    26  	load uint8 `state:"nosave"`
    27  }
    28  
    29  func (t *truncatingUint8) StateSave(m state.Sink) {
    30  	m.Save(0, &t.save)
    31  }
    32  
    33  func (t *truncatingUint8) StateLoad(_ context.Context, m state.Source) {
    34  	m.Load(0, &t.load)
    35  	t.save = uint64(t.load)
    36  	t.load = 0
    37  }
    38  
    39  var _ state.SaverLoader = (*truncatingUint8)(nil)
    40  
    41  // +stateify type
    42  type truncatingUint16 struct {
    43  	save uint64
    44  	load uint16 `state:"nosave"`
    45  }
    46  
    47  func (t *truncatingUint16) StateSave(m state.Sink) {
    48  	m.Save(0, &t.save)
    49  }
    50  
    51  func (t *truncatingUint16) StateLoad(_ context.Context, m state.Source) {
    52  	m.Load(0, &t.load)
    53  	t.save = uint64(t.load)
    54  	t.load = 0
    55  }
    56  
    57  var _ state.SaverLoader = (*truncatingUint16)(nil)
    58  
    59  // +stateify type
    60  type truncatingUint32 struct {
    61  	save uint64
    62  	load uint32 `state:"nosave"`
    63  }
    64  
    65  func (t *truncatingUint32) StateSave(m state.Sink) {
    66  	m.Save(0, &t.save)
    67  }
    68  
    69  func (t *truncatingUint32) StateLoad(_ context.Context, m state.Source) {
    70  	m.Load(0, &t.load)
    71  	t.save = uint64(t.load)
    72  	t.load = 0
    73  }
    74  
    75  var _ state.SaverLoader = (*truncatingUint32)(nil)
    76  
    77  // +stateify type
    78  type truncatingInt8 struct {
    79  	save int64
    80  	load int8 `state:"nosave"`
    81  }
    82  
    83  func (t *truncatingInt8) StateSave(m state.Sink) {
    84  	m.Save(0, &t.save)
    85  }
    86  
    87  func (t *truncatingInt8) StateLoad(_ context.Context, m state.Source) {
    88  	m.Load(0, &t.load)
    89  	t.save = int64(t.load)
    90  	t.load = 0
    91  }
    92  
    93  var _ state.SaverLoader = (*truncatingInt8)(nil)
    94  
    95  // +stateify type
    96  type truncatingInt16 struct {
    97  	save int64
    98  	load int16 `state:"nosave"`
    99  }
   100  
   101  func (t *truncatingInt16) StateSave(m state.Sink) {
   102  	m.Save(0, &t.save)
   103  }
   104  
   105  func (t *truncatingInt16) StateLoad(_ context.Context, m state.Source) {
   106  	m.Load(0, &t.load)
   107  	t.save = int64(t.load)
   108  	t.load = 0
   109  }
   110  
   111  var _ state.SaverLoader = (*truncatingInt16)(nil)
   112  
   113  // +stateify type
   114  type truncatingInt32 struct {
   115  	save int64
   116  	load int32 `state:"nosave"`
   117  }
   118  
   119  func (t *truncatingInt32) StateSave(m state.Sink) {
   120  	m.Save(0, &t.save)
   121  }
   122  
   123  func (t *truncatingInt32) StateLoad(_ context.Context, m state.Source) {
   124  	m.Load(0, &t.load)
   125  	t.save = int64(t.load)
   126  	t.load = 0
   127  }
   128  
   129  var _ state.SaverLoader = (*truncatingInt32)(nil)
   130  
   131  // +stateify type
   132  type truncatingFloat32 struct {
   133  	save float64
   134  	load float32 `state:"nosave"`
   135  }
   136  
   137  func (t *truncatingFloat32) StateSave(m state.Sink) {
   138  	m.Save(0, &t.save)
   139  }
   140  
   141  func (t *truncatingFloat32) StateLoad(_ context.Context, m state.Source) {
   142  	m.Load(0, &t.load)
   143  	t.save = float64(t.load)
   144  	t.load = 0
   145  }
   146  
   147  var _ state.SaverLoader = (*truncatingFloat32)(nil)
   148  
   149  // +stateify type
   150  type truncatingComplex64 struct {
   151  	save complex128
   152  	load complex64 `state:"nosave"`
   153  }
   154  
   155  func (t *truncatingComplex64) StateSave(m state.Sink) {
   156  	m.Save(0, &t.save)
   157  }
   158  
   159  func (t *truncatingComplex64) StateLoad(_ context.Context, m state.Source) {
   160  	m.Load(0, &t.load)
   161  	t.save = complex128(t.load)
   162  	t.load = 0
   163  }
   164  
   165  var _ state.SaverLoader = (*truncatingComplex64)(nil)