Skip to content

Commit d47d38e

Browse files
Add doc comments
1 parent 38c711d commit d47d38e

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/cow.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,50 +6,78 @@ use crate::len_type::LenType;
66
use crate::{CapacityError, String, StringView};
77
use core::borrow::Borrow;
88

9+
/// A clone-on-write (COW) string type for heapless.
10+
///
11+
/// `Cow` can either **borrow** a `StringView` or **own** a `String`.
12+
/// This allows efficient handling of strings that may be either temporary
13+
/// references or fully owned data.
14+
///
15+
/// # Type Parameters
16+
///
17+
/// - `N`: The inline buffer size for owned strings.
18+
/// - `LenT`: The integer type used for length tracking (must implement [`LenType`]).
19+
920
/// A clone-on-write string type for heapless
1021
#[derive(Debug)]
1122
pub enum Cow<'a, const N: usize, LenT: LenType = usize> {
23+
/// A borrowed view of a string.
1224
Borrowed(&'a StringView<LenT>),
25+
26+
/// An owned string with inline storage of size `N`.
1327
Owned(String<N, LenT>),
1428
}
1529

1630
impl<'a, const N: usize, LenT: LenType> Cow<'a, N, LenT> {
31+
/// Converts the `Cow` into an owned [`String`].
32+
///
33+
/// If the `Cow` is borrowed, this clones the underlying string data.
34+
/// If the `Cow` is already owned, this returns a clone of it.
1735
pub fn to_owned(&self) -> String<N, LenT> {
1836
match self {
1937
Cow::Borrowed(sv) => String::from(sv),
2038
Cow::Owned(s) => s.clone(),
2139
}
2240
}
2341

42+
/// Returns the string as a `&str`.
43+
///
44+
/// Works for both borrowed and owned variants.
2445
pub fn as_str(&self) -> &str {
2546
match self {
2647
Cow::Borrowed(sv) => sv.as_str(),
2748
Cow::Owned(s) => s.as_str(),
2849
}
2950
}
3051

52+
/// Returns `true` if this `Cow` is a borrowed string.
3153
pub fn is_borrowed(&self) -> bool {
3254
matches!(self, Cow::Borrowed(_))
3355
}
3456

57+
/// Returns `true` if this `Cow` is an owned string.
3558
pub fn is_owned(&self) -> bool {
3659
matches!(self, Cow::Owned(_))
3760
}
3861
}
3962

4063
impl<'a, const N: usize, LenT: LenType> From<&'a StringView<LenT>> for Cow<'a, N, LenT> {
64+
/// Creates a borrowed `Cow` from a `StringView`.
4165
fn from(sv: &'a StringView<LenT>) -> Self {
4266
Cow::Borrowed(sv)
4367
}
4468
}
4569

4670
impl<const N: usize, LenT: LenType> From<String<N, LenT>> for Cow<'_, N, LenT> {
71+
/// Creates an owned `Cow` from a `String`.
4772
fn from(s: String<N, LenT>) -> Self {
4873
Cow::Owned(s)
4974
}
5075
}
5176

5277
impl<'a, const N: usize, LenT: LenType> Borrow<str> for Cow<'a, N, LenT> {
78+
/// Borrows the string as a `&str`.
79+
///
80+
/// This allows `Cow` to be used anywhere a `&str` is expected.
5381
fn borrow(&self) -> &str {
5482
self.as_str()
5583
}

0 commit comments

Comments
 (0)