Skip to content

Separate methods v.s. "mode" parameters #58

Closed
rust-lang/rust
#74850
@SimonSapin

Description

@SimonSapin

#41 has split realloc into separate grow and shrink methods. However as of this writing some methods take AllocInit or ReallocPlacement parameters that are effectively boolean mode switches.

Part of the reasoning for separate methods is that most callers are always in one of the two cases, so separating saves a branch in the allocator implementation. The same seems to apply to AllocInit and ReallocPlacement.

The current design with 5 methods seems inconsistent to me. I think the trait should pick one principle among easier/nicer-looking API or single-purpose methods. This would give either 4 methods:

  • alloc
  • dealloc
  • realloc
  • by_ref

Or 10 methods:

  • alloc
  • alloc_zeroed
  • dealloc
  • grow
  • grow_zeroed
  • grow_in_place
  • grow_in_place_zeroed
  • shrink
  • shrink_in_place
  • by_ref

I tend to favor the latter. The reason against it given in #44 is that this "blows up the trait". But I feel 10 methods is not that bad, especially given that they are not 10 completely different behaviors that need to be learned independently, but follow a pattern that’s reflected in their names.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions