nowasm/
components.rs

1use crate::decode::Decode;
2use crate::instructions::Instr;
3use crate::reader::Reader;
4use crate::vector::Vector;
5use crate::{DecodeError, ExecuteError, GlobalVal, Module, Val, VectorFactory, PAGE_SIZE};
6use core::fmt::{Debug, Formatter};
7
8pub struct Name<V: VectorFactory>(V::Vector<u8>);
9
10impl<V: VectorFactory> Name<V> {
11    pub fn as_str(&self) -> &str {
12        core::str::from_utf8(&self.0).expect("unreachable")
13    }
14
15    pub fn len(&self) -> usize {
16        self.0.len()
17    }
18
19    pub fn is_empty(&self) -> bool {
20        self.0.is_empty()
21    }
22}
23
24impl<V: VectorFactory> Decode<V> for Name<V> {
25    fn decode(reader: &mut Reader) -> Result<Self, DecodeError> {
26        let bytes = Decode::<V>::decode_vector(reader)?;
27        let _ = core::str::from_utf8(&bytes).map_err(DecodeError::InvalidUtf8)?;
28        Ok(Self(bytes))
29    }
30}
31
32impl<V: VectorFactory> Debug for Name<V> {
33    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
34        f.debug_tuple("Name").field(&self.as_str()).finish()
35    }
36}
37
38impl<V: VectorFactory> Clone for Name<V> {
39    fn clone(&self) -> Self {
40        Self(V::clone_vector(&self.0))
41    }
42}
43
44pub struct Import<V: VectorFactory> {
45    pub module: Name<V>,
46    pub name: Name<V>,
47    pub desc: Importdesc,
48}
49
50impl<V: VectorFactory> Decode<V> for Import<V> {
51    fn decode(reader: &mut Reader) -> Result<Self, DecodeError> {
52        let module = Name::decode(reader)?;
53        let name = Name::decode(reader)?;
54        let desc = Decode::<V>::decode(reader)?;
55        Ok(Self { module, name, desc })
56    }
57}
58
59impl<V: VectorFactory> Debug for Import<V> {
60    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
61        f.debug_struct("Import")
62            .field("module", &self.module)
63            .field("name", &self.name)
64            .field("desc", &self.desc)
65            .finish()
66    }
67}
68
69impl<V: VectorFactory> Clone for Import<V> {
70    fn clone(&self) -> Self {
71        Self {
72            module: self.module.clone(),
73            name: self.name.clone(),
74            desc: self.desc,
75        }
76    }
77}
78
79#[derive(Debug, Clone, Copy)]
80pub enum Importdesc {
81    Func(Typeidx),
82    Table(Tabletype),
83    Mem(Memtype),
84    Global(Globaltype),
85}
86
87impl<V: VectorFactory> Decode<V> for Importdesc {
88    fn decode(reader: &mut Reader) -> Result<Self, DecodeError> {
89        match reader.read_u8()? {
90            0x00 => Ok(Self::Func(Decode::<V>::decode(reader)?)),
91            0x01 => Ok(Self::Table(Decode::<V>::decode(reader)?)),
92            0x02 => Ok(Self::Mem(Decode::<V>::decode(reader)?)),
93            0x03 => Ok(Self::Global(Decode::<V>::decode(reader)?)),
94            value => Err(DecodeError::InvalidImportDescTag { value }),
95        }
96    }
97}
98
99pub struct Export<V: VectorFactory> {
100    pub name: Name<V>,
101    pub desc: Exportdesc,
102}
103
104impl<V: VectorFactory> Decode<V> for Export<V> {
105    fn decode(reader: &mut Reader) -> Result<Self, DecodeError> {
106        let name = Name::decode(reader)?;
107        let desc = Decode::<V>::decode(reader)?;
108        Ok(Self { name, desc })
109    }
110}
111
112impl<V: VectorFactory> Debug for Export<V> {
113    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
114        f.debug_struct("Export")
115            .field("name", &self.name)
116            .field("desc", &self.desc)
117            .finish()
118    }
119}
120
121impl<V: VectorFactory> Clone for Export<V> {
122    fn clone(&self) -> Self {
123        Self {
124            name: self.name.clone(),
125            desc: self.desc,
126        }
127    }
128}
129
130#[derive(Debug, Clone, Copy)]
131pub enum Exportdesc {
132    Func(Funcidx),
133    Table(Tableidx),
134    Mem(Memidx),
135    Global(Globalidx),
136}
137
138impl<V: VectorFactory> Decode<V> for Exportdesc {
139    fn decode(reader: &mut Reader) -> Result<Self, DecodeError> {
140        match reader.read_u8()? {
141            0x00 => Ok(Self::Func(Decode::<V>::decode(reader)?)),
142            0x01 => Ok(Self::Table(Decode::<V>::decode(reader)?)),
143            0x02 => Ok(Self::Mem(Decode::<V>::decode(reader)?)),
144            0x03 => Ok(Self::Global(Decode::<V>::decode(reader)?)),
145            value => Err(DecodeError::InvalidExportDescTag { value }),
146        }
147    }
148}
149
150#[derive(Debug, Clone, Copy)]
151pub struct Typeidx(u32);
152
153impl Typeidx {
154    pub const fn get(self) -> usize {
155        self.0 as usize
156    }
157}
158
159impl<V: VectorFactory> Decode<V> for Typeidx {
160    fn decode(reader: &mut Reader) -> Result<Self, DecodeError> {
161        reader.read_u32().map(Self)
162    }
163}
164
165#[derive(Debug, Clone, Copy)]
166pub struct Funcidx(u32);
167
168impl Funcidx {
169    pub const fn get(self) -> usize {
170        self.0 as usize
171    }
172}
173
174impl<V: VectorFactory> Decode<V> for Funcidx {
175    fn decode(reader: &mut Reader) -> Result<Self, DecodeError> {
176        reader.read_u32().map(Self)
177    }
178}
179
180#[derive(Debug, Clone, Copy)]
181pub struct Tableidx;
182
183impl<V: VectorFactory> Decode<V> for Tableidx {
184    fn decode(reader: &mut Reader) -> Result<Self, DecodeError> {
185        let i = reader.read_u32()?;
186        if i != 0 {
187            return Err(DecodeError::InvalidTableIdx { value: i });
188        }
189        Ok(Self)
190    }
191}
192
193#[derive(Debug, Clone, Copy)]
194pub struct Memidx;
195
196impl<V: VectorFactory> Decode<V> for Memidx {
197    fn decode(reader: &mut Reader) -> Result<Self, DecodeError> {
198        let i = reader.read_u32()?;
199        if i != 0 {
200            return Err(DecodeError::InvalidMemIdx { value: i });
201        }
202        Ok(Self)
203    }
204}
205
206#[derive(Debug, Clone, Copy)]
207pub struct Globalidx(u32);
208
209impl Globalidx {
210    pub const fn get(self) -> usize {
211        self.0 as usize
212    }
213}
214
215impl<V: VectorFactory> Decode<V> for Globalidx {
216    fn decode(reader: &mut Reader) -> Result<Self, DecodeError> {
217        reader.read_u32().map(Self)
218    }
219}
220
221#[derive(Debug, Clone, Copy)]
222pub struct Localidx(u32);
223
224impl Localidx {
225    pub const fn get(self) -> usize {
226        self.0 as usize
227    }
228}
229
230impl<V: VectorFactory> Decode<V> for Localidx {
231    fn decode(reader: &mut Reader) -> Result<Self, DecodeError> {
232        reader.read_u32().map(Self)
233    }
234}
235
236#[derive(Debug, Clone, Copy)]
237pub struct Labelidx(u32);
238
239impl Labelidx {
240    pub const fn new(v: u32) -> Self {
241        Self(v)
242    }
243
244    pub const fn get(self) -> usize {
245        self.0 as usize
246    }
247}
248
249impl<V: VectorFactory> Decode<V> for Labelidx {
250    fn decode(reader: &mut Reader) -> Result<Self, DecodeError> {
251        reader.read_u32().map(Self)
252    }
253}
254
255#[derive(Debug, Clone, Copy)]
256pub struct Tabletype {
257    pub elemtype: Elemtype,
258    pub limits: Limits,
259}
260
261impl Tabletype {
262    pub fn contains(self, size: usize) -> bool {
263        match self.limits.max {
264            Some(max) => size >= self.limits.min as usize && size <= max as usize,
265            None => size >= self.limits.min as usize,
266        }
267    }
268}
269
270impl<V: VectorFactory> Decode<V> for Tabletype {
271    fn decode(reader: &mut Reader) -> Result<Self, DecodeError> {
272        let elemtype = Decode::<V>::decode(reader)?;
273        let limits = Decode::<V>::decode(reader)?;
274        Ok(Self { elemtype, limits })
275    }
276}
277
278#[derive(Debug, Clone, Copy)]
279pub struct Elemtype;
280
281impl<V: VectorFactory> Decode<V> for Elemtype {
282    fn decode(reader: &mut Reader) -> Result<Self, DecodeError> {
283        let elem_type = reader.read_u8()?;
284        if elem_type != 0x70 {
285            return Err(DecodeError::InvalidElemType { value: elem_type });
286        }
287        Ok(Self)
288    }
289}
290
291#[derive(Debug, Clone, Copy)]
292pub struct Limits {
293    pub min: u32,
294    pub max: Option<u32>,
295}
296
297impl<V: VectorFactory> Decode<V> for Limits {
298    fn decode(reader: &mut Reader) -> Result<Self, DecodeError> {
299        match reader.read_u8()? {
300            0x00 => {
301                let min = reader.read_u32()?;
302                Ok(Self { min, max: None })
303            }
304            0x01 => {
305                let min = reader.read_u32()?;
306                let max = Some(reader.read_u32()?);
307                Ok(Self { min, max })
308            }
309            value => Err(DecodeError::InvalidLimitsFlag { value }),
310        }
311    }
312}
313
314#[derive(Debug, Clone, Copy)]
315pub struct Memtype {
316    pub limits: Limits,
317}
318
319impl Memtype {
320    pub fn min_bytes(self) -> usize {
321        self.limits.min as usize * PAGE_SIZE
322    }
323
324    pub fn max_bytes(self) -> Option<usize> {
325        self.limits.max.map(|max| max as usize * PAGE_SIZE)
326    }
327
328    pub fn contains(self, bytes: usize) -> bool {
329        if let Some(max) = self.max_bytes() {
330            self.min_bytes() <= bytes && bytes <= max
331        } else {
332            self.min_bytes() <= bytes
333        }
334    }
335}
336
337impl<V: VectorFactory> Decode<V> for Memtype {
338    fn decode(reader: &mut Reader) -> Result<Self, DecodeError> {
339        Ok(Self {
340            limits: Decode::<V>::decode(reader)?,
341        })
342    }
343}
344
345#[derive(Debug, Clone, Copy)]
346pub enum Globaltype {
347    Const(Valtype),
348    Var(Valtype),
349}
350
351impl Globaltype {
352    pub fn is_const(self) -> bool {
353        matches!(self, Self::Const(_))
354    }
355
356    pub fn valtype(self) -> Valtype {
357        match self {
358            Self::Const(t) => t,
359            Self::Var(t) => t,
360        }
361    }
362}
363
364impl<V: VectorFactory> Decode<V> for Globaltype {
365    fn decode(reader: &mut Reader) -> Result<Self, DecodeError> {
366        let t = Decode::<V>::decode(reader)?;
367        match reader.read_u8()? {
368            0x00 => Ok(Self::Const(t)),
369            0x01 => Ok(Self::Var(t)),
370            value => Err(DecodeError::InvalidMutabilityFlag { value }),
371        }
372    }
373}
374
375#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
376pub enum Valtype {
377    I32,
378    I64,
379    F32,
380    F64,
381}
382
383impl<V: VectorFactory> Decode<V> for Valtype {
384    fn decode(reader: &mut Reader) -> Result<Self, DecodeError> {
385        match reader.read_u8()? {
386            0x7f => Ok(Self::I32),
387            0x7e => Ok(Self::I64),
388            0x7d => Ok(Self::F32),
389            0x7c => Ok(Self::F64),
390            value => Err(DecodeError::InvalidValType { value }),
391        }
392    }
393}
394
395pub struct Func<V: VectorFactory> {
396    pub ty: Typeidx,
397    pub locals: V::Vector<Valtype>,
398    pub body: Expr<V>,
399}
400
401impl<V: VectorFactory> Debug for Func<V> {
402    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
403        f.debug_struct("Func")
404            .field("ty", &self.ty)
405            .field("locals", &self.locals.as_ref())
406            .field("body", &self.body)
407            .finish()
408    }
409}
410
411impl<V: VectorFactory> Clone for Func<V> {
412    fn clone(&self) -> Self {
413        Self {
414            ty: self.ty,
415            locals: V::clone_vector(&self.locals),
416            body: self.body.clone(),
417        }
418    }
419}
420
421pub struct Functype<V: VectorFactory> {
422    pub params: V::Vector<Valtype>,
423    pub result: Resulttype,
424}
425
426impl<V: VectorFactory> Functype<V> {
427    pub fn validate_args(
428        &self,
429        args: &[Val],
430        _module: &Module<impl VectorFactory>,
431    ) -> Result<(), ExecuteError> {
432        if args.len() != self.params.len() {
433            return Err(ExecuteError::InvalidFuncArgs);
434        }
435
436        for (&expected_type, actual_value) in self.params.iter().zip(args.iter()) {
437            if expected_type != actual_value.ty() {
438                return Err(ExecuteError::InvalidFuncArgs);
439            }
440        }
441
442        Ok(())
443    }
444}
445
446impl<V: VectorFactory> PartialEq for Functype<V> {
447    fn eq(&self, other: &Self) -> bool {
448        self.params.as_ref() == other.params.as_ref() && self.result == other.result
449    }
450}
451
452impl<V: VectorFactory> Eq for Functype<V> {}
453
454impl<V: VectorFactory> Decode<V> for Functype<V> {
455    fn decode(reader: &mut Reader) -> Result<Self, DecodeError> {
456        let tag = reader.read_u8()?;
457        if tag != 0x60 {
458            return Err(DecodeError::InvalidFuncTypeTag { value: tag });
459        }
460        let params = Decode::<V>::decode_vector(reader)?;
461        let result = Decode::<V>::decode(reader)?;
462        Ok(Self { params, result })
463    }
464}
465
466impl<V: VectorFactory> Debug for Functype<V> {
467    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
468        f.debug_struct("Functype")
469            .field("params", &self.params.as_ref())
470            .field("result", &self.result)
471            .finish()
472    }
473}
474
475impl<V: VectorFactory> Clone for Functype<V> {
476    fn clone(&self) -> Self {
477        Self {
478            params: V::clone_vector(&self.params),
479            result: self.result,
480        }
481    }
482}
483
484#[derive(Debug, Clone, Copy, PartialEq, Eq)]
485pub struct Resulttype(Option<Valtype>);
486
487impl Resulttype {
488    pub fn len(self) -> usize {
489        self.0.is_some() as usize
490    }
491
492    pub fn is_empty(self) -> bool {
493        self.0.is_none()
494    }
495
496    pub fn get(self) -> Option<Valtype> {
497        self.0
498    }
499}
500
501impl<V: VectorFactory> Decode<V> for Resulttype {
502    fn decode(reader: &mut Reader) -> Result<Self, DecodeError> {
503        let size = reader.read_usize()?;
504        match size {
505            0 => Ok(Self(None)),
506            1 => Ok(Self(Some(Decode::<V>::decode(reader)?))),
507            _ => Err(DecodeError::InvalidResultArity { value: size }),
508        }
509    }
510}
511
512#[derive(Debug, Clone)]
513pub struct Global {
514    pub ty: Globaltype,
515    pub init: ConstantExpr,
516}
517
518impl Global {
519    pub fn init(&self, imported_globals: &[GlobalVal]) -> Option<GlobalVal> {
520        match (self.ty.valtype(), self.init) {
521            (Valtype::I32, ConstantExpr::I32(v)) => {
522                Some(GlobalVal::new(self.ty.is_const(), Val::I32(v)))
523            }
524            (Valtype::I64, ConstantExpr::I64(v)) => {
525                Some(GlobalVal::new(self.ty.is_const(), Val::I64(v)))
526            }
527            (Valtype::F32, ConstantExpr::F32(v)) => {
528                Some(GlobalVal::new(self.ty.is_const(), Val::F32(v)))
529            }
530            (Valtype::F64, ConstantExpr::F64(v)) => {
531                Some(GlobalVal::new(self.ty.is_const(), Val::F64(v)))
532            }
533            (ty, ConstantExpr::Global(idx)) => {
534                let g = imported_globals.get(idx.get()).copied()?;
535                if !g.is_const() {
536                    return None;
537                }
538                if g.get().ty() == ty {
539                    Some(g)
540                } else {
541                    None
542                }
543            }
544            _ => None,
545        }
546    }
547}
548
549impl<V: VectorFactory> Decode<V> for Global {
550    fn decode(reader: &mut Reader) -> Result<Self, DecodeError> {
551        let ty = Decode::<V>::decode(reader)?;
552        let init = Decode::<V>::decode(reader)?;
553        Ok(Self { ty, init })
554    }
555}
556
557#[derive(Debug, Clone, Copy)]
558pub enum I32ConstantExpr {
559    I32(i32),
560    Global(Globalidx),
561}
562
563impl I32ConstantExpr {
564    pub fn get(self, globals: &[GlobalVal]) -> Option<i32> {
565        match self {
566            Self::I32(v) => Some(v),
567            Self::Global(idx) => {
568                let g = globals.get(idx.get()).copied()?;
569                if !g.is_const() {
570                    return None;
571                }
572                let Val::I32(v) = g.get() else {
573                    return None;
574                };
575                Some(v)
576            }
577        }
578    }
579}
580
581impl<V: VectorFactory> Decode<V> for I32ConstantExpr {
582    fn decode(reader: &mut Reader) -> Result<Self, DecodeError> {
583        let expr = Expr::<V>::decode(reader)?;
584        if expr.instrs().len() != 1 {
585            return Err(DecodeError::UnexpectedExpr);
586        }
587        match &expr.instrs()[0] {
588            Instr::I32Const(x) => Ok(Self::I32(*x)),
589            Instr::GlobalGet(x) => Ok(Self::Global(*x)),
590            _ => Err(DecodeError::UnexpectedExpr),
591        }
592    }
593}
594
595#[derive(Debug, Clone, Copy)]
596pub enum ConstantExpr {
597    I32(i32),
598    I64(i64),
599    F32(f32),
600    F64(f64),
601    Global(Globalidx),
602}
603
604impl<V: VectorFactory> Decode<V> for ConstantExpr {
605    fn decode(reader: &mut Reader) -> Result<Self, DecodeError> {
606        let expr = Expr::<V>::decode(reader)?;
607        if expr.instrs().len() != 1 {
608            return Err(DecodeError::UnexpectedExpr);
609        }
610        match &expr.instrs()[0] {
611            Instr::I32Const(x) => Ok(Self::I32(*x)),
612            Instr::I64Const(x) => Ok(Self::I64(*x)),
613            Instr::F32Const(x) => Ok(Self::F32(*x)),
614            Instr::F64Const(x) => Ok(Self::F64(*x)),
615            Instr::GlobalGet(x) => Ok(Self::Global(*x)),
616            _ => Err(DecodeError::UnexpectedExpr),
617        }
618    }
619}
620
621pub struct Expr<V: VectorFactory> {
622    instrs: V::Vector<Instr<V>>,
623}
624
625impl<V: VectorFactory> Expr<V> {
626    pub fn instrs(&self) -> &[Instr<V>] {
627        &self.instrs
628    }
629}
630
631impl<V: VectorFactory> Decode<V> for Expr<V> {
632    fn decode(reader: &mut Reader) -> Result<Self, DecodeError> {
633        let mut instrs = V::create_vector(None);
634        while reader.peek_u8()? != 0x0b {
635            instrs.push(Instr::decode(reader)?);
636        }
637        reader.read_u8()?;
638        Ok(Self { instrs })
639    }
640}
641
642impl<V: VectorFactory> Debug for Expr<V> {
643    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
644        f.debug_struct("Expr")
645            .field("instrs", &self.instrs.as_ref())
646            .finish()
647    }
648}
649
650impl<V: VectorFactory> Clone for Expr<V> {
651    fn clone(&self) -> Self {
652        Self {
653            instrs: V::clone_vector(&self.instrs),
654        }
655    }
656}
657
658#[derive(Debug, Clone, Copy)]
659pub struct Memarg {
660    pub align: u32,
661    pub offset: u32,
662}
663
664impl<V: VectorFactory> Decode<V> for Memarg {
665    fn decode(reader: &mut Reader) -> Result<Self, DecodeError> {
666        let align = reader.read_u32()?;
667        let offset = reader.read_u32()?;
668        Ok(Self { align, offset })
669    }
670}
671
672pub struct Elem<V: VectorFactory> {
673    pub table: Tableidx,
674    pub offset: I32ConstantExpr,
675    pub init: V::Vector<Funcidx>,
676}
677
678impl<V: VectorFactory> Decode<V> for Elem<V> {
679    fn decode(reader: &mut Reader) -> Result<Self, DecodeError> {
680        let table = Decode::<V>::decode(reader)?;
681        let offset = Decode::<V>::decode(reader)?;
682        let init = Decode::<V>::decode_vector(reader)?;
683        Ok(Self {
684            table,
685            offset,
686            init,
687        })
688    }
689}
690
691impl<V: VectorFactory> Debug for Elem<V> {
692    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
693        f.debug_struct("Elem")
694            .field("table", &self.table)
695            .field("offset", &self.offset)
696            .field("init", &self.init.as_ref())
697            .finish()
698    }
699}
700
701impl<V: VectorFactory> Clone for Elem<V> {
702    fn clone(&self) -> Self {
703        Self {
704            table: self.table,
705            offset: self.offset,
706            init: V::clone_vector(&self.init),
707        }
708    }
709}
710
711pub(crate) struct Code<V: VectorFactory> {
712    pub locals: V::Vector<Valtype>,
713    pub body: Expr<V>,
714}
715
716impl<V: VectorFactory> Decode<V> for Code<V> {
717    fn decode(reader: &mut Reader) -> Result<Self, DecodeError> {
718        let code_size = reader.read_usize()?;
719        let mut reader = Reader::new(reader.read(code_size)?);
720        let mut locals = V::create_vector(None);
721        let locals_len = reader.read_usize()?;
722        for _ in 0..locals_len {
723            let val_types_len = reader.read_usize()?;
724            let val_type = Decode::<V>::decode(&mut reader)?;
725            for _ in 0..val_types_len {
726                locals.push(val_type);
727            }
728        }
729        let body = Expr::decode(&mut reader)?;
730        Ok(Self { locals, body })
731    }
732}
733
734#[derive(Debug, Clone, Copy)]
735pub enum Blocktype {
736    Empty,
737    Val(Valtype),
738}
739
740impl Blocktype {
741    pub fn arity(self) -> usize {
742        match self {
743            Blocktype::Empty => 0,
744            Blocktype::Val(_) => 1,
745        }
746    }
747}
748
749impl<V: VectorFactory> Decode<V> for Blocktype {
750    fn decode(reader: &mut Reader) -> Result<Self, DecodeError> {
751        if reader.read_u8()? == 0x40 {
752            return Ok(Self::Empty);
753        }
754        reader.unread_u8();
755
756        let t = Decode::<V>::decode(reader)?;
757        Ok(Self::Val(t))
758    }
759}
760
761pub struct Data<V: VectorFactory> {
762    pub data: Memidx,
763    pub offset: I32ConstantExpr,
764    pub init: V::Vector<u8>,
765}
766
767impl<V: VectorFactory> Decode<V> for Data<V> {
768    fn decode(reader: &mut Reader) -> Result<Self, DecodeError> {
769        let data = Decode::<V>::decode(reader)?;
770        let offset = Decode::<V>::decode(reader)?;
771        let init = Decode::<V>::decode_vector(reader)?;
772        Ok(Self { data, offset, init })
773    }
774}
775
776impl<V: VectorFactory> Debug for Data<V> {
777    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
778        f.debug_struct("Data")
779            .field("data", &self.data)
780            .field("offset", &self.offset)
781            .field("init", &self.init.as_ref())
782            .finish()
783    }
784}
785
786impl<V: VectorFactory> Clone for Data<V> {
787    fn clone(&self) -> Self {
788        Self {
789            data: self.data,
790            offset: self.offset,
791            init: V::clone_vector(&self.init),
792        }
793    }
794}