Skip to content

Commit 1d8f3ad

Browse files
committed
more doc examples
1 parent b5882bf commit 1d8f3ad

File tree

1 file changed

+27
-2
lines changed

1 file changed

+27
-2
lines changed

src/raw/cstr.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,30 @@ use core::str;
22

33
use crate::error::{Error, Result};
44

5+
#[allow(rustdoc::invalid_rust_codeblocks)]
56
/// A borrowed BSON-spec cstring: Zero or more UTF-8 encoded characters, excluding the nul byte.
67
/// Most conveniently constructed via the [`cstr!`](crate::raw::cstr) macro.
78
///
89
/// Unlike [`std::ffi::CStr`], this is required to be valid UTF-8, and does not include the nul
9-
/// terminator in the buffer.
10+
/// terminator in the buffer:
11+
/// ```
12+
/// // std::ffi::CStr accepts invalid UTF-8:
13+
/// let invalid: &std::ffi::CStr = c"\xc3\x28";
14+
/// ```
15+
/// ```compile_fail
16+
/// # use bson::raw::cstr;
17+
/// // bson::raw::CStr does not:
18+
/// let invalid: &bson::raw::CStr = cstr!("\xc3\x28"); // will not compile
19+
/// ```
20+
/// ```
21+
/// // &str accepts embedded nil characters:
22+
/// let invalid: &str = "foo\0bar";
23+
/// ```
24+
/// ```compile_fail
25+
/// # use bson::raw::cstr;
26+
/// // &str accepts embedded nil characters:
27+
/// let invalid: &bson::raw::CStr = cstr!("foo\0bar"); // will not compile
28+
/// ```
1029
#[derive(Debug)]
1130
#[repr(transparent)]
1231
pub struct CStr {
@@ -121,6 +140,7 @@ pub const fn validate_cstr(text: &str) -> Option<&CStr> {
121140
#[doc(hidden)]
122141
pub const fn assert_valid_cstr<T: ValidCStr>() {}
123142

143+
#[allow(rustdoc::invalid_rust_codeblocks)]
124144
/// Construct a `'static &CStr`. The validitiy will be verified at compile-time.
125145
/// ```
126146
/// # use bson::raw::{CStr, cstr};
@@ -129,7 +149,12 @@ pub const fn assert_valid_cstr<T: ValidCStr>() {}
129149
/// ```
130150
/// ```compile_fail
131151
/// # use bson::raw::{CStr, cstr};
132-
/// // An invalid literal will not compile:
152+
/// // A literal with invalid UTF-8 will not compile:
153+
/// let key: &CStr = cstr!("\xc3\x28");
154+
/// ```
155+
/// ```compile_fail
156+
/// # use bson::raw::{CStr, cstr};
157+
/// // A literal with an embedded nil will not compile:
133158
/// let key: &CStr = cstr!("hel\0lo");
134159
/// ```
135160
#[macro_export]

0 commit comments

Comments
 (0)