Skip to content

Commit 36f7a15

Browse files
authored
Add more clarifying examples to Sculptor documentation (#251)
1 parent a95af41 commit 36f7a15

1 file changed

Lines changed: 46 additions & 1 deletion

File tree

‎core/src/hlist.rs‎

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,14 +310,45 @@ macro_rules! gen_inherent_methods {
310310
///
311311
/// ```
312312
/// # fn main() {
313-
/// use frunk_core::{hlist, HList};
313+
/// use frunk_core::{hlist, HList, hlist_pat};
314314
///
315315
/// let h = hlist![9000, "joe", 41f32, true];
316316
/// let (reshaped, remainder): (HList![f32, i32, &str], _) = h.sculpt();
317317
/// assert_eq!(reshaped, hlist![41f32, 9000, "joe"]);
318318
/// assert_eq!(remainder, hlist![true]);
319319
/// # }
320320
/// ```
321+
/// ```
322+
/// // Also supports projecting references of a desired shape with 'to_ref' and 'to_mut'
323+
/// # fn main() {
324+
/// # use frunk_core::{hlist, HList};
325+
/// let h = hlist![76u32, "hello world", false, 27f64];
326+
/// let h_ref = h.to_ref();
327+
/// let (reshaped_ref, remainder_ref): (HList![&u32, &bool], _) = h_ref.sculpt();
328+
///
329+
/// assert_eq!(reshaped_ref, hlist![&76u32, &false]);
330+
/// assert_eq!(remainder_ref, hlist![&"hello world", &27f64]);
331+
///
332+
/// h.prepend(12i32); // original is unmoved
333+
/// # }
334+
/// ```
335+
/// ```
336+
/// # fn main () {
337+
/// # use frunk_core::{hlist, HList, hlist_pat};
338+
/// let mut h = hlist![76u32, "hello world", false, 27f64];
339+
/// let h_mut_ref = h.to_mut();
340+
///
341+
/// let (reshaped_mut_ref, _): (HList![&mut u32, &mut bool], _) = h_mut_ref.sculpt();
342+
/// let hlist_pat![u32_mut_ref, bool_mut_ref] = reshaped_mut_ref;
343+
///
344+
/// *u32_mut_ref = 67;
345+
/// *bool_mut_ref = true;
346+
///
347+
/// assert_eq!(h, hlist![67u32, "hello world", true, 27f64]);
348+
///
349+
/// h.prepend(12i32); // original is unmoved
350+
/// # }
351+
/// ```
321352
#[inline(always)]
322353
pub fn sculpt<Ts, Indices>(self) -> (Ts, <Self as Sculptor<Ts, Indices>>::Remainder)
323354
where Self: Sculptor<Ts, Indices>,
@@ -2057,4 +2088,18 @@ mod tests {
20572088

20582089
assert_eq!(first.extend(second), hlist![]);
20592090
}
2091+
2092+
#[test]
2093+
fn test_project_mut() {
2094+
let mut h = hlist![76u32, "hello world", false, 27f64];
2095+
let h_mut_ref = h.to_mut();
2096+
2097+
let (reshaped_mut_ref, _): (HList![&mut u32, &mut bool], _) = h_mut_ref.sculpt();
2098+
let hlist_pat![u32_mut, bool_mut] = reshaped_mut_ref;
2099+
2100+
*u32_mut = 67;
2101+
*bool_mut = true;
2102+
2103+
assert_eq!(h, hlist![67u32, "hello world", true, 27f64]);
2104+
}
20602105
}

0 commit comments

Comments
 (0)