all: don't use a pointer receiver for many method calls

This is unnecessary because the values are passed by value in other
cases, and can in some cases lead to more (heap or stack) allocation
than is necessary.
This commit is contained in:
Ayke van Laethem 2024-01-03 16:10:21 +01:00 committed by Ron Evans
parent 83fba1b809
commit 5746ccfb60
4 changed files with 22 additions and 22 deletions

View file

@ -76,7 +76,7 @@ type deviceService struct {
}
// UUID returns the UUID for this DeviceService.
func (s *DeviceService) UUID() UUID {
func (s DeviceService) UUID() UUID {
return s.uuidWrapper
}
@ -89,7 +89,7 @@ func (s *DeviceService) UUID() UUID {
//
// Passing a nil slice of UUIDs will return a complete list of
// characteristics.
func (s *DeviceService) DiscoverCharacteristics(uuids []UUID) ([]DeviceCharacteristic, error) {
func (s DeviceService) DiscoverCharacteristics(uuids []UUID) ([]DeviceCharacteristic, error) {
cbuuids := []cbgo.UUID{}
s.device.prph.DiscoverCharacteristics(cbuuids, s.service)
@ -142,7 +142,7 @@ func (s *DeviceService) DiscoverCharacteristics(uuids []UUID) ([]DeviceCharacter
}
// Small helper to create a DeviceCharacteristic object.
func (s *DeviceService) makeCharacteristic(uuid UUID, dchar cbgo.Characteristic) DeviceCharacteristic {
func (s DeviceService) makeCharacteristic(uuid UUID, dchar cbgo.Characteristic) DeviceCharacteristic {
char := DeviceCharacteristic{
deviceCharacteristic: &deviceCharacteristic{
uuidWrapper: uuid,
@ -163,7 +163,7 @@ type DeviceCharacteristic struct {
type deviceCharacteristic struct {
uuidWrapper
service *DeviceService
service DeviceService
characteristic cbgo.Characteristic
callback func(buf []byte)
@ -171,7 +171,7 @@ type deviceCharacteristic struct {
}
// UUID returns the UUID for this DeviceCharacteristic.
func (c *DeviceCharacteristic) UUID() UUID {
func (c DeviceCharacteristic) UUID() UUID {
return c.uuidWrapper
}

View file

@ -27,7 +27,7 @@ type DeviceService struct {
}
// UUID returns the UUID for this DeviceService.
func (s *DeviceService) UUID() UUID {
func (s DeviceService) UUID() UUID {
return s.uuidWrapper
}
@ -41,7 +41,7 @@ func (s *DeviceService) UUID() UUID {
//
// On Linux with BlueZ, this just waits for the ServicesResolved signal (if
// services haven't been resolved yet) and uses this list of cached services.
func (d *Device) DiscoverServices(uuids []UUID) ([]DeviceService, error) {
func (d Device) DiscoverServices(uuids []UUID) ([]DeviceService, error) {
start := time.Now()
for {
@ -137,7 +137,7 @@ type DeviceCharacteristic struct {
}
// UUID returns the UUID for this DeviceCharacteristic.
func (c *DeviceCharacteristic) UUID() UUID {
func (c DeviceCharacteristic) UUID() UUID {
return c.uuidWrapper
}
@ -150,7 +150,7 @@ func (c *DeviceCharacteristic) UUID() UUID {
//
// Passing a nil slice of UUIDs will return a complete
// list of characteristics.
func (s *DeviceService) DiscoverCharacteristics(uuids []UUID) ([]DeviceCharacteristic, error) {
func (s DeviceService) DiscoverCharacteristics(uuids []UUID) ([]DeviceCharacteristic, error) {
var chars []DeviceCharacteristic
if len(uuids) > 0 {
// The caller wants to get a list of characteristics in a specific
@ -235,7 +235,7 @@ func (c DeviceCharacteristic) WriteWithoutResponse(p []byte) (n int, err error)
// changes.
//
// Users may call EnableNotifications with a nil callback to disable notifications.
func (c *DeviceCharacteristic) EnableNotifications(callback func(buf []byte)) error {
func (c DeviceCharacteristic) EnableNotifications(callback func(buf []byte)) error {
switch callback {
default:
if c.property != nil {
@ -295,7 +295,7 @@ func (c DeviceCharacteristic) GetMTU() (uint16, error) {
}
// Read reads the current characteristic value.
func (c *DeviceCharacteristic) Read(data []byte) (int, error) {
func (c DeviceCharacteristic) Read(data []byte) (int, error) {
options := make(map[string]interface{})
var result []byte
err := c.characteristic.Call("org.bluez.GattCharacteristic1.ReadValue", 0, options).Store(&result)

View file

@ -45,7 +45,7 @@ type DeviceService struct {
}
// UUID returns the UUID for this DeviceService.
func (s *DeviceService) UUID() UUID {
func (s DeviceService) UUID() UUID {
return s.uuid.UUID()
}
@ -167,7 +167,7 @@ type DeviceCharacteristic struct {
}
// UUID returns the UUID for this DeviceCharacteristic.
func (c *DeviceCharacteristic) UUID() UUID {
func (c DeviceCharacteristic) UUID() UUID {
return c.uuid.UUID()
}
@ -188,7 +188,7 @@ var discoveringCharacteristic struct {
//
// Passing a nil slice of UUIDs will return a complete
// list of characteristics.
func (s *DeviceService) DiscoverCharacteristics(uuids []UUID) ([]DeviceCharacteristic, error) {
func (s DeviceService) DiscoverCharacteristics(uuids []UUID) ([]DeviceCharacteristic, error) {
if discoveringCharacteristic.handle_value.Get() != 0 {
return nil, errAlreadyDiscovering
}
@ -424,7 +424,7 @@ var readingCharacteristic struct {
// Read reads the current characteristic value up to MTU length.
// A future enhancement would be to be able to retrieve a longer
// value by making multiple calls.
func (c *DeviceCharacteristic) Read(data []byte) (n int, err error) {
func (c DeviceCharacteristic) Read(data []byte) (n int, err error) {
// global will copy bytes from read operation into data slice
readingCharacteristic.value = data

View file

@ -137,7 +137,7 @@ type DeviceService struct {
}
// UUID returns the UUID for this DeviceService.
func (s *DeviceService) UUID() UUID {
func (s DeviceService) UUID() UUID {
return s.uuidWrapper
}
@ -150,7 +150,7 @@ func (s *DeviceService) UUID() UUID {
//
// Passing a nil slice of UUIDs will return a complete
// list of characteristics.
func (s *DeviceService) DiscoverCharacteristics(filterUUIDs []UUID) ([]DeviceCharacteristic, error) {
func (s DeviceService) DiscoverCharacteristics(filterUUIDs []UUID) ([]DeviceCharacteristic, error) {
getCharacteristicsOp, err := s.service.GetCharacteristicsWithCacheModeAsync(bluetooth.BluetoothCacheModeUncached)
if err != nil {
return nil, err
@ -234,20 +234,20 @@ type DeviceCharacteristic struct {
characteristic *genericattributeprofile.GattCharacteristic
properties genericattributeprofile.GattCharacteristicProperties
service *DeviceService
service DeviceService
}
// UUID returns the UUID for this DeviceCharacteristic.
func (c *DeviceCharacteristic) UUID() UUID {
func (c DeviceCharacteristic) UUID() UUID {
return c.uuidWrapper
}
func (c *DeviceCharacteristic) Properties() uint32 {
func (c DeviceCharacteristic) Properties() uint32 {
return uint32(c.properties)
}
// GetMTU returns the MTU for the characteristic.
func (c *DeviceCharacteristic) GetMTU() (uint16, error) {
func (c DeviceCharacteristic) GetMTU() (uint16, error) {
return c.service.device.session.GetMaxPduSize()
}
@ -314,7 +314,7 @@ func (c DeviceCharacteristic) write(p []byte, mode genericattributeprofile.GattW
}
// Read reads the current characteristic value.
func (c *DeviceCharacteristic) Read(data []byte) (int, error) {
func (c DeviceCharacteristic) Read(data []byte) (int, error) {
if c.properties&genericattributeprofile.GattCharacteristicPropertiesRead == 0 {
return 0, errNoRead
}