Skip to content

Commit

Permalink
feat: add Devec::to_string and Devec::debug_write method
Browse files Browse the repository at this point in the history
  • Loading branch information
ZnPdCo committed May 22, 2024
1 parent 8f150b8 commit 88b0a7f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
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

0 comments on commit 88b0a7f

Please sign in to comment.