Skip to main content

CodeChannel

Struct CodeChannel 

Source
pub struct CodeChannel { /* private fields */ }
Expand description

A cloneable, blocking result channel. Multiple consumers may call recv; disconnected means shut_down_resrap has finished draining every result.

Implementations§

Source§

impl CodeChannel

Source

pub fn recv(&self) -> Result<CodeGenRes, RecvError>

Examples found in repository?
examples/multithreaded.rs (line 20)
9fn main() -> Result<(), String> {
10    let mut resrap = ResrapMT::new(4, 256);
11    templates::load(&mut resrap, templates::Lang::Python)?;
12    resrap.start_resrap();
13
14    let channel = resrap.get_code_channel();
15    for id in 0..8u64 {
16        resrap.generate_with_seeded(&format!("job-{id}"), "python", "program", id + 1, 80)?;
17    }
18
19    for _ in 0..8 {
20        let result = channel.recv().map_err(|err| err.to_string())?;
21        println!("--- {} ---\n{}\n", result.id, result.code);
22    }
23
24    resrap.shutdown_resrap();
25    Ok(())
26}
More examples
Hide additional examples
examples/memory.rs (line 82)
39fn main() {
40    report("baseline");
41
42    let mut resrap = Resrap::new();
43    for lang in templates::Lang::ALL {
44        templates::load(&mut resrap, lang).unwrap();
45    }
46    report("all 10 templates loaded");
47
48    let outputs: Vec<String> = (0..10_000)
49        .map(|i| resrap.generate_with_seeded("c", "program", i % 97 + 1, 200))
50        .collect();
51    let output_bytes: usize = outputs.iter().map(String::len).sum();
52    report("10,000 outputs retained (C, 200 tokens)");
53    println!(
54        "  retained output: {:.1} MiB, average {:.0} B/output",
55        output_bytes as f64 / (1024.0 * 1024.0),
56        output_bytes as f64 / outputs.len() as f64
57    );
58
59    drop(outputs);
60    report("outputs dropped");
61
62    let mut mt = ResrapMT::new(8, 4096);
63    templates::load(&mut mt, "c").unwrap();
64    mt.start_resrap();
65    let channel = mt.get_code_channel();
66
67    let jobs = 10_000;
68    for i in 0..jobs {
69        mt.generate_with_seeded(
70            &format!("job-{i}"),
71            "c",
72            "program",
73            (i as u64) % 97 + 1,
74            200,
75        )
76        .unwrap();
77    }
78    report("MT results buffered, consumer idle");
79
80    let mut received = 0usize;
81    for _ in 0..jobs {
82        received += channel.recv().unwrap().code.len();
83    }
84    report("MT results drained");
85    println!(
86        "  received output: {:.1} MiB",
87        received as f64 / (1024.0 * 1024.0)
88    );
89
90    drop(channel);
91    mt.shutdown_resrap();
92    drop(mt);
93    report("MT instance dropped");
94    // Run the long-output case last: large allocations can change glibc's
95    // mmap threshold and make later RSS figures depend on allocator history.
96    let long = resrap.generate_with_seeded("c", "program", 7, 1_000_000);
97    report("one 1M-token output retained");
98    println!(
99        "  retained output: {:.1} MiB",
100        long.len() as f64 / (1024.0 * 1024.0)
101    );
102    drop(long);
103    report("1M-token output dropped");
104}
Source

pub fn try_recv(&self) -> Result<CodeGenRes, TryRecvError>

Source

pub fn recv_timeout( &self, timeout: Duration, ) -> Result<CodeGenRes, RecvTimeoutError>

Trait Implementations§

Source§

impl Clone for CodeChannel

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.