// WriteState writes a state somewhere in a binary format.
160177
func WriteState(d *State, dst io.Writer) error {
	// writing a nil state is a noop.
	if d == nil {
		return nil
	}

	// make sure we have no uninitialized fields
	d.init()

	// Make sure it is sorted
	d.sort()

	// Ensure the version is set
	d.Version = StateVersion

	// If the TFVersion is set, verify it. We used to just set the version
	// here, but this isn't safe since it changes the MD5 sum on some remote
	// state storage backends such as Atlas. We now leave it be if needed.
	if d.TFVersion != "" {
		if _, err := version.NewVersion(d.TFVersion); err != nil {
			return fmt.Errorf(
				"Error writing state, invalid version: %s\n\n"+
					"The Terraform version when writing the state must be a semantic\n"+
					"version.",
				d.TFVersion)
		}
	}

	// Encode the data in a human-friendly way
	data, err := json.MarshalIndent(d, "", "    ")
	if err != nil {
		return fmt.Errorf("Failed to encode state: %s", err)
	}

	// We append a newline to the data because MarshalIndent doesn't
	data = append(data, '\n')

	// Write the data out to the dst
	if _, err := io.Copy(dst, bytes.NewReader(data)); err != nil {
		return fmt.Errorf("Failed to write state: %v", err)
	}

	return nil
}
279653
func (s *ReservedElasticsearchInstance) SetState(v string) *ReservedElasticsearchInstance {
	s.State = &v
	return s
}
399231
func (s *ExportSnapshotRecord) SetState(v string) *ExportSnapshotRecord {
	s.State = &v
	return s
}
250892
func (d *FakeStateDriver) Write(key string, value []byte) error {
	val := valueData{value: value}
	d.TestState[key] = val

	return nil
}
432471
func (s *ListDirectoriesInput) SetState(v string) *ListDirectoriesInput {
	s.State = &v
	return s
}
1063647
func (a *Automaton) createState() int {
	state := len(a.states) / 2
	a.states = append(a.states, -1, 0)
	return state
}
99432
func (s *LifecyclePolicySummary) SetState(v string) *LifecyclePolicySummary {
	s.State = &v
	return s
}
552017
func (s *DnetOperState) Write() error {
	key := fmt.Sprintf(docknetOperPath, s.ID)
	return s.StateDriver.WriteState(key, s, json.Marshal)
}
463530
func (d *ConsulStateDriver) ReadState(key string, value core.State,
	unmarshal func([]byte, interface{}) error) error {
	key = processKey(key)
	encodedState, err := d.Read(key)
	if err != nil {
		return err
	}

	return unmarshal(encodedState, value)
}
693429
func (f *StateFile) Write(st *State) error {
	if err := st.validate(); err != nil {
		return errors.Trace(err)
	}
	return utils.WriteYaml(f.path, st)
}
644204
func SaveState(db dbm.DB, state State) {
	saveState(db, state, stateKey)
}
855896
func (s *Malware) SetState(v string) *Malware {
	s.State = &v
	return s
}
938709
func (f *File) State() (ret []FilePieceState) {
	f.t.cl.rLock()
	defer f.t.cl.rUnlock()
	pieceSize := int64(f.t.usualPieceSize())
	off := f.offset % pieceSize
	remaining := f.length
	for i := pieceIndex(f.offset / pieceSize); ; i++ {
		if remaining == 0 {
			break
		}
		len1 := pieceSize - off
		if len1 > remaining {
			len1 = remaining
		}
		ps := f.t.pieceState(i)
		ret = append(ret, FilePieceState{len1, ps})
		off = 0
		remaining -= len1
	}
	return
}
1079016
func putState(stor storage.StorageWriter, data []byte) error {
	logger.Debugf("putting %q to bootstrap storage %T", StateFile, stor)
	return stor.Put(StateFile, bytes.NewBuffer(data), int64(len(data)))
}
654592
func (s *TrafficPolicyInstance) SetState(v string) *TrafficPolicyInstance {
	s.State = &v
	return s
}
230333
func (s *DescribeHapgOutput) SetState(v string) *DescribeHapgOutput {
	s.State = &v
	return s
}
45945
func (s *AvailabilityZone) SetState(v string) *AvailabilityZone {
	s.State = &v
	return s
}