Returns the size that a structure will encode into. This fuction doesn't check that Write() will succeed, and may return unexpectedly large results for those structures that Write() will fail on.
(v any)
| 233 | // and may return unexpectedly large results for those |
| 234 | // structures that Write() will fail on. |
| 235 | func wireSize(v any) int { |
| 236 | value := reflect.ValueOf(v) |
| 237 | // Indirect is safe to call on non-pointers, and |
| 238 | // will simply return the same value in such cases |
| 239 | value = reflect.Indirect(value) |
| 240 | size := int(0) |
| 241 | |
| 242 | switch value.Kind() { |
| 243 | case reflect.Slice: |
| 244 | for i := 0; i < value.Len(); i++ { |
| 245 | if value.Index(i).CanInterface() { |
| 246 | size += wireSize(value.Index(i).Interface()) |
| 247 | } else { |
| 248 | size += int(value.Index(i).Type().Size()) //nolint:gosec // RTCP element sizes are small and bounded |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | case reflect.Struct: |
| 253 | for i := 0; i < value.NumField(); i++ { |
| 254 | encoding := value.Type().Field(i).Tag.Get("encoding") |
| 255 | if encoding == omit { |
| 256 | continue |
| 257 | } |
| 258 | if value.Field(i).CanInterface() { |
| 259 | size += wireSize(value.Field(i).Interface()) |
| 260 | } else { |
| 261 | size += int(value.Field(i).Type().Size()) // nolint:gosec // Size comes from type system and is bounded |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | default: |
| 266 | size = int(value.Type().Size()) // nolint:gosec // Size comes from type system and is bounde |
| 267 | } |
| 268 | |
| 269 | return size |
| 270 | } |
searching dependent graphs…