Skip to content
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

feat: add Devec::to_string and Devec::debug_write method #427

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
34 changes: 34 additions & 0 deletions devec/devec.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -803,3 +803,37 @@ test "reserve and push" {
inspect(d.front(), content="Some(Some(1))")?
inspect(d.back(), content="Some(Some(1))")?
}

/// Convert the queue to a string.
///
/// # Example
///
/// ```
/// let dv = Devec::[1, 2, 3, 4]
/// println(dv) // "Devec::[1, 2, 3, 4]"
/// ```
pub fn to_string[T : Show](self : Devec[T]) -> String {
let mut res = "Devec::["
self.iteri(
fn(i, t) {
if i > 0 {
res += ", "
}
res += t.to_string()
},
)
res + "]"
}

pub fn debug_write[T : Show](self : Devec[T], buf : Buffer) -> Unit {
buf.write_string(self.to_string())
}

test "to_string" {
let dv : Devec[Int] = Devec::[1, 2, 3, 4]
inspect(dv, content="Devec::[1, 2, 3, 4]")?
dv.push_back(5)
inspect(dv, content="Devec::[1, 2, 3, 4, 5]")?
dv.pop_front() |> ignore
inspect(dv, content="Devec::[2, 3, 4, 5]")?
}
2 changes: 2 additions & 0 deletions devec/devec.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ impl Devec {
capacity[T](Self[T]) -> Int
clear[T](Self[T]) -> Unit
contains[T : Eq](Self[T], T) -> Bool
debug_write[T : Show](Self[T], Buffer) -> Unit
from_array[T](Array[T]) -> Self[T]
front[T](Self[T]) -> Option[T]
is_empty[T](Self[T]) -> Bool
Expand All @@ -35,6 +36,7 @@ impl Devec {
reserve_capacity[T](Self[T], Int) -> Unit
search[T : Eq](Self[T], T) -> Option[Int]
shrink_to_fit[T](Self[T]) -> Unit
to_string[T : Show](Self[T]) -> String
with_capacity[T](Int) -> Self[T]
}

Expand Down