Skip to content

refactor(bytesview): change bytesview back to concrete type #2104

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 23 additions & 30 deletions bytes/view.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,12 @@
/// assert_eq(bv[1], b'\x02')
/// assert_eq(bv[2], b'\x03')
/// ```
type View

///|
fn View::bytes(self : View) -> Bytes = "%bytesview.bytes"

///|
fn View::start(self : View) -> Int = "%bytesview.start"

///|
fn View::len(self : View) -> Int = "%bytesview.len"

///|
fn View::make(b : Bytes, start : Int, len : Int) -> View = "%bytesview.make"
#builtin.valtype
struct View {
bytes : Bytes
start : Int
len : Int
}

///|
/// Returns the number of bytes in the view.
Expand All @@ -58,7 +51,7 @@ fn View::make(b : Bytes, start : Int, len : Int) -> View = "%bytesview.make"
/// }
/// ```
pub fn View::length(self : View) -> Int {
self.len()
self.len
}

///|
Expand Down Expand Up @@ -86,12 +79,12 @@ pub fn View::length(self : View) -> Int {
/// }
/// ```
pub fn View::op_get(self : View, index : Int) -> Byte {
guard index >= 0 && index < self.len() else {
guard index >= 0 && index < self.len else {
abort(
"index out of bounds: the len is from 0 to \{self.len()} but the index is \{index}",
"index out of bounds: the len is from 0 to \{self.len} but the index is \{index}",
)
}
self.bytes()[self.start() + index]
self.bytes[self.start + index]
}

///|
Expand Down Expand Up @@ -121,8 +114,8 @@ pub fn View::op_get(self : View, index : Int) -> Byte {
/// }
/// ```
pub fn View::get(self : View, index : Int) -> Byte? {
guard index >= 0 && index < self.len() else { None }
Some(self.bytes().unsafe_get(self.start() + index))
guard index >= 0 && index < self.len else { None }
Some(self.bytes.unsafe_get(self.start + index))
}

///|
Expand Down Expand Up @@ -157,7 +150,7 @@ pub fn View::get(self : View, index : Int) -> Byte? {
///
#internal(unsafe, "Panic if index is out of bounds")
pub fn View::unsafe_get(self : View, index : Int) -> Byte {
self.bytes()[self.start() + index]
self.bytes[self.start + index]
}

///|
Expand All @@ -183,7 +176,7 @@ pub fn Bytes::op_as_view(self : Bytes, start~ : Int = 0, end? : Int) -> View {
guard start >= 0 && start <= end && end <= len else {
abort("Invalid index for View")
}
View::make(self, start, end - start)
{ bytes: self, start, len: end - start }
}

///|
Expand All @@ -207,7 +200,7 @@ pub fn View::op_as_view(self : View, start~ : Int = 0, end? : Int) -> View {
guard start >= 0 && start <= end && end <= len else {
abort("Invalid index for View")
}
View::make(self.bytes(), self.start() + start, end - start)
{ bytes: self.bytes, start: self.start + start, len: end - start }
}

///|
Expand All @@ -223,7 +216,7 @@ pub fn View::op_as_view(self : View, start~ : Int = 0, end? : Int) -> View {
/// ```
pub fn View::iter(self : View) -> Iter[Byte] {
Iter::new(fn(yield_) {
for i in 0..<self.len() {
for i in 0..<self.len {
guard yield_(self[i]) is IterContinue else { break IterEnd }

} else {
Expand Down Expand Up @@ -597,7 +590,7 @@ pub impl Show for View with output(self, logger) {
}

logger.write_string("b\"")
for i in 0..<self.len() {
for i in 0..<self.len {
let byte = self[i].to_int()
logger
..write_string("\\x")
Expand Down Expand Up @@ -626,8 +619,8 @@ pub impl Show for View with output(self, logger) {
/// inspect(bytes[0:4] == bytes[3:6], content="false")
/// ```
pub impl Eq for View with op_equal(self, other) -> Bool {
guard self.len() == other.len() else { return false }
for i in 0..<self.len() {
guard self.len == other.len else { return false }
for i in 0..<self.len {
guard self.unsafe_get(i) == other.unsafe_get(i) else { return false }

}
Expand Down Expand Up @@ -661,8 +654,8 @@ pub impl Eq for View with op_equal(self, other) -> Bool {
/// inspect(bytes[1:5].compare(bytes[2:5]), content="1") // bcab > cab
/// ```
pub impl Compare for View with compare(self, other) -> Int {
let self_len = self.len()
let other_len = other.len()
let self_len = self.len
let other_len = other.len
let cmp = self_len.compare(other_len)
guard cmp == 0 else { return cmp }
for i in 0..<self_len {
Expand All @@ -678,11 +671,11 @@ pub impl Compare for View with compare(self, other) -> Int {
///|
/// Retrieves the underlying `Bytes` from a `View`.
pub fn View::data(self : View) -> Bytes {
self.bytes()
self.bytes
}

///|
/// Retrieves the start index of the view.
pub fn View::start_offset(self : View) -> Int {
self.start()
self.start
}