Skip to content

Commit 5a9df79

Browse files
committed
Use FnOnce in the Vec API
1 parent 7dfb3f4 commit 5a9df79

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

subspace/containers/vec.h

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
#include "subspace/containers/__private/vec_iter.h"
2424
#include "subspace/containers/__private/vec_marker.h"
2525
#include "subspace/containers/slice.h"
26+
#include "subspace/fn/fn_concepts.h"
27+
#include "subspace/fn/run_fn.h"
2628
#include "subspace/iter/from_iterator.h"
2729
#include "subspace/iter/into_iterator.h"
2830
#include "subspace/macros/compiler.h"
@@ -94,8 +96,10 @@ class Vec {
9496
/// This is highly unsafe, due to the number of invariants that aren’t
9597
/// checked:
9698
///
97-
/// * `ptr` must be heap allocated through malloc() (TODO: Want our own global
98-
/// allocator API).
99+
/// * `ptr` must be heap allocated with the same method as Vec uses
100+
/// internally, which is not currently stable. (TODO: Want our own global
101+
/// allocator API.) The only safe way to get this pointer is from
102+
/// `from_raw_parts()`.
99103
/// * `T` needs to have an alignment no more than what `ptr` was allocated
100104
/// with.
101105
/// * The size of `T` times the `capacity` (ie. the allocated size in bytes)
@@ -442,21 +446,21 @@ class Vec {
442446
void sort() { as_mut_slice().sort(); }
443447

444448
/// #[doc.inherit=[n]sus::[n]containers::[r]Slice::[f]sort_by]
445-
template <class F, int&...,
446-
class R = std::invoke_result_t<F, const T&, const T&>>
449+
template <::sus::fn::FnOnce<::sus::fn::NonVoid(const T&, const T&)> F,
450+
int&..., class R = std::invoke_result_t<F, const T&, const T&>>
447451
requires(::sus::ops::Ordering<R>)
448-
void sort_by(F compare) {
452+
void sort_by(F&& compare) {
449453
as_mut_slice().sort_by(sus::move(compare));
450454
}
451455

452456
/// #[doc.inherit=[n]sus::[n]containers::[r]Slice::[f]sort_unstable]
453457
void sort_unstable() { as_mut_slice().sort(); }
454458

455459
/// #[doc.inherit=[n]sus::[n]containers::[r]Slice::[f]sort_unstable_by]
456-
template <class F, int&...,
457-
class R = std::invoke_result_t<F, const T&, const T&>>
460+
template <::sus::fn::FnOnce<::sus::fn::NonVoid(const T&, const T&)> F,
461+
int&..., class R = std::invoke_result_t<F, const T&, const T&>>
458462
requires(::sus::ops::Ordering<R>)
459-
void sort_unstable_by(F compare) {
463+
void sort_unstable_by(F&& compare) {
460464
as_mut_slice().sort_by(sus::move(compare));
461465
}
462466

0 commit comments

Comments
 (0)