| 1 | #![deny(missing_docs)] |
| 2 | #![doc = include_strrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/include_str!!(concat!(env!("CARGO_MANIFEST_DIR"), "/README.md"))] |
| 3 | use pyo3rust-analyzer cargo pyo3 0.29.0 crate/::preluderust-analyzer cargo pyo3 0.29.0 prelude/::*; |
| 4 | |
| 5 | use pyo3rust-analyzer cargo pyo3 0.29.0 crate/::exceptionsrust-analyzer cargo pyo3 0.29.0 exceptions/::{PyAttributeErrorrust-analyzer cargo pyo3 0.29.0 exceptions/PyAttributeError#, PyValueErrorrust-analyzer cargo pyo3 0.29.0 exceptions/PyValueError#}; |
| 6 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::{Readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#, Seekrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Seek#, Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#}; |
| 7 | #[cfg(any(unix, target_os = "wasi"))] |
| 8 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::osrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/::fdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/::{AsFdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/owned/AsFd#, BorrowedFdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/owned/BorrowedFd#, RawFdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/raw/RawFd#}; |
| 9 | |
| 10 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::borrowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/::Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#; |
| 11 | |
| 12 | /// Rust wrapper for a Python file-like object that implements the `io` protocol. |
| 13 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 14 | pub struct PyBinaryFilerust-analyzer cargo pyo3-filelike 0.5.3 PyBinaryFile#(Pyrust-analyzer cargo pyo3 0.29.0 instance/Py#<PyAnyrust-analyzer cargo pyo3 0.29.0 types/any/PyAny#>); |
| 15 | |
| 16 | impl<'py> IntoPyObjectrust-analyzer cargo pyo3 0.29.0 conversion/IntoPyObject#<'py> for PyBinaryFilerust-analyzer cargo pyo3-filelike 0.5.3 PyBinaryFile# { |
| 17 | type Targetrust-analyzer cargo pyo3-filelike 0.5.3 impl#[PyBinaryFile][`IntoPyObject<'py>`]Target# = PyAnyrust-analyzer cargo pyo3 0.29.0 types/any/PyAny#; |
| 18 | |
| 19 | type Outputrust-analyzer cargo pyo3-filelike 0.5.3 impl#[PyBinaryFile][`IntoPyObject<'py>`]Output# = Boundrust-analyzer cargo pyo3 0.29.0 instance/Bound#<'py, PyAnyrust-analyzer cargo pyo3 0.29.0 types/any/PyAny#>; |
| 20 | |
| 21 | type Errorrust-analyzer cargo pyo3-filelike 0.5.3 impl#[PyBinaryFile][`IntoPyObject<'py>`]Error# = PyErrrust-analyzer cargo pyo3 0.29.0 err/PyErr#; |
| 22 | |
| 23 | fn into_pyobjectrust-analyzer cargo pyo3-filelike 0.5.3 impl#[PyBinaryFile][`IntoPyObject<'py>`]into_pyobject().(self, py: Pythonrust-analyzer cargo pyo3 0.29.0 marker/Python#<'py>) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Selfrust-analyzer cargo pyo3-filelike 0.5.3 impl#[PyBinaryFile][`IntoPyObject<'py>`]::Outputrust-analyzer cargo pyo3 0.29.0 conversion/IntoPyObject#Output#, Selfrust-analyzer cargo pyo3-filelike 0.5.3 impl#[PyBinaryFile][`IntoPyObject<'py>`]::Errorrust-analyzer cargo pyo3 0.29.0 conversion/IntoPyObject#Error#> { |
| 24 | let obj = self.0rust-analyzer cargo pyo3-filelike 0.5.3 PyBinaryFile#0..clone_refrust-analyzer cargo pyo3 0.29.0 instance/impl#[`Py<T>`]clone_ref().(py); |
| 25 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(obj.into_boundrust-analyzer cargo pyo3 0.29.0 instance/impl#[`Py<T>`]into_bound().(py)) |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | impl Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone# for PyBinaryFilerust-analyzer cargo pyo3-filelike 0.5.3 PyBinaryFile# { |
| 30 | fn clonerust-analyzer cargo pyo3-filelike 0.5.3 impl#[PyBinaryFile][Clone]clone().(&self) -> Selfrust-analyzer cargo pyo3-filelike 0.5.3 impl#[PyBinaryFile][Clone] { |
| 31 | Pythonrust-analyzer cargo pyo3 0.29.0 marker/Python#::attachrust-analyzer cargo pyo3 0.29.0 marker/impl#[`Python<'_>`]attach().(|py| PyBinaryFilerust-analyzer cargo pyo3-filelike 0.5.3 PyBinaryFile#::fromrust-analyzer cargo pyo3-filelike 0.5.3 impl#[PyBinaryFile][`From<Py<PyAny>>`]from().(self.0rust-analyzer cargo pyo3-filelike 0.5.3 PyBinaryFile#0..clone_refrust-analyzer cargo pyo3 0.29.0 instance/impl#[`Py<T>`]clone_ref().(py))) |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | impl Readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read# for PyBinaryFilerust-analyzer cargo pyo3-filelike 0.5.3 PyBinaryFile# { |
| 36 | fn readrust-analyzer cargo pyo3-filelike 0.5.3 impl#[PyBinaryFile][Read]read().(&mut self, buf: &mut [u8]) -> stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<usize> { |
| 37 | Pythonrust-analyzer cargo pyo3 0.29.0 marker/Python#::attachrust-analyzer cargo pyo3 0.29.0 marker/impl#[`Python<'_>`]attach().(|py| { |
| 38 | let bytes = self.0rust-analyzer cargo pyo3-filelike 0.5.3 PyBinaryFile#0..call_method1rust-analyzer cargo pyo3 0.29.0 instance/impl#[`Py<T>`]call_method1().(py, "read", (buf.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().(),))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 39 | let bytes = bytes |
| 40 | .extractrust-analyzer cargo pyo3 0.29.0 instance/impl#[`Py<T>`]extract().::<&[u8]>(py) |
| 41 | .map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|e| stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]new().(stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::InvalidDatarust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#InvalidData#, e.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 42 | let len = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/::minrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/min().(buf.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().(), bytes.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().()); |
| 43 | buf[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#len]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut()..copy_from_slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]copy_from_slice().(&bytes[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#len]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().); |
| 44 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(len) |
| 45 | }) |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | impl Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write# for PyBinaryFilerust-analyzer cargo pyo3-filelike 0.5.3 PyBinaryFile# { |
| 50 | fn writerust-analyzer cargo pyo3-filelike 0.5.3 impl#[PyBinaryFile][Write]write().(&mut self, buf: &[u8]) -> stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<usize> { |
| 51 | Pythonrust-analyzer cargo pyo3 0.29.0 marker/Python#::attachrust-analyzer cargo pyo3 0.29.0 marker/impl#[`Python<'_>`]attach().(|py| { |
| 52 | let bytes = pyo3rust-analyzer cargo pyo3 0.29.0 crate/::typesrust-analyzer cargo pyo3 0.29.0 types/::PyBytesrust-analyzer cargo pyo3 0.29.0 types/bytes/PyBytes#::newrust-analyzer cargo pyo3 0.29.0 types/bytes/impl#[PyBytes]new().(py, buf); |
| 53 | self.0rust-analyzer cargo pyo3-filelike 0.5.3 PyBinaryFile#0..call_method1rust-analyzer cargo pyo3 0.29.0 instance/impl#[`Py<T>`]call_method1().(py, "write", (bytes,))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 54 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(buf.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().()) |
| 55 | }) |
| 56 | } |
| 57 | |
| 58 | fn flushrust-analyzer cargo pyo3-filelike 0.5.3 impl#[PyBinaryFile][Write]flush().(&mut self) -> stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<()> { |
| 59 | Pythonrust-analyzer cargo pyo3 0.29.0 marker/Python#::attachrust-analyzer cargo pyo3 0.29.0 marker/impl#[`Python<'_>`]attach().(|py| { |
| 60 | self.0rust-analyzer cargo pyo3-filelike 0.5.3 PyBinaryFile#0..call_method0rust-analyzer cargo pyo3 0.29.0 instance/impl#[`Py<T>`]call_method0().(py, "flush")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 61 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 62 | }) |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | impl Seekrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Seek# for PyBinaryFilerust-analyzer cargo pyo3-filelike 0.5.3 PyBinaryFile# { |
| 67 | fn seekrust-analyzer cargo pyo3-filelike 0.5.3 impl#[PyBinaryFile][Seek]seek().(&mut self, pos: stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::SeekFromrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/SeekFrom#) -> stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<u64> { |
| 68 | Pythonrust-analyzer cargo pyo3 0.29.0 marker/Python#::attachrust-analyzer cargo pyo3 0.29.0 marker/impl#[`Python<'_>`]attach().(|py| { |
| 69 | let (whence, offset) = match pos { |
| 70 | stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::SeekFromrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/SeekFrom#::Startrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/SeekFrom#Start#(offset) => (0, offset as i64), |
| 71 | stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::SeekFromrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/SeekFrom#::Endrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/SeekFrom#End#(offset) => (2, offset), |
| 72 | stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::SeekFromrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/SeekFrom#::Currentrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/SeekFrom#Current#(offset) => (1, offset), |
| 73 | }; |
| 74 | let pos = self.0rust-analyzer cargo pyo3-filelike 0.5.3 PyBinaryFile#0..call_method1rust-analyzer cargo pyo3 0.29.0 instance/impl#[`Py<T>`]call_method1().(py, "seek", (offset, whence))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 75 | let pos = pos.extractrust-analyzer cargo pyo3 0.29.0 instance/impl#[`Py<T>`]extract().::<u64>(py)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 76 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(pos) |
| 77 | }) |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | #[cfg(any(unix, target_os = "wasi"))] |
| 82 | impl AsFdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/owned/AsFd# for PyBinaryFilerust-analyzer cargo pyo3-filelike 0.5.3 PyBinaryFile# { |
| 83 | fn as_fdrust-analyzer cargo pyo3-filelike 0.5.3 impl#[PyBinaryFile][AsFd]as_fd().(&self) -> BorrowedFdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/owned/BorrowedFd#<'_> { |
| 84 | Pythonrust-analyzer cargo pyo3 0.29.0 marker/Python#::attachrust-analyzer cargo pyo3 0.29.0 marker/impl#[`Python<'_>`]attach().(|py| { |
| 85 | let fd = self.0rust-analyzer cargo pyo3-filelike 0.5.3 PyBinaryFile#0..call_method0rust-analyzer cargo pyo3 0.29.0 instance/impl#[`Py<T>`]call_method0().(py, "fileno")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 86 | let fd = fd.extractrust-analyzer cargo pyo3 0.29.0 instance/impl#[`Py<T>`]extract().::<RawFdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/raw/RawFd#>(py)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 87 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#::<BorrowedFdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/owned/BorrowedFd#<'_>, PyErrrust-analyzer cargo pyo3 0.29.0 err/PyErr#>(unsafe { BorrowedFdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/owned/BorrowedFd#::borrow_rawrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/owned/impl#[`BorrowedFd<'_>`]borrow_raw().(fd) }) |
| 88 | }) |
| 89 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().() |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | impl PyBinaryFilerust-analyzer cargo pyo3-filelike 0.5.3 PyBinaryFile# { |
| 94 | fn newrust-analyzer cargo pyo3-filelike 0.5.3 impl#[PyBinaryFile]new().(file: Pyrust-analyzer cargo pyo3 0.29.0 instance/Py#<PyAnyrust-analyzer cargo pyo3 0.29.0 types/any/PyAny#>) -> PyResultrust-analyzer cargo pyo3 0.29.0 err/PyResult#<Selfrust-analyzer cargo pyo3-filelike 0.5.3 impl#[PyBinaryFile]> { |
| 95 | let o = PyBinaryFilerust-analyzer cargo pyo3-filelike 0.5.3 PyBinaryFile#(file); |
| 96 | o.check_moderust-analyzer cargo pyo3-filelike 0.5.3 impl#[PyBinaryFile]check_mode().('b')?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 97 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(o) |
| 98 | } |
| 99 | |
| 100 | fn check_moderust-analyzer cargo pyo3-filelike 0.5.3 impl#[PyBinaryFile]check_mode().(&self, expected_mode: char) -> PyResultrust-analyzer cargo pyo3 0.29.0 err/PyResult#<()> { |
| 101 | Pythonrust-analyzer cargo pyo3 0.29.0 marker/Python#::attachrust-analyzer cargo pyo3 0.29.0 marker/impl#[`Python<'_>`]attach().(|py| { |
| 102 | match self.0rust-analyzer cargo pyo3-filelike 0.5.3 PyBinaryFile#0..getattrrust-analyzer cargo pyo3 0.29.0 instance/impl#[`Py<T>`]getattr().(py, "mode") { |
| 103 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(mode) => { |
| 104 | let mode_str = match mode.extractrust-analyzer cargo pyo3 0.29.0 instance/impl#[`Py<T>`]extract().::<Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#<str>>(py) { |
| 105 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(mode_str) => mode_str, |
| 106 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(_) => { |
| 107 | // Assume binary mode if mode attribute is not a string |
| 108 | return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()); |
| 109 | } |
| 110 | }; |
| 111 | if mode_str.containsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]contains().(expected_mode) { |
| 112 | return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()); |
| 113 | } |
| 114 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(PyValueErrorrust-analyzer cargo pyo3 0.29.0 exceptions/PyValueError#::new_errrust-analyzer cargo pyo3 0.29.0 exceptions/impl#[PyValueError]new_err().(formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!( |
| 115 | "file must be opened in {} mode", |
| 116 | expected_mode |
| 117 | ))) |
| 118 | } |
| 119 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(e) if e.is_instance_ofrust-analyzer cargo pyo3 0.29.0 err/impl#[PyErr]is_instance_of().::<PyAttributeErrorrust-analyzer cargo pyo3 0.29.0 exceptions/PyAttributeError#>(py) => { |
| 120 | // Assume binary mode if mode attribute is not present |
| 121 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 122 | } |
| 123 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(e) => Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(e), |
| 124 | } |
| 125 | }) |
| 126 | } |
| 127 | |
| 128 | /// Check if the underlying Python file-like object supports seeking. |
| 129 | /// |
| 130 | /// This method calls the Python `seekable()` method on the underlying object |
| 131 | /// to determine if seek operations are supported at runtime. |
| 132 | /// |
| 133 | /// Note: This must be checked at runtime rather than compile time because: |
| 134 | /// - The wrapped Python object is dynamically typed (`Py<PyAny>`) |
| 135 | /// - Whether seeking is supported depends on the specific Python object's |
| 136 | /// implementation, which cannot be known at compile time |
| 137 | /// - Rust's trait system operates on static types, but Python objects have |
| 138 | /// runtime capabilities that vary between instances |
| 139 | /// |
| 140 | /// # Returns |
| 141 | /// |
| 142 | /// Returns `Ok(true)` if the file supports seeking, `Ok(false)` if not, |
| 143 | /// or `Err` if the `seekable()` method call fails. |
| 144 | /// |
| 145 | /// # Example |
| 146 | /// |
| 147 | /// ``` |
| 148 | /// use pyo3::prelude::*; |
| 149 | /// use pyo3_filelike::PyBinaryFile; |
| 150 | /// |
| 151 | /// Python::attach(|py| -> PyResult<()> { |
| 152 | /// let io = py.import("io")?; |
| 153 | /// let file = io.call_method1("BytesIO", (&b"hello"[..],))?; |
| 154 | /// let file = PyBinaryFile::from(file); |
| 155 | /// assert_eq!(file.seekable()?, true); |
| 156 | /// Ok(()) |
| 157 | /// }).unwrap(); |
| 158 | /// ``` |
| 159 | pub fn seekablerust-analyzer cargo pyo3-filelike 0.5.3 impl#[PyBinaryFile]seekable().(&self) -> PyResultrust-analyzer cargo pyo3 0.29.0 err/PyResult#<bool> { |
| 160 | Pythonrust-analyzer cargo pyo3 0.29.0 marker/Python#::attachrust-analyzer cargo pyo3 0.29.0 marker/impl#[`Python<'_>`]attach().(|py| { |
| 161 | let result = self.0rust-analyzer cargo pyo3-filelike 0.5.3 PyBinaryFile#0..call_method0rust-analyzer cargo pyo3 0.29.0 instance/impl#[`Py<T>`]call_method0().(py, "seekable")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 162 | result.extractrust-analyzer cargo pyo3 0.29.0 instance/impl#[`Py<T>`]extract().::<bool>(py) |
| 163 | }) |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | impl Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<Pyrust-analyzer cargo pyo3 0.29.0 instance/Py#<PyAnyrust-analyzer cargo pyo3 0.29.0 types/any/PyAny#>> for PyBinaryFilerust-analyzer cargo pyo3-filelike 0.5.3 PyBinaryFile# { |
| 168 | fn fromrust-analyzer cargo pyo3-filelike 0.5.3 impl#[PyBinaryFile][`From<Py<PyAny>>`]from().(obj: Pyrust-analyzer cargo pyo3 0.29.0 instance/Py#<PyAnyrust-analyzer cargo pyo3 0.29.0 types/any/PyAny#>) -> Selfrust-analyzer cargo pyo3-filelike 0.5.3 impl#[PyBinaryFile][`From<Py<PyAny>>`] { |
| 169 | PyBinaryFilerust-analyzer cargo pyo3-filelike 0.5.3 PyBinaryFile#::newrust-analyzer cargo pyo3-filelike 0.5.3 impl#[PyBinaryFile]new().(obj).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().() |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | impl Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<Boundrust-analyzer cargo pyo3 0.29.0 instance/Bound#<'_, PyAnyrust-analyzer cargo pyo3 0.29.0 types/any/PyAny#>> for PyBinaryFilerust-analyzer cargo pyo3-filelike 0.5.3 PyBinaryFile# { |
| 174 | fn fromrust-analyzer cargo pyo3-filelike 0.5.3 impl#[PyBinaryFile][`From<Bound<'_, PyAny>>`]from().(obj: Boundrust-analyzer cargo pyo3 0.29.0 instance/Bound#<'_, PyAnyrust-analyzer cargo pyo3 0.29.0 types/any/PyAny#>) -> Selfrust-analyzer cargo pyo3-filelike 0.5.3 impl#[PyBinaryFile][`From<Bound<'_, PyAny>>`] { |
| 175 | PyBinaryFilerust-analyzer cargo pyo3-filelike 0.5.3 PyBinaryFile#::newrust-analyzer cargo pyo3-filelike 0.5.3 impl#[PyBinaryFile]new().(obj.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().() |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | /// Rust wrapper for a Python text file-like object that implements the `io` protocol. |
| 180 | /// |
| 181 | /// This wrapper is similar to `PyBinaryFile`, but it assumes that the file is text and |
| 182 | /// returns the byte representation of the text. |
| 183 | /// |
| 184 | /// The Python file-like object must have a `read` method that returns either a |
| 185 | /// `bytes` object or a `str` object. |
| 186 | /// |
| 187 | /// Seek operations are not supported on text files, since the equivalent Python |
| 188 | /// file-like objects seek by characters, not bytes. |
| 189 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 190 | pub struct PyTextFilerust-analyzer cargo pyo3-filelike 0.5.3 PyTextFile# { |
| 191 | innerrust-analyzer cargo pyo3-filelike 0.5.3 PyTextFile#inner.: Pyrust-analyzer cargo pyo3 0.29.0 instance/Py#<PyAnyrust-analyzer cargo pyo3 0.29.0 types/any/PyAny#>, |
| 192 | bufferrust-analyzer cargo pyo3-filelike 0.5.3 PyTextFile#buffer.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>, |
| 193 | } |
| 194 | |
| 195 | impl PyTextFilerust-analyzer cargo pyo3-filelike 0.5.3 PyTextFile# { |
| 196 | /// Create a new `PyTextFile` from a Python file-like object and an encoding. |
| 197 | pub fn newrust-analyzer cargo pyo3-filelike 0.5.3 impl#[PyTextFile]new().(file: Pyrust-analyzer cargo pyo3 0.29.0 instance/Py#<PyAnyrust-analyzer cargo pyo3 0.29.0 types/any/PyAny#>) -> PyResultrust-analyzer cargo pyo3 0.29.0 err/PyResult#<Selfrust-analyzer cargo pyo3-filelike 0.5.3 impl#[PyTextFile]> { |
| 198 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(PyTextFilerust-analyzer cargo pyo3-filelike 0.5.3 PyTextFile# { |
| 199 | innerrust-analyzer cargo pyo3-filelike 0.5.3 PyTextFile#inner.: file, |
| 200 | bufferrust-analyzer cargo pyo3-filelike 0.5.3 PyTextFile#buffer.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().(), |
| 201 | }) |
| 202 | } |
| 203 | |
| 204 | /// Check if the underlying Python file-like object supports seeking. |
| 205 | /// |
| 206 | /// This method calls the Python `seekable()` method on the underlying object |
| 207 | /// to determine if seek operations are supported at runtime. |
| 208 | /// |
| 209 | /// Note: This must be checked at runtime rather than compile time because: |
| 210 | /// - The wrapped Python object is dynamically typed (`Py<PyAny>`) |
| 211 | /// - Whether seeking is supported depends on the specific Python object's |
| 212 | /// implementation, which cannot be known at compile time |
| 213 | /// - Rust's trait system operates on static types, but Python objects have |
| 214 | /// runtime capabilities that vary between instances |
| 215 | /// |
| 216 | /// # Returns |
| 217 | /// |
| 218 | /// Returns `Ok(true)` if the file supports seeking, `Ok(false)` if not, |
| 219 | /// or `Err` if the `seekable()` method call fails. |
| 220 | /// |
| 221 | /// # Example |
| 222 | /// |
| 223 | /// ``` |
| 224 | /// use pyo3::prelude::*; |
| 225 | /// use pyo3_filelike::PyTextFile; |
| 226 | /// |
| 227 | /// Python::attach(|py| -> PyResult<()> { |
| 228 | /// let io = py.import("io")?; |
| 229 | /// let file = io.call_method1("StringIO", ("hello",))?; |
| 230 | /// let file = PyTextFile::from(file); |
| 231 | /// assert_eq!(file.seekable()?, true); |
| 232 | /// Ok(()) |
| 233 | /// }).unwrap(); |
| 234 | /// ``` |
| 235 | pub fn seekablerust-analyzer cargo pyo3-filelike 0.5.3 impl#[PyTextFile]seekable().(&self) -> PyResultrust-analyzer cargo pyo3 0.29.0 err/PyResult#<bool> { |
| 236 | Pythonrust-analyzer cargo pyo3 0.29.0 marker/Python#::attachrust-analyzer cargo pyo3 0.29.0 marker/impl#[`Python<'_>`]attach().(|py| { |
| 237 | let result = self.innerrust-analyzer cargo pyo3-filelike 0.5.3 PyTextFile#inner..call_method0rust-analyzer cargo pyo3 0.29.0 instance/impl#[`Py<T>`]call_method0().(py, "seekable")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 238 | result.extractrust-analyzer cargo pyo3 0.29.0 instance/impl#[`Py<T>`]extract().::<bool>(py) |
| 239 | }) |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | impl Readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read# for PyTextFilerust-analyzer cargo pyo3-filelike 0.5.3 PyTextFile# { |
| 244 | fn readrust-analyzer cargo pyo3-filelike 0.5.3 impl#[PyTextFile][Read]read().(&mut self, buf: &mut [u8]) -> stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<usize> { |
| 245 | Pythonrust-analyzer cargo pyo3 0.29.0 marker/Python#::attachrust-analyzer cargo pyo3 0.29.0 marker/impl#[`Python<'_>`]attach().(|py| { |
| 246 | if self.bufferrust-analyzer cargo pyo3-filelike 0.5.3 PyTextFile#buffer..lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().buf.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() { |
| 247 | buf.copy_from_slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]copy_from_slice().(&self.bufferrust-analyzer cargo pyo3-filelike 0.5.3 PyTextFile#buffer.[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#buf.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().()]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().); |
| 248 | self.bufferrust-analyzer cargo pyo3-filelike 0.5.3 PyTextFile#buffer..drainrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]drain().(..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#buf.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().()); |
| 249 | return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(buf.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().()); |
| 250 | } |
| 251 | let text = self |
| 252 | .innerrust-analyzer cargo pyo3-filelike 0.5.3 PyTextFile#inner. |
| 253 | .call_method1rust-analyzer cargo pyo3 0.29.0 instance/impl#[`Py<T>`]call_method1().(py, "read", (buf.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().self.bufferrust-analyzer cargo pyo3-filelike 0.5.3 PyTextFile#buffer..lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().(),))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 254 | if let Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(t) = text.extractrust-analyzer cargo pyo3 0.29.0 instance/impl#[`Py<T>`]extract().::<Cowrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc borrow/Cow#<str>>(py) { |
| 255 | self.bufferrust-analyzer cargo pyo3-filelike 0.5.3 PyTextFile#buffer..extend_from_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]extend_from_slice().(t.as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().()); |
| 256 | } else { |
| 257 | self.bufferrust-analyzer cargo pyo3-filelike 0.5.3 PyTextFile#buffer. |
| 258 | .extend_from_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]extend_from_slice().(text.extractrust-analyzer cargo pyo3 0.29.0 instance/impl#[`Py<T>`]extract().::<&[u8]>(py).map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|e| { |
| 259 | stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]new().(stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::InvalidDatarust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#InvalidData#, e.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()) |
| 260 | })?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().); |
| 261 | } |
| 262 | |
| 263 | let len = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/::minrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/min().(self.bufferrust-analyzer cargo pyo3-filelike 0.5.3 PyTextFile#buffer..lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().(), buf.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().()); |
| 264 | buf[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#len]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut()..copy_from_slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]copy_from_slice().(&self.bufferrust-analyzer cargo pyo3-filelike 0.5.3 PyTextFile#buffer.[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#len]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().); |
| 265 | self.bufferrust-analyzer cargo pyo3-filelike 0.5.3 PyTextFile#buffer..drainrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]drain().(..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#len); |
| 266 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(len) |
| 267 | }) |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | impl Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<Pyrust-analyzer cargo pyo3 0.29.0 instance/Py#<PyAnyrust-analyzer cargo pyo3 0.29.0 types/any/PyAny#>> for PyTextFilerust-analyzer cargo pyo3-filelike 0.5.3 PyTextFile# { |
| 272 | fn fromrust-analyzer cargo pyo3-filelike 0.5.3 impl#[PyTextFile][`From<Py<PyAny>>`]from().(obj: Pyrust-analyzer cargo pyo3 0.29.0 instance/Py#<PyAnyrust-analyzer cargo pyo3 0.29.0 types/any/PyAny#>) -> Selfrust-analyzer cargo pyo3-filelike 0.5.3 impl#[PyTextFile][`From<Py<PyAny>>`] { |
| 273 | PyTextFilerust-analyzer cargo pyo3-filelike 0.5.3 PyTextFile#::newrust-analyzer cargo pyo3-filelike 0.5.3 impl#[PyTextFile]new().(obj).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().() |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | impl Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<Boundrust-analyzer cargo pyo3 0.29.0 instance/Bound#<'_, PyAnyrust-analyzer cargo pyo3 0.29.0 types/any/PyAny#>> for PyTextFilerust-analyzer cargo pyo3-filelike 0.5.3 PyTextFile# { |
| 278 | fn fromrust-analyzer cargo pyo3-filelike 0.5.3 impl#[PyTextFile][`From<Bound<'_, PyAny>>`]from().(obj: Boundrust-analyzer cargo pyo3 0.29.0 instance/Bound#<'_, PyAnyrust-analyzer cargo pyo3 0.29.0 types/any/PyAny#>) -> Selfrust-analyzer cargo pyo3-filelike 0.5.3 impl#[PyTextFile][`From<Bound<'_, PyAny>>`] { |
| 279 | PyTextFilerust-analyzer cargo pyo3-filelike 0.5.3 PyTextFile#::newrust-analyzer cargo pyo3-filelike 0.5.3 impl#[PyTextFile]new().(obj.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().() |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | impl<'py> IntoPyObjectrust-analyzer cargo pyo3 0.29.0 conversion/IntoPyObject#<'py> for PyTextFilerust-analyzer cargo pyo3-filelike 0.5.3 PyTextFile# { |
| 284 | type Targetrust-analyzer cargo pyo3-filelike 0.5.3 impl#[PyTextFile][`IntoPyObject<'py>`]Target# = PyAnyrust-analyzer cargo pyo3 0.29.0 types/any/PyAny#; |
| 285 | |
| 286 | type Outputrust-analyzer cargo pyo3-filelike 0.5.3 impl#[PyTextFile][`IntoPyObject<'py>`]Output# = Boundrust-analyzer cargo pyo3 0.29.0 instance/Bound#<'py, PyAnyrust-analyzer cargo pyo3 0.29.0 types/any/PyAny#>; |
| 287 | |
| 288 | type Errorrust-analyzer cargo pyo3-filelike 0.5.3 impl#[PyTextFile][`IntoPyObject<'py>`]Error# = PyErrrust-analyzer cargo pyo3 0.29.0 err/PyErr#; |
| 289 | |
| 290 | fn into_pyobjectrust-analyzer cargo pyo3-filelike 0.5.3 impl#[PyTextFile][`IntoPyObject<'py>`]into_pyobject().(self, py: Pythonrust-analyzer cargo pyo3 0.29.0 marker/Python#<'py>) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<Selfrust-analyzer cargo pyo3-filelike 0.5.3 impl#[PyTextFile][`IntoPyObject<'py>`]::Outputrust-analyzer cargo pyo3 0.29.0 conversion/IntoPyObject#Output#, Selfrust-analyzer cargo pyo3-filelike 0.5.3 impl#[PyTextFile][`IntoPyObject<'py>`]::Errorrust-analyzer cargo pyo3 0.29.0 conversion/IntoPyObject#Error#> { |
| 291 | let obj = self.innerrust-analyzer cargo pyo3-filelike 0.5.3 PyTextFile#inner..clone_refrust-analyzer cargo pyo3 0.29.0 instance/impl#[`Py<T>`]clone_ref().(py); |
| 292 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(obj.into_boundrust-analyzer cargo pyo3 0.29.0 instance/impl#[`Py<T>`]into_bound().(py)) |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | impl Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone# for PyTextFilerust-analyzer cargo pyo3-filelike 0.5.3 PyTextFile# { |
| 297 | fn clonerust-analyzer cargo pyo3-filelike 0.5.3 impl#[PyTextFile][Clone]clone().(&self) -> Selfrust-analyzer cargo pyo3-filelike 0.5.3 impl#[PyTextFile][Clone] { |
| 298 | Pythonrust-analyzer cargo pyo3 0.29.0 marker/Python#::attachrust-analyzer cargo pyo3 0.29.0 marker/impl#[`Python<'_>`]attach().(|py| PyTextFilerust-analyzer cargo pyo3-filelike 0.5.3 PyTextFile#::fromrust-analyzer cargo pyo3-filelike 0.5.3 impl#[PyTextFile][`From<Py<PyAny>>`]from().(self.innerrust-analyzer cargo pyo3-filelike 0.5.3 PyTextFile#inner..clone_refrust-analyzer cargo pyo3 0.29.0 instance/impl#[`Py<T>`]clone_ref().(py))) |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | #[cfg(test)] |
| 303 | mod testsrust-analyzer cargo pyo3-filelike 0.5.3 tests/ { |
| 304 | use superrust-analyzer cargo pyo3-filelike 0.5.3 crate/::*; |
| 305 | use pyo3rust-analyzer cargo pyo3 0.29.0 crate/::ffirust-analyzer cargo pyo3 0.29.0 ffi/::c_strrust-analyzer cargo pyo3-ffi 0.29.0 c_str!; |
| 306 | |
| 307 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 308 | fn test_readrust-analyzer cargo pyo3-filelike 0.5.3 tests/test_read().() { |
| 309 | Pythonrust-analyzer cargo pyo3 0.29.0 marker/Python#::attachrust-analyzer cargo pyo3 0.29.0 marker/impl#[`Python<'_>`]attach().(|py| -> PyResultrust-analyzer cargo pyo3 0.29.0 err/PyResult#<()> { |
| 310 | let io = py.importrust-analyzer cargo pyo3 0.29.0 marker/impl#[`Python<'py>`]import().("io")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 311 | let file = io.call_method1rust-analyzer cargo pyo3 0.29.0 types/any/impl#[`Bound<'py, PyAny>`][`PyAnyMethods<'py>`]call_method1().("BytesIO", (&b"hello"[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFull#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().,))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 312 | let mut file = PyBinaryFilerust-analyzer cargo pyo3-filelike 0.5.3 PyBinaryFile#::fromrust-analyzer cargo pyo3-filelike 0.5.3 impl#[PyBinaryFile][`From<Bound<'_, PyAny>>`]from().(file); |
| 313 | let mut buf = [0u8; 5]; |
| 314 | file.read_exactrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#read_exact().(&mut buf)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 315 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(&buf, b"hello"); |
| 316 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 317 | }) |
| 318 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 319 | } |
| 320 | |
| 321 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 322 | fn test_read_notexactrust-analyzer cargo pyo3-filelike 0.5.3 tests/test_read_notexact().() { |
| 323 | Pythonrust-analyzer cargo pyo3 0.29.0 marker/Python#::attachrust-analyzer cargo pyo3 0.29.0 marker/impl#[`Python<'_>`]attach().(|py| -> PyResultrust-analyzer cargo pyo3 0.29.0 err/PyResult#<()> { |
| 324 | let io = py.importrust-analyzer cargo pyo3 0.29.0 marker/impl#[`Python<'py>`]import().("io")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 325 | let file = io.call_method1rust-analyzer cargo pyo3 0.29.0 types/any/impl#[`Bound<'py, PyAny>`][`PyAnyMethods<'py>`]call_method1().("BytesIO", (&b"hello"[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFull#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().,))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 326 | let mut file = PyBinaryFilerust-analyzer cargo pyo3-filelike 0.5.3 PyBinaryFile#::fromrust-analyzer cargo pyo3-filelike 0.5.3 impl#[PyBinaryFile][`From<Bound<'_, PyAny>>`]from().(file); |
| 327 | let mut buf = [0u8; 10]; |
| 328 | let n = file.readrust-analyzer cargo pyo3-filelike 0.5.3 impl#[PyBinaryFile][Read]read().(&mut buf)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 329 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(n, 5); |
| 330 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(&buf[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()...n]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`Index<I>`]index()., b"hello"); |
| 331 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 332 | }) |
| 333 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 334 | } |
| 335 | |
| 336 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 337 | fn test_read_eofrust-analyzer cargo pyo3-filelike 0.5.3 tests/test_read_eof().() { |
| 338 | Pythonrust-analyzer cargo pyo3 0.29.0 marker/Python#::attachrust-analyzer cargo pyo3 0.29.0 marker/impl#[`Python<'_>`]attach().(|py| -> PyResultrust-analyzer cargo pyo3 0.29.0 err/PyResult#<()> { |
| 339 | let io = py.importrust-analyzer cargo pyo3 0.29.0 marker/impl#[`Python<'py>`]import().("io")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 340 | let file = io.call_method1rust-analyzer cargo pyo3 0.29.0 types/any/impl#[`Bound<'py, PyAny>`][`PyAnyMethods<'py>`]call_method1().("BytesIO", (&b"hello"[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFull#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().,))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 341 | let mut file = PyBinaryFilerust-analyzer cargo pyo3-filelike 0.5.3 PyBinaryFile#::fromrust-analyzer cargo pyo3-filelike 0.5.3 impl#[PyBinaryFile][`From<Bound<'_, PyAny>>`]from().(file); |
| 342 | let mut buf = [0u8; 6]; |
| 343 | let err = file.read_exactrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#read_exact().(&mut buf).unwrap_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_err().(); |
| 344 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(err.kindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]kind().(), stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::UnexpectedEofrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#UnexpectedEof#); |
| 345 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 346 | }) |
| 347 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 348 | } |
| 349 | |
| 350 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 351 | fn test_read_to_endrust-analyzer cargo pyo3-filelike 0.5.3 tests/test_read_to_end().() { |
| 352 | Pythonrust-analyzer cargo pyo3 0.29.0 marker/Python#::attachrust-analyzer cargo pyo3 0.29.0 marker/impl#[`Python<'_>`]attach().(|py| -> PyResultrust-analyzer cargo pyo3 0.29.0 err/PyResult#<()> { |
| 353 | let io = py.importrust-analyzer cargo pyo3 0.29.0 marker/impl#[`Python<'py>`]import().("io")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 354 | let file = io.call_method1rust-analyzer cargo pyo3 0.29.0 types/any/impl#[`Bound<'py, PyAny>`][`PyAnyMethods<'py>`]call_method1().("BytesIO", (&b"hello"[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFull#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().,))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 355 | let mut file = PyBinaryFilerust-analyzer cargo pyo3-filelike 0.5.3 PyBinaryFile#::fromrust-analyzer cargo pyo3-filelike 0.5.3 impl#[PyBinaryFile][`From<Bound<'_, PyAny>>`]from().(file); |
| 356 | let mut buf = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().(); |
| 357 | file.read_to_endrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#read_to_end().(&mut buf)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 358 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(&buf, b"hello"); |
| 359 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 360 | }) |
| 361 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 362 | } |
| 363 | |
| 364 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 365 | fn test_writerust-analyzer cargo pyo3-filelike 0.5.3 tests/test_write().() { |
| 366 | Pythonrust-analyzer cargo pyo3 0.29.0 marker/Python#::attachrust-analyzer cargo pyo3 0.29.0 marker/impl#[`Python<'_>`]attach().(|py| { |
| 367 | let io = py.importrust-analyzer cargo pyo3 0.29.0 marker/impl#[`Python<'py>`]import().("io")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 368 | let file = io.call_method1rust-analyzer cargo pyo3 0.29.0 types/any/impl#[`Bound<'py, PyAny>`][`PyAnyMethods<'py>`]call_method1().("BytesIO", (&b""[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFull#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().,))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 369 | let mut file = PyBinaryFilerust-analyzer cargo pyo3-filelike 0.5.3 PyBinaryFile#::fromrust-analyzer cargo pyo3-filelike 0.5.3 impl#[PyBinaryFile][`From<Bound<'_, PyAny>>`]from().(file); |
| 370 | file.write_allrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#write_all().(b"hello ")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 371 | file.write_allrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#write_all().(b"world")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 372 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 373 | file.0rust-analyzer cargo pyo3-filelike 0.5.3 PyBinaryFile#0..call_method0rust-analyzer cargo pyo3 0.29.0 instance/impl#[`Py<T>`]call_method0().(py, "getvalue")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()..extractrust-analyzer cargo pyo3 0.29.0 instance/impl#[`Py<T>`]extract().::<&[u8]>(py)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()., |
| 374 | b"hello world" |
| 375 | ); |
| 376 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#::<(), PyErrrust-analyzer cargo pyo3 0.29.0 err/PyErr#>(()) |
| 377 | }) |
| 378 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 379 | } |
| 380 | |
| 381 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 382 | fn test_seekrust-analyzer cargo pyo3-filelike 0.5.3 tests/test_seek().() { |
| 383 | Pythonrust-analyzer cargo pyo3 0.29.0 marker/Python#::attachrust-analyzer cargo pyo3 0.29.0 marker/impl#[`Python<'_>`]attach().(|py| { |
| 384 | let io = py.importrust-analyzer cargo pyo3 0.29.0 marker/impl#[`Python<'py>`]import().("io")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 385 | let file = io.call_method1rust-analyzer cargo pyo3 0.29.0 types/any/impl#[`Bound<'py, PyAny>`][`PyAnyMethods<'py>`]call_method1().("BytesIO", (&b"hello"[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFull#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().,))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 386 | let mut file = PyBinaryFilerust-analyzer cargo pyo3-filelike 0.5.3 PyBinaryFile#::fromrust-analyzer cargo pyo3-filelike 0.5.3 impl#[PyBinaryFile][`From<Bound<'_, PyAny>>`]from().(file); |
| 387 | file.seekrust-analyzer cargo pyo3-filelike 0.5.3 impl#[PyBinaryFile][Seek]seek().(stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::SeekFromrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/SeekFrom#::Startrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/SeekFrom#Start#(1))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 388 | let mut buf = [0u8; 4]; |
| 389 | file.read_exactrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#read_exact().(&mut buf)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 390 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(&buf, b"ello"); |
| 391 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#::<(), PyErrrust-analyzer cargo pyo3 0.29.0 err/PyErr#>(()) |
| 392 | }) |
| 393 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 394 | } |
| 395 | |
| 396 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 397 | fn test_flushrust-analyzer cargo pyo3-filelike 0.5.3 tests/test_flush().() { |
| 398 | Pythonrust-analyzer cargo pyo3 0.29.0 marker/Python#::attachrust-analyzer cargo pyo3 0.29.0 marker/impl#[`Python<'_>`]attach().(|py| { |
| 399 | let io = py.importrust-analyzer cargo pyo3 0.29.0 marker/impl#[`Python<'py>`]import().("io")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 400 | let file = io.call_method1rust-analyzer cargo pyo3 0.29.0 types/any/impl#[`Bound<'py, PyAny>`][`PyAnyMethods<'py>`]call_method1().("BytesIO", (&b""[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFull#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().,))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 401 | let mut file = PyBinaryFilerust-analyzer cargo pyo3-filelike 0.5.3 PyBinaryFile#::fromrust-analyzer cargo pyo3-filelike 0.5.3 impl#[PyBinaryFile][`From<Bound<'_, PyAny>>`]from().(file); |
| 402 | file.write_allrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#write_all().(b"hello")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 403 | file.flushrust-analyzer cargo pyo3-filelike 0.5.3 impl#[PyBinaryFile][Write]flush().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 404 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!( |
| 405 | file.0rust-analyzer cargo pyo3-filelike 0.5.3 PyBinaryFile#0..call_method0rust-analyzer cargo pyo3 0.29.0 instance/impl#[`Py<T>`]call_method0().(py, "getvalue")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()..extractrust-analyzer cargo pyo3 0.29.0 instance/impl#[`Py<T>`]extract().::<&[u8]>(py)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()., |
| 406 | b"hello" |
| 407 | ); |
| 408 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#::<(), PyErrrust-analyzer cargo pyo3 0.29.0 err/PyErr#>(()) |
| 409 | }) |
| 410 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 411 | } |
| 412 | |
| 413 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 414 | fn test_read_textrust-analyzer cargo pyo3-filelike 0.5.3 tests/test_read_text().() { |
| 415 | Pythonrust-analyzer cargo pyo3 0.29.0 marker/Python#::attachrust-analyzer cargo pyo3 0.29.0 marker/impl#[`Python<'_>`]attach().(|py| -> PyResultrust-analyzer cargo pyo3 0.29.0 err/PyResult#<()> { |
| 416 | let io = py.importrust-analyzer cargo pyo3 0.29.0 marker/impl#[`Python<'py>`]import().("io")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 417 | let file = io.call_method1rust-analyzer cargo pyo3 0.29.0 types/any/impl#[`Bound<'py, PyAny>`][`PyAnyMethods<'py>`]call_method1().("StringIO", ("hello world",))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 418 | let mut file = PyTextFilerust-analyzer cargo pyo3-filelike 0.5.3 PyTextFile#::fromrust-analyzer cargo pyo3-filelike 0.5.3 impl#[PyTextFile][`From<Bound<'_, PyAny>>`]from().(file); |
| 419 | let mut buf = [0u8; 5]; |
| 420 | file.read_exactrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#read_exact().(&mut buf)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 421 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(&buf, b"hello"); |
| 422 | file.read_exactrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#read_exact().(&mut buf)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 423 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(&buf, b" worl"); |
| 424 | let mut buf = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().(); |
| 425 | file.read_to_endrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#read_to_end().(&mut buf).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 426 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(&buf, b"d"); |
| 427 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 428 | }) |
| 429 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 430 | } |
| 431 | |
| 432 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 433 | fn test_read_text_unicoderust-analyzer cargo pyo3-filelike 0.5.3 tests/test_read_text_unicode().() { |
| 434 | // read halfway through a unicode character |
| 435 | let io = Pythonrust-analyzer cargo pyo3 0.29.0 marker/Python#::attachrust-analyzer cargo pyo3 0.29.0 marker/impl#[`Python<'_>`]attach().(|py| -> PyResultrust-analyzer cargo pyo3 0.29.0 err/PyResult#<Pyrust-analyzer cargo pyo3 0.29.0 instance/Py#<PyAnyrust-analyzer cargo pyo3 0.29.0 types/any/PyAny#>> { |
| 436 | let io = py.importrust-analyzer cargo pyo3 0.29.0 marker/impl#[`Python<'py>`]import().("io")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 437 | let file = io.call_method1rust-analyzer cargo pyo3 0.29.0 types/any/impl#[`Bound<'py, PyAny>`][`PyAnyMethods<'py>`]call_method1().("StringIO", ("hello \u{1f600} world",))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 438 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(file.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().()) |
| 439 | }) |
| 440 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 441 | |
| 442 | let mut file = PyTextFilerust-analyzer cargo pyo3-filelike 0.5.3 PyTextFile#::fromrust-analyzer cargo pyo3-filelike 0.5.3 impl#[PyTextFile][`From<Py<PyAny>>`]from().(io); |
| 443 | let mut buf = [0u8; 7]; |
| 444 | file.read_exactrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#read_exact().(&mut buf).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 445 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(&buf, b"hello \xf0"); |
| 446 | |
| 447 | let mut buf = [0u8; 1]; |
| 448 | file.read_exactrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#read_exact().(&mut buf).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 449 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(&buf, b"\x9f"); |
| 450 | } |
| 451 | |
| 452 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 453 | fn test_seekable_binaryrust-analyzer cargo pyo3-filelike 0.5.3 tests/test_seekable_binary().() { |
| 454 | Pythonrust-analyzer cargo pyo3 0.29.0 marker/Python#::attachrust-analyzer cargo pyo3 0.29.0 marker/impl#[`Python<'_>`]attach().(|py| -> PyResultrust-analyzer cargo pyo3 0.29.0 err/PyResult#<()> { |
| 455 | let io = py.importrust-analyzer cargo pyo3 0.29.0 marker/impl#[`Python<'py>`]import().("io")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 456 | let file = io.call_method1rust-analyzer cargo pyo3 0.29.0 types/any/impl#[`Bound<'py, PyAny>`][`PyAnyMethods<'py>`]call_method1().("BytesIO", (&b"hello"[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFull#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().,))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 457 | let file = PyBinaryFilerust-analyzer cargo pyo3-filelike 0.5.3 PyBinaryFile#::fromrust-analyzer cargo pyo3-filelike 0.5.3 impl#[PyBinaryFile][`From<Bound<'_, PyAny>>`]from().(file); |
| 458 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(file.seekablerust-analyzer cargo pyo3-filelike 0.5.3 impl#[PyBinaryFile]seekable().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()., true); |
| 459 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 460 | }) |
| 461 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 462 | } |
| 463 | |
| 464 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 465 | fn test_seekable_textrust-analyzer cargo pyo3-filelike 0.5.3 tests/test_seekable_text().() { |
| 466 | Pythonrust-analyzer cargo pyo3 0.29.0 marker/Python#::attachrust-analyzer cargo pyo3 0.29.0 marker/impl#[`Python<'_>`]attach().(|py| -> PyResultrust-analyzer cargo pyo3 0.29.0 err/PyResult#<()> { |
| 467 | let io = py.importrust-analyzer cargo pyo3 0.29.0 marker/impl#[`Python<'py>`]import().("io")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 468 | let file = io.call_method1rust-analyzer cargo pyo3 0.29.0 types/any/impl#[`Bound<'py, PyAny>`][`PyAnyMethods<'py>`]call_method1().("StringIO", ("hello",))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 469 | let file = PyTextFilerust-analyzer cargo pyo3-filelike 0.5.3 PyTextFile#::fromrust-analyzer cargo pyo3-filelike 0.5.3 impl#[PyTextFile][`From<Bound<'_, PyAny>>`]from().(file); |
| 470 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(file.seekablerust-analyzer cargo pyo3-filelike 0.5.3 impl#[PyTextFile]seekable().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()., true); |
| 471 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 472 | }) |
| 473 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 474 | } |
| 475 | |
| 476 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 477 | fn test_non_seekable_binaryrust-analyzer cargo pyo3-filelike 0.5.3 tests/test_non_seekable_binary().() { |
| 478 | Pythonrust-analyzer cargo pyo3 0.29.0 marker/Python#::attachrust-analyzer cargo pyo3 0.29.0 marker/impl#[`Python<'_>`]attach().(|py| -> PyResultrust-analyzer cargo pyo3 0.29.0 err/PyResult#<()> { |
| 479 | // Create a custom non-seekable file-like object |
| 480 | let locals = pyo3rust-analyzer cargo pyo3 0.29.0 crate/::typesrust-analyzer cargo pyo3 0.29.0 types/::PyDictrust-analyzer cargo pyo3 0.29.0 types/dict/PyDict#::newrust-analyzer cargo pyo3 0.29.0 types/dict/impl#[PyDict]new().(py); |
| 481 | py.runrust-analyzer cargo pyo3 0.29.0 marker/impl#[`Python<'py>`]run().( |
| 482 | c_strrust-analyzer cargo pyo3-ffi 0.29.0 c_str!!( |
| 483 | r#"class NonSeekableFile: |
| 484 | def __init__(self): |
| 485 | self.data = b"hello" |
| 486 | self.pos = 0 |
| 487 | |
| 488 | def read(self, n=-1): |
| 489 | if n == -1: |
| 490 | result = self.data[self.pos:] |
| 491 | self.pos = len(self.data) |
| 492 | else: |
| 493 | result = self.data[self.pos:self.pos + n] |
| 494 | self.pos += len(result) |
| 495 | return result |
| 496 | |
| 497 | def write(self, data): |
| 498 | return len(data) |
| 499 | |
| 500 | def flush(self): |
| 501 | pass |
| 502 | |
| 503 | def seekable(self): |
| 504 | return False |
| 505 | |
| 506 | @property |
| 507 | def mode(self): |
| 508 | return 'rb' |
| 509 | |
| 510 | file = NonSeekableFile()"# |
| 511 | ), |
| 512 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, |
| 513 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(&locals), |
| 514 | )?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 515 | let file = locals.get_itemrust-analyzer cargo pyo3 0.29.0 types/dict/impl#[`Bound<'py, PyDict>`][`PyDictMethods<'py>`]get_item().("file")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()..unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(); |
| 516 | let file = PyBinaryFilerust-analyzer cargo pyo3-filelike 0.5.3 PyBinaryFile#::fromrust-analyzer cargo pyo3-filelike 0.5.3 impl#[PyBinaryFile][`From<Bound<'_, PyAny>>`]from().(file); |
| 517 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(file.seekablerust-analyzer cargo pyo3-filelike 0.5.3 impl#[PyBinaryFile]seekable().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()., false); |
| 518 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 519 | }) |
| 520 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 521 | } |
| 522 | |
| 523 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 524 | fn test_non_seekable_textrust-analyzer cargo pyo3-filelike 0.5.3 tests/test_non_seekable_text().() { |
| 525 | Pythonrust-analyzer cargo pyo3 0.29.0 marker/Python#::attachrust-analyzer cargo pyo3 0.29.0 marker/impl#[`Python<'_>`]attach().(|py| -> PyResultrust-analyzer cargo pyo3 0.29.0 err/PyResult#<()> { |
| 526 | // Create a custom non-seekable file-like object |
| 527 | let locals = pyo3rust-analyzer cargo pyo3 0.29.0 crate/::typesrust-analyzer cargo pyo3 0.29.0 types/::PyDictrust-analyzer cargo pyo3 0.29.0 types/dict/PyDict#::newrust-analyzer cargo pyo3 0.29.0 types/dict/impl#[PyDict]new().(py); |
| 528 | py.runrust-analyzer cargo pyo3 0.29.0 marker/impl#[`Python<'py>`]run().( |
| 529 | c_strrust-analyzer cargo pyo3-ffi 0.29.0 c_str!!( |
| 530 | r#"class NonSeekableTextFile: |
| 531 | def __init__(self): |
| 532 | self.data = "hello" |
| 533 | self.pos = 0 |
| 534 | |
| 535 | def read(self, n=-1): |
| 536 | if n == -1: |
| 537 | result = self.data[self.pos:] |
| 538 | self.pos = len(self.data) |
| 539 | else: |
| 540 | result = self.data[self.pos:self.pos + n] |
| 541 | self.pos += len(result) |
| 542 | return result |
| 543 | |
| 544 | def seekable(self): |
| 545 | return False |
| 546 | |
| 547 | file = NonSeekableTextFile()"# |
| 548 | ), |
| 549 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, |
| 550 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(&locals), |
| 551 | )?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 552 | let file = locals.get_itemrust-analyzer cargo pyo3 0.29.0 types/dict/impl#[`Bound<'py, PyDict>`][`PyDictMethods<'py>`]get_item().("file")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()..unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().(); |
| 553 | let file = PyTextFilerust-analyzer cargo pyo3-filelike 0.5.3 PyTextFile#::fromrust-analyzer cargo pyo3-filelike 0.5.3 impl#[PyTextFile][`From<Bound<'_, PyAny>>`]from().(file); |
| 554 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(file.seekablerust-analyzer cargo pyo3-filelike 0.5.3 impl#[PyTextFile]seekable().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch()., false); |
| 555 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 556 | }) |
| 557 | .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 558 | } |
| 559 | } |