Skip to content

Commit cbd0d45

Browse files
authored
Have into_ruby return a Cow<[u8]> (#833)
1 parent 95833f0 commit cbd0d45

File tree

4 files changed

+102
-78
lines changed

4 files changed

+102
-78
lines changed

librubyfmt/src/format_prism.rs

Lines changed: 52 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1245,7 +1245,7 @@ fn format_flip_flop_node<'src>(
12451245
if let Some(left) = flip_flop_node.left() {
12461246
format_node(ps, left);
12471247
}
1248-
ps.emit_op(loc_to_str(flip_flop_node.operator_loc()));
1248+
ps.emit_op(flip_flop_node.operator_loc().as_slice());
12491249
if let Some(right) = flip_flop_node.right() {
12501250
format_node(ps, right);
12511251
}
@@ -1285,7 +1285,7 @@ fn format_class_variable_and_write_node<'src>(
12851285
format_write_node(
12861286
ps,
12871287
const_to_str(class_variable_and_write_node.name()),
1288-
"&&=",
1288+
b"&&=",
12891289
class_variable_and_write_node.value(),
12901290
);
12911291
}
@@ -1297,7 +1297,9 @@ fn format_class_variable_operator_write_node<'src>(
12971297
format_write_node(
12981298
ps,
12991299
const_to_str(class_variable_operator_write_node.name()),
1300-
loc_to_str(class_variable_operator_write_node.binary_operator_loc()),
1300+
class_variable_operator_write_node
1301+
.binary_operator_loc()
1302+
.as_slice(),
13011303
class_variable_operator_write_node.value(),
13021304
);
13031305
}
@@ -1309,7 +1311,7 @@ fn format_class_variable_or_write_node<'src>(
13091311
format_write_node(
13101312
ps,
13111313
const_to_str(class_variable_or_write_node.name()),
1312-
"||=",
1314+
b"||=",
13131315
class_variable_or_write_node.value(),
13141316
);
13151317
}
@@ -1336,7 +1338,7 @@ fn format_class_variable_write_node<'src>(
13361338
format_write_node(
13371339
ps,
13381340
const_to_str(class_variable_write_node.name()),
1339-
"=",
1341+
b"=",
13401342
class_variable_write_node.value(),
13411343
);
13421344
}
@@ -1432,7 +1434,7 @@ fn format_def_body<'src>(ps: &mut ParserState<'src>, def_node: prism::DefNode<'s
14321434
});
14331435
} else {
14341436
ps.emit_space();
1435-
ps.emit_op("=");
1437+
ps.emit_op(b"=");
14361438
ps.emit_space();
14371439

14381440
ps.with_start_of_line(
@@ -2233,7 +2235,7 @@ fn format_call_chain_segments<'src>(
22332235
// Attr_write values are formatted after the breakable so they break independently
22342236
if let Some(value) = trailing_attr_write_value {
22352237
ps.emit_space();
2236-
ps.emit_op("=");
2238+
ps.emit_op(b"=");
22372239
ps.emit_space();
22382240
ps.with_start_of_line(false, |ps| format_node(ps, value));
22392241
}
@@ -2507,7 +2509,7 @@ fn format_call_and_write_node<'src>(
25072509
}
25082510

25092511
ps.emit_space();
2510-
ps.emit_op("&&=");
2512+
ps.emit_op(b"&&=");
25112513
ps.emit_space();
25122514

25132515
ps.with_start_of_line(false, |ps| format_node(ps, call_and_write_node.value()));
@@ -2530,7 +2532,7 @@ fn format_call_operator_write_node<'src>(
25302532
}
25312533

25322534
ps.emit_space();
2533-
ps.emit_op(loc_to_str(call_operator_write_node.binary_operator_loc()));
2535+
ps.emit_op(call_operator_write_node.binary_operator_loc().as_slice());
25342536
ps.emit_space();
25352537

