@@ -1150,7 +1150,7 @@ impl Lua {
11501150 }
11511151 }
11521152
1153- /// Create and return an interned Lua string.
1153+ /// Creates and returns an interned Lua string.
11541154 ///
11551155 /// Lua strings can be arbitrary `[u8]` data including embedded nulls, so in addition to `&str`
11561156 /// and `&String`, you can also pass plain `&[u8]` here.
@@ -1159,27 +1159,32 @@ impl Lua {
11591159 unsafe { self . lock ( ) . create_string ( s) }
11601160 }
11611161
1162- /// Create and return a Luau [buffer] object from a byte slice of data.
1162+ /// Creates and returns a Luau [buffer] object from a byte slice of data.
11631163 ///
11641164 /// [buffer]: https://luau.org/library#buffer-library
11651165 #[ cfg( any( feature = "luau" , doc) ) ]
11661166 #[ cfg_attr( docsrs, doc( cfg( feature = "luau" ) ) ) ]
1167- pub fn create_buffer ( & self , buf : impl AsRef < [ u8 ] > ) -> Result < Buffer > {
1167+ pub fn create_buffer ( & self , data : impl AsRef < [ u8 ] > ) -> Result < Buffer > {
11681168 let lua = self . lock ( ) ;
1169- let state = lua . state ( ) ;
1169+ let data = data . as_ref ( ) ;
11701170 unsafe {
1171- if lua. unlikely_memory_error ( ) {
1172- crate :: util:: push_buffer ( state, buf. as_ref ( ) , false ) ?;
1173- return Ok ( Buffer ( lua. pop_ref ( ) ) ) ;
1174- }
1175-
1176- let _sg = StackGuard :: new ( state) ;
1177- check_stack ( state, 3 ) ?;
1178- crate :: util:: push_buffer ( state, buf. as_ref ( ) , true ) ?;
1179- Ok ( Buffer ( lua. pop_ref ( ) ) )
1171+ let ( ptr, buffer) = lua. create_buffer_with_capacity ( data. len ( ) ) ?;
1172+ ptr. copy_from_nonoverlapping ( data. as_ptr ( ) , data. len ( ) ) ;
1173+ Ok ( buffer)
11801174 }
11811175 }
11821176
1177+ /// Creates and returns a Luau [buffer] object with the specified size.
1178+ ///
1179+ /// Size limit is 1GB. All bytes will be initialized to zero.
1180+ ///
1181+ /// [buffer]: https://luau.org/library#buffer-library
1182+ #[ cfg( any( feature = "luau" , doc) ) ]
1183+ #[ cfg_attr( docsrs, doc( cfg( feature = "luau" ) ) ) ]
1184+ pub fn create_buffer_with_capacity ( & self , size : usize ) -> Result < Buffer > {
1185+ unsafe { Ok ( self . lock ( ) . create_buffer_with_capacity ( size) ?. 1 ) }
1186+ }
1187+
11831188 /// Creates and returns a new empty table.
11841189 #[ inline]
11851190 pub fn create_table ( & self ) -> Result < Table > {
0 commit comments