Skip to content

Commit f7e74af

Browse files
committed
fix: a panic case & add some docs
1 parent 32da074 commit f7e74af

File tree

5 files changed

+36
-9
lines changed

5 files changed

+36
-9
lines changed

planglib/std/slice.pi

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,11 @@ pub struct SliceOutOfIdxErr {
99

1010
}
1111

12-
fn test2<T>() void {
13-
14-
return;
15-
}
16-
1712
use core::eq::Eq;
1813
impl<T:Eq<T>> SliceExt<T> for [T] {
14+
/// # index_of
15+
///
16+
/// Find the index of the first occurrence of a sub array in the array.
1917
fn index_of(sub_arr:[T]) Option<i64> {
2018
let len = self.len();
2119
let sub_len = sub_arr.len();
@@ -40,13 +38,23 @@ impl<T:Eq<T>> SliceExt<T> for [T] {
4038

4139
}
4240

41+
/// # len
42+
///
43+
/// Get the length of the array.
4344
fn len() i64 {
4445
return arr_len(*self) ;
4546
}
4647

48+
/// # slice
49+
///
50+
/// Get a slice of the array.
4751
fn slice(start: i64, len: i64) [T] {
4852
return arr_slice(*self, start, len);
4953
}
54+
55+
/// # copy
56+
///
57+
/// Copy the array to another array.
5058
fn copy(to:[T], len:i64) Result<()|SliceOutOfIdxErr> {
5159
let from = *self;
5260
if from.len() < len || to.len() < len {

planglib/std/string.pi

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,29 @@ pub trait StringExt {
99

1010

1111
impl StringExt for string {
12+
/// # index_of
13+
///
14+
/// Returns the index of the first occurrence of the
15+
/// specified substring in this string, or `None` if there is no such occurrence.
1216
fn index_of(s:string) Option<i64> {
1317
let chars = self.chars();
1418
let subchars = s.chars();
1519
return chars.index_of(subchars);
1620
}
21+
/// # contains
22+
///
23+
/// Returns `true` if the specified substring is a
24+
/// substring of this string, and `false` otherwise.
1725
fn contains(s:string) bool {
1826
return self.index_of(s) is i64;
1927
}
28+
29+
/// # slice
30+
///
31+
/// Returns a new string that is a substring of this string.
32+
///
33+
/// The substring is a slice of the original string starting from the `start` index and
34+
/// having `len` characters.
2035
fn slice(start:i64, len:i64) string {
2136
if start + len == 0 {
2237
return "";
@@ -50,7 +65,7 @@ impl StringExt for string {
5065
}
5166

5267
if byte_len == 0 && len != 0 {
53-
pl_panic();
68+
pl_panic();
5469
return string {
5570
_len: 0,
5671
_byte_len: 0,
@@ -67,6 +82,10 @@ impl StringExt for string {
6782

6883
}
6984

85+
86+
/// # from_chars
87+
///
88+
/// Converts a list of characters to a string.
7089
pub fn from_chars(chars:[char]) string {
7190
let bytes:[u8] = [u8*4*arr_len(chars);];
7291
let i = 0;

src/ast/ctx.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1213,7 +1213,7 @@ impl<'a, 'ctx> Ctx<'a> {
12131213
let binding = l
12141214
.trait_impl
12151215
.clone()
1216-
.map(|e| e.get_types(self, builder).unwrap())
1216+
.map(|e| e.get_types(self, builder).unwrap_or_default())
12171217
.unwrap_or_default();
12181218
let miss = binding
12191219
.iter()

src/ast/pltype.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1716,7 +1716,7 @@ impl GenericType {
17161716
};
17171717
let ph = Arc::new(RefCell::new(PLType::Struct(place_holder)));
17181718

1719-
for it in impls.get_types(ctx, builder).unwrap() {
1719+
for it in impls.get_types(ctx, builder).unwrap_or_default() {
17201720
if let PLType::Trait(t) = &*it.clone().borrow() {
17211721
ctx.get_root_ctx().plmod.add_impl(
17221722
&ph.borrow().get_full_elm_name_without_generic(),

test/test/std_test.pi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ pub fn test_std() void {
2424
arr1.slice(2, 1)[0] = 9;
2525
panic::assert(arr1.eq(&[1,3,9]));
2626
return;
27-
}
27+
}

0 commit comments

Comments
 (0)