// Code generated by thriftgo (0.3.15) (fastgo). DO NOT EDIT.
package deep

import (
	"encoding/binary"
	"fmt"

	"github.com/cloudwego/gopkg/protocol/thrift"
)

func (p *TestStruct) BLength() int {
	if p == nil {
		return 1
	}
	off := 0

	// p.A ID:1 thrift.I64
	off += 3
	off += 8

	// p.B ID:2 thrift.STRING
	off += 3
	off += 4 + len(p.B)
	return off + 1
}

func (p *TestStruct) FastWrite(b []byte) int { return p.FastWriteNocopy(b, nil) }

func (p *TestStruct) FastWriteNocopy(b []byte, w thrift.NocopyWriter) int {
	if p == nil {
		b[0] = 0
		return 1
	}
	off := 0

	// p.A ID:1 thrift.I64
	b[off] = 10
	binary.BigEndian.PutUint16(b[off+1:], 1)
	off += 3
	binary.BigEndian.PutUint64(b[off:], uint64(p.A))
	off += 8

	// p.B ID:2 thrift.STRING
	b[off] = 11
	binary.BigEndian.PutUint16(b[off+1:], 2)
	off += 3
	off += thrift.Binary.WriteStringNocopy(b[off:], w, p.B)

	b[off] = 0
	return off + 1
}

func (p *TestStruct) FastRead(b []byte) (off int, err error) {
	var ftyp thrift.TType
	var fid int16
	var l int
	x := thrift.BinaryProtocol{}
	for {
		ftyp, fid, l, err = x.ReadFieldBegin(b[off:])
		off += l
		if err != nil {
			goto ReadFieldBeginError
		}
		if ftyp == thrift.STOP {
			break
		}
		switch uint32(fid)<<8 | uint32(ftyp) {
		case 0x10a: // p.A ID:1 thrift.I64
			p.A, l, err = x.ReadI64(b[off:])
			off += l
			if err != nil {
				goto ReadFieldError
			}
		case 0x20b: // p.B ID:2 thrift.STRING
			p.B, l, err = x.ReadString(b[off:])
			off += l
			if err != nil {
				goto ReadFieldError
			}
		default:
			l, err = x.Skip(b[off:], ftyp)
			off += l
			if err != nil {
				goto SkipFieldError
			}
		}
	}
	return
ReadFieldBeginError:
	return off, thrift.PrependError(fmt.Sprintf("%T read field begin error: ", p), err)
ReadFieldError:
	return off, thrift.PrependError(fmt.Sprintf("%T read field %d '%s' error: ", p, fid, fieldIDToName_TestStruct[fid]), err)
SkipFieldError:
	return off, thrift.PrependError(fmt.Sprintf("%T skip field %d type %d error: ", p, fid, ftyp), err)
}
