Skip to content

Commit

Permalink
karm-base: Allow constructing Cursor<T> and MutCursor<T> from Slice.
Browse files Browse the repository at this point in the history
  • Loading branch information
sleepy-monax committed Oct 29, 2024
1 parent 3b158af commit 2b196a0
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/libs/karm-base/cursor.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ struct Cursor {

constexpr Cursor() = default;

always_inline constexpr Cursor(None)
: Cursor() {}

always_inline constexpr Cursor(T const *ptr)
: Cursor(ptr, ptr ? 1 : 0) {
}
Expand All @@ -28,7 +31,10 @@ struct Cursor {
: _begin(begin), _end(end) {
}

always_inline constexpr Cursor(Sliceable<T> auto const &slice)
always_inline constexpr Cursor(Sliceable<T> auto &slice)
: Cursor{begin(slice), end(slice)} {}

always_inline constexpr Cursor(Slice<T> slice)
: Cursor{begin(slice), end(slice)} {}

always_inline constexpr T const &operator[](usize i) const {
Expand Down Expand Up @@ -127,9 +133,11 @@ struct MutCursor {

constexpr MutCursor() = default;

always_inline constexpr MutCursor(None)
: MutCursor() {}

always_inline constexpr MutCursor(T *ptr)
: MutCursor(ptr, ptr ? 1 : 0) {
}
: MutCursor(ptr, ptr ? 1 : 0) {}

always_inline constexpr MutCursor(T *ptr, usize len)
: _begin(ptr), _end(ptr + len) {
Expand All @@ -143,6 +151,9 @@ struct MutCursor {
always_inline constexpr MutCursor(MutSliceable<T> auto &slice)
: MutCursor{begin(slice), end(slice)} {}

always_inline constexpr MutCursor(MutSlice<T> slice)
: MutCursor{begin(slice), end(slice)} {}

always_inline constexpr T &operator[](usize i) {
if (i >= len()) [[unlikely]]
panic("index out of bounds");
Expand Down

0 comments on commit 2b196a0

Please sign in to comment.