Skip to content

Commit

Permalink
chore: fix lint and doc
Browse files Browse the repository at this point in the history
  • Loading branch information
CrazyboyQCD committed Jul 12, 2024
1 parent 4586d24 commit 162e157
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
19 changes: 12 additions & 7 deletions core/string/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1151,15 +1151,16 @@ pub trait JsStringData: std::fmt::Display {}
impl JsStringData for u8 {}
impl JsStringData for u16 {}

/// A mutable builder to create instance of JsString.
/// A mutable builder to create instance of `JsString`.
///
/// # Examples
///
/// ```
/// ```rust
/// use boa_string::JsStringBuilder;
/// let mut s = JsStringBuilder::new();
/// s.push(ch);
/// s.extend_from_slice(slice);
/// s.extend(iter);
/// s.push(b'x');
/// s.extend_from_slice(&[b'1', b'2', b'3']);
/// s.extend([b'1', b'2', b'3']);
/// let js_string = s.build();
/// ```
#[derive(Debug)]
Expand Down Expand Up @@ -1190,6 +1191,7 @@ impl<T: JsStringData> JsStringBuilder<T> {

/// Create a new `JsStringBuilder` with capacity of zero.
#[inline]
#[must_use]
pub const fn new() -> Self {
Self::NEW
}
Expand Down Expand Up @@ -1223,6 +1225,7 @@ impl<T: JsStringData> JsStringBuilder<T> {
}

/// create a new `JsStringBuilder` with specific capacity
#[must_use]
pub fn with_capacity(cap: usize) -> Self {
let layout = Self::new_layout(cap);
#[allow(clippy::cast_ptr_alignment)]
Expand All @@ -1245,6 +1248,7 @@ impl<T: JsStringData> JsStringBuilder<T> {
self.inner == NonNull::dangling()
}

#[allow(clippy::cast_ptr_alignment)]
fn reserve(&mut self, new_layout: Layout) {
// SAFETY:
// The layout size of `RawJsString` is never zero, since it has to store
Expand Down Expand Up @@ -1379,13 +1383,14 @@ impl<T: JsStringData> JsStringBuilder<T> {
}
}

/// Returns true if this JsStringBuilder has a length of zero, and false otherwise.
/// Returns true if this `JsStringBuilder` has a length of zero, and false otherwise.
#[must_use]
pub fn is_empty(&self) -> bool {
self.len() == 0
}

/// build `JsString` from JsStringBuilder
#[must_use]
pub fn build(mut self) -> JsString {
if self.is_empty() {
return JsString::default();
Expand Down Expand Up @@ -1421,7 +1426,7 @@ impl<T: JsStringData> JsStringBuilder<T> {
}

impl<T: JsStringData> Drop for JsStringBuilder<T> {
/// Set cold since [`JsStringBuilder`] should be created to build JsString
/// Set cold since [`JsStringBuilder`] should be created to build `JsString`
#[cold]
fn drop(&mut self) {
if self.is_dangling() {
Expand Down
12 changes: 10 additions & 2 deletions core/string/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,11 @@ fn js_string_builder() {
assert_eq!(s_js_string, s_builder);

// from_iter
let s_builder = JsStringBuilder::from_iter(s_utf8.iter().copied()).build();
let s_builder = s_utf8
.iter()
.copied()
.collect::<JsStringBuilder<_>>()
.build();
assert_eq!(s_js_string, s_builder);

// extend_from_slice
Expand All @@ -204,7 +208,11 @@ fn js_string_builder() {
assert_eq!(s_js_string, s_builder);

// from_iter
let s_builder = JsStringBuilder::from_iter(s_utf16.iter().copied()).build();
let s_builder = s_utf8
.iter()
.copied()
.collect::<JsStringBuilder<_>>()
.build();
assert_eq!(s_js_string, s_builder);

// extend_from_slice
Expand Down

0 comments on commit 162e157

Please sign in to comment.