Skip to main content

rustpython_vm/
utils.rs

1use rustpython_common::wtf8::{Wtf8, Wtf8Buf};
2
3use crate::{
4    PyObjectRef, PyResult, VirtualMachine,
5    builtins::{PyStr, PyUtf8Str},
6    convert::{ToPyException, ToPyObject},
7    exceptions::cstring_error,
8};
9
10pub fn hash_iter<'a, I: IntoIterator<Item = &'a PyObjectRef>>(
11    iter: I,
12    vm: &VirtualMachine,
13) -> PyResult<rustpython_common::hash::PyHash> {
14    vm.state.hash_secret.hash_iter(iter, |obj| obj.hash(vm))
15}
16
17impl ToPyObject for core::convert::Infallible {
18    fn to_pyobject(self, _vm: &VirtualMachine) -> PyObjectRef {
19        match self {}
20    }
21}
22
23pub trait ToCString: AsRef<Wtf8> {
24    fn to_cstring(&self, vm: &VirtualMachine) -> PyResult<alloc::ffi::CString> {
25        alloc::ffi::CString::new(self.as_ref().as_bytes()).map_err(|err| err.to_pyexception(vm))
26    }
27    fn ensure_no_nul(&self, vm: &VirtualMachine) -> PyResult<()> {
28        if self.as_ref().as_bytes().contains(&b'\0') {
29            Err(cstring_error(vm))
30        } else {
31            Ok(())
32        }
33    }
34}
35
36impl ToCString for &str {}
37impl ToCString for PyStr {}
38impl ToCString for PyUtf8Str {}
39
40pub(crate) fn collection_repr<'a, I>(
41    class_name: Option<&str>,
42    prefix: &str,
43    suffix: &str,
44    iter: I,
45    vm: &VirtualMachine,
46) -> PyResult<Wtf8Buf>
47where
48    I: core::iter::Iterator<Item = &'a PyObjectRef>,
49{
50    let mut repr = Wtf8Buf::new();
51    if let Some(name) = class_name {
52        repr.push_str(name);
53        repr.push_char('(');
54    }
55    repr.push_str(prefix);
56    {
57        let mut parts_iter = iter.map(|o| o.repr(vm));
58        let first = parts_iter
59            .next()
60            .transpose()?
61            .expect("this is not called for empty collection");
62        repr.push_wtf8(first.as_wtf8());
63        for part in parts_iter {
64            repr.push_str(", ");
65            repr.push_wtf8(part?.as_wtf8());
66        }
67    }
68    repr.push_str(suffix);
69    if class_name.is_some() {
70        repr.push_char(')');
71    }
72
73    Ok(repr)
74}