25362538
ps.with_start_of_line(false, |ps| {
@@ -2555,7 +2557,7 @@ fn format_call_or_write_node<'src>(
25552557
}
25562558

25572559
ps.emit_space();
2558-
ps.emit_op("||=");
2560+
ps.emit_op(b"||=");
25592561
ps.emit_space();
25602562

25612563
ps.with_start_of_line(false, |ps| format_node(ps, call_or_write_node.value()));
@@ -3298,7 +3300,7 @@ fn format_local_variable_and_write_node<'src>(
32983300
format_write_node(
32993301
ps,
33003302
variable_name,
3301-
"&&=",
3303+
b"&&=",
33023304
local_variable_and_write_node.value(),
33033305
);
33043306
}
@@ -3312,7 +3314,9 @@ fn format_local_variable_operator_write_node<'src>(
33123314
format_write_node(
33133315
ps,
33143316
variable_name,
3315-
loc_to_str(local_variable_operator_write_node.binary_operator_loc()),
3317+
local_variable_operator_write_node
3318+
.binary_operator_loc()
3319+
.as_slice(),
33163320
local_variable_operator_write_node.value(),
33173321
);
33183322
}
@@ -3326,7 +3330,7 @@ fn format_local_variable_or_write_node<'src>(
33263330
format_write_node(
33273331
ps,
33283332
variable_name,
3329-
"||=",
3333+
b"||=",
33303334
local_variable_or_write_node.value(),
33313335
);
33323336
}
@@ -3354,7 +3358,7 @@ fn format_local_variable_write_node<'src>(
33543358
) {
33553359
let name = const_to_str(local_variable_write_node.name());
33563360
ps.bind_variable(name);
3357-
format_write_node(ps, name, "=", local_variable_write_node.value());
3361+
format_write_node(ps, name, b"=", local_variable_write_node.value());
33583362
}
33593363

33603364
fn format_splat_node<'src>(ps: &mut ParserState<'src>, splat_node: prism::SplatNode<'src>) {
@@ -3378,7 +3382,7 @@ fn format_instance_variable_write_node<'src>(
33783382
format_write_node(
33793383
ps,
33803384
const_to_str(instance_variable_write_node.name()),
3381-
"=",
3385+
b"=",
33823386
instance_variable_write_node.value(),
33833387
);
33843388
}
@@ -3492,7 +3496,7 @@ fn format_global_variable_and_write_node<'src>(
34923496
format_write_node(
34933497
ps,
34943498
const_to_str(global_variable_and_write_node.name()),
3495-
"&&=",
3499+
b"&&=",
34963500
global_variable_and_write_node.value(),
34973501
);
34983502
}
@@ -3504,7 +3508,9 @@ fn format_global_variable_operator_write_node<'src>(
35043508
format_write_node(
35053509
ps,
35063510
const_to_str(global_variable_operator_write_node.name()),
3507-
loc_to_str(global_variable_operator_write_node.binary_operator_loc()),
3511+
global_variable_operator_write_node
3512+
.binary_operator_loc()
3513+
.as_slice(),
35083514
global_variable_operator_write_node.value(),
35093515
);
35103516
}
@@ -3516,7 +3522,7 @@ fn format_global_variable_or_write_node<'src>(
35163522
format_write_node(
35173523
ps,
35183524
const_to_str(global_variable_or_write_node.name()),
3519-
"||=",
3525+
b"||=",
35203526
global_variable_or_write_node.value(),
35213527
);
35223528
}
@@ -3542,7 +3548,7 @@ fn format_global_variable_write_node<'src>(
35423548
format_write_node(
35433549
ps,
35443550
const_to_str(global_variable_write_node.name()),
3545-
"=",
3551+
b"=",
35463552
global_variable_write_node.value(),
35473553
);
35483554
}
@@ -3931,7 +3937,7 @@ fn format_index_and_write_node<'src>(
39313937
}
39323938

39333939
ps.emit_space();
3934-
ps.emit_op("&&=");
3940+
ps.emit_op(b"&&=");
39353941
ps.emit_space();
39363942

39373943
ps.with_start_of_line(false, |ps| format_node(ps, index_and_write_node.value()));
@@ -3952,7 +3958,7 @@ fn format_index_operator_write_node<'src>(
39523958
}
39533959

39543960
ps.emit_space();
3955-
ps.emit_op(loc_to_str(index_operator_write_node.binary_operator_loc()));
3961+
ps.emit_op(index_operator_write_node.binary_operator_loc().as_slice());
39563962
ps.emit_space();
39573963

39583964
ps.with_start_of_line(false, |ps| {
@@ -3975,7 +3981,7 @@ fn format_index_or_write_node<'src>(
39753981
}
39763982

39773983
ps.emit_space();
3978-
ps.emit_op("||=");
3984+
ps.emit_op(b"||=");
39793985
ps.emit_space();
39803986

39813987
ps.with_start_of_line(false, |ps| format_node(ps, index_or_write_node.value()));
@@ -4001,7 +4007,7 @@ fn format_instance_variable_and_write_node<'src>(
40014007
format_write_node(
40024008
ps,
40034009
const_to_str(instance_variable_and_write_node.name()),
4004-
"&&=",
4010+
b"&&=",
40054011
instance_variable_and_write_node.value(),
40064012
);
40074013
}
@@ -4013,7 +4019,9 @@ fn format_instance_variable_operator_write_node<'src>(
40134019
format_write_node(
40144020
ps,
40154021
const_to_str(instance_variable_operator_write_node.name()),
4016-
loc_to_str(instance_variable_operator_write_node.binary_operator_loc()),
4022+
instance_variable_operator_write_node
4023+
.binary_operator_loc()
4024+
.as_slice(),
40174025
instance_variable_operator_write_node.value(),
40184026
);
40194027
}
@@ -4025,7 +4033,7 @@ fn format_instance_variable_or_write_node<'src>(
40254033
format_write_node(
40264034
ps,
40274035
const_to_str(instance_variable_or_write_node.name()),
4028-
"||=",
4036+
b"||=",
40294037
instance_variable_or_write_node.value(),
40304038
);
40314039
}
@@ -4082,7 +4090,7 @@ fn format_constant_and_write_node<'src>(
40824090
format_write_node(
40834091
ps,
40844092
const_to_str(constant_and_write_node.name()),
4085-
"&&=",
4093+
b"&&=",
40864094
constant_and_write_node.value(),
40874095
);
40884096
}
@@ -4094,7 +4102,9 @@ fn format_constant_operator_write_node<'src>(
40944102
format_write_node(
40954103
ps,
40964104
const_to_str(constant_operator_write_node.name()),
4097-
loc_to_str(constant_operator_write_node.binary_operator_loc()),
4105+
constant_operator_write_node
4106+
.binary_operator_loc()
4107+
.as_slice(),
40984108
constant_operator_write_node.value(),
40994109
);
41004110
}
@@ -4106,7 +4116,7 @@ fn format_constant_or_write_node<'src>(
41064116
format_write_node(
41074117
ps,
41084118
const_to_str(constant_or_write_node.name()),
4109-
"||=",
4119+
b"||=",
41104120
constant_or_write_node.value(),
41114121
);
41124122
}
@@ -4118,7 +4128,7 @@ fn format_constant_path_and_write_node<'src>(
41184128
format_constant_path_write(
41194129
ps,
41204130
constant_path_and_write_node.target(),
4121-
"&&=",
4131+
b"&&=",
41224132
constant_path_and_write_node.value(),
41234133
);
41244134
}
@@ -4130,7 +4140,9 @@ fn format_constant_path_operator_write_node<'src>(
41304140
format_constant_path_write(
41314141
ps,
41324142
constant_path_operator_write_node.target(),
4133-
loc_to_str(constant_path_operator_write_node.binary_operator_loc()),
4143+
constant_path_operator_write_node
4144+
.binary_operator_loc()
4145+
.as_slice(),
41344146
constant_path_operator_write_node.value(),
41354147
);
41364148
}
@@ -4142,7 +4154,7 @@ fn format_constant_path_or_write_node<'src>(
41424154
format_constant_path_write(
41434155
ps,
41444156
constant_path_or_write_node.target(),
4145-
"||=",
4157+
b"||=",
41464158
constant_path_or_write_node.value(),
41474159
);
41484160
}
@@ -4174,7 +4186,7 @@ fn format_constant_path_write_node<'src>(
41744186
format_constant_path_write(
41754187
ps,
41764188
constant_path_write_node.target(),
4177-
"=",
4189+
b"=",
41784190
constant_path_write_node.value(),
41794191
);
41804192
}
@@ -4193,7 +4205,7 @@ fn format_constant_target_node<'src>(
41934205
fn format_constant_path_write<'src>(
41944206
ps: &mut ParserState<'src>,
41954207
target: prism::ConstantPathNode<'src>,
4196-
op: &'src str,
4208+
op: &'src [u8],
41974209
value: prism::Node<'src>,
41984210
) {
41994211
format_constant_path_node(ps, target);
@@ -4210,7 +4222,7 @@ fn format_constant_write_node<'src>(
42104222
format_write_node(
42114223
ps,
42124224
const_to_str(constant_write_node.name()),
4213-
"=",
4225+
b"=",
42144226
constant_write_node.value(),
42154227
);
42164228
}
@@ -4475,7 +4487,7 @@ fn format_optional_keyword_parameter_node<'src>(
44754487
let name = const_to_str(optional_keyword_parameter_node.name());
44764488
ps.bind_variable(name);
44774489
ps.emit_ident(name);
4478-
ps.emit_op(":");
4490+
ps.emit_op(b":");
44794491
ps.emit_space();
44804492
ps.with_start_of_line(false, |ps| {
44814493
format_node(ps, optional_keyword_parameter_node.value());
@@ -4490,7 +4502,7 @@ fn format_optional_parameter_node<'src>(
44904502
ps.bind_variable(name);
44914503
ps.emit_ident(name);
44924504
ps.emit_space();
4493-
ps.emit_op("=");
4505+
ps.emit_op(b"=");
44944506
ps.emit_space();
44954507
format_node(ps, optional_parameter_node.value());
44964508
}
@@ -4581,7 +4593,7 @@ fn format_range_node<'src>(ps: &mut ParserState<'src>, range_node: prism::RangeN
45814593
if let Some(left) = range_node.left() {
45824594
format_node(ps, left);
45834595
}
4584-
ps.emit_op(loc_to_str(range_node.operator_loc()));
4596+
ps.emit_op(range_node.operator_loc().as_slice());
45854597
if let Some(right) = range_node.right() {
45864598
format_node(ps, right);
45874599
}
@@ -4655,15 +4667,15 @@ fn format_rescue_node<'src>(ps: &mut ParserState<'src>, rescue_node: prism::Resc
46554667
}
46564668
if let Some(ref_node) = reference {
46574669
ps.emit_space();
4658-
ps.emit_op("=>");
4670+
ps.emit_op(b"=>");
46594671
ps.emit_space();
46604672
format_node(ps, ref_node);
46614673
}
46624674
});
46634675
});
46644676
} else if let Some(ref_node) = reference {
46654677
ps.emit_space();
4666-
ps.emit_op("=>");
4678+
ps.emit_op(b"=>");
46674679
ps.emit_space();
46684680
ps.with_start_of_line(false, |ps| {
46694681
format_node(ps, ref_node);
@@ -5046,7 +5058,7 @@ fn format_list_like_thing<'src>(
50465058
fn format_write_node<'src>(
50475059
ps: &mut ParserState<'src>,
50485060
name: &'src str,
5049-
op: &'src str,
5061+
op: &'src [u8],
50505062
value: prism::Node<'src>,
50515063
) {
50525064
ps.emit_ident(name);

0 commit comments

Comments
 (0)