Comments (6)
Do you have a real world example that would use this?
from scroll.
Yes, similar to the example above, I was dealing with some C structs which had some header information, etc., then a large byte array containing image bytes, then some more stuff.
I wanted to pread the struct, match on some header values, validate, then do something with the bytes, then write them out to disk with slightly different header values.
Scroll would have been extremely fast way to prototype/work with the data, but because I couldn't box the pread data, it exploded my stack; testing with the pwrite version taking references, I was able to write the data, which was one half of the problem.
Hope that's clear?
I think this will generally come up in FFI with large C structs of byte arrays where the api expects a malloc(sizeof(LargeStruct))
to construct the initial type.
from scroll.
I was hoping for an actual real world example. It seems to me that large fixed size arrays are quite uncommon, and even then you can usually use Vec<u8>
anyway. So the use case is:
- you have a struct containing a large fixed size array
- you need to pass this struct to C with FFI so you can't use
Vec
?
from scroll.
It was the other way around, basically:
struct Foo {
struct Header header;
uint8_t data [0x8000];
struct Footer footer;
}
In rust land I then wanted to:
let x = bytes.pread::<Foo>(0)?; // this fails because we allocate x on the stack.
// check x.header, do stuff, manipulate x.bytes, pwrite
from scroll.
So I had something like this in mind 😈 , but it still overflows the stack (it also allocates twice which sucks):
impl<'a, Ctx, T> TryFromCtx<'a, Ctx> for Box<T>
where
T: TryFromCtx<'a, Ctx>,
Ctx: Copy,
error::Error: From<<T as TryFromCtx<'a, Ctx>>::Error>
{
type Error = error::Error;
#[inline]
fn try_from_ctx(src: &'a [u8], ctx: Ctx) -> result::Result<(Self, usize), Self::Error> {
let res = Box::new(TryFromCtx::try_from_ctx(src, ctx)?);
Ok((Box::new(res.0), res.1))
}
}
#[test]
fn pwrite_big_struct() {
use scroll::{LE, Pwrite};
#[derive(Pread, Pwrite, Clone)]
struct Big {
arr: [u8; 10000000],
}
let mut bytes = vec![0; 10000001];
let big = bytes.pread_with::<Box<Big>>(0, LE).unwrap();
let _ = bytes.pwrite_with(big.as_ref(), 0, LE).unwrap();
assert!(false);
}
I'm wondering if its even possible to write in scroll TryFromCtx
as is without blowing up the stack?
from scroll.
If Big::try_from_ctx(src, ctx)
overflows the stack, then wrapping it in Box::new
won't change anything. You need placement new or an equivalent (pass in a preallocated pointer for the destination of the read, instead of returning a value), which is incompatible with returning by value.
from scroll.
Related Issues (20)
- derive newtypes?
- Generated Pread code is large/slow HOT 3
- derive(Pread) doesn't work with fields containing an array of structs HOT 6
- derive(Pread) in structs with elements that don't use scroll::Endian as ctx HOT 4
- remove usize pread/pwrite HOT 1
- scroll 1.0 HOT 12
- Repo in Cargo.toml for scroll_derive refers to old repo
- What does the "g" in "gread" stand for? HOT 3
- Publish new versions to crates.io HOT 4
- usize/isize impls must die HOT 1
- tests fail in README.md on published crates HOT 6
- Docs mention lost Gread and Gwrite traits HOT 3
- cargo test --no-default-features fails HOT 2
- Enums derive HOT 10
- Is it possible to do stateful parsing? HOT 2
- Pread, Pwrite with struct containing [u8; 8] do not work anymore with Rust 1.47.0 HOT 18
- src/lib.rs doctest fails on s390x / IBM System Z (big endian) HOT 7
- Writing into a dynamic container HOT 20
- `Pwrite`-ing to a Tokio BufWriter - is it possible?
- Pwrite deriving fails with "unparseable tokens" with struct containing lifetimes? HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from scroll